⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 awinidb.pas

📁 测试用例
💻 PAS
📖 第 1 页 / 共 2 页
字号:
        TempRec := AddWordToPtr(TempRec, CurItem^.DataSize);
        CurItem := CurItem^.Next;
      end;
      if (CurItem = Key) then
        iGetIniDataString := PChar(TempRec)
      else
        iGetIniDataString := nil;
    end;
  end;

  function iUpdateIniRecCount(Ini : PIniDatabase) : Integer;
    {-Update the NumEntries field in the .INI file }
  var
    Temp : array[0..5] of Char;

  begin
    with Ini^ do begin
      Long2StrZ(Temp, NumRecords);
      if not WritePrivateProfileString(dbDefaults, dbNumEntries, Temp, FName) then
        iUpdateIniRecCount := ecIniWrite
      else
        iUpdateIniRecCount := ecOK;
    end;
  end;

  function iPutIniString(Ini : PIniDatabase; Name, Key, Str : PChar) : Bool;
    {-Put a string to the .INI file }
  var
    TempStr : array[0..Length(NonValue)] of Char;

  begin
    { if the string is intentionally left blank, exit }
    if (Str = nil) or (Str[0] = #0) then begin
      GetPrivateProfileString(Name, Key, '', TempStr, SizeOf(TempStr), Ini^.FName);
      if (StrComp(TempStr, NonValue) = 0) then begin
        iPutIniString := True;
        Exit;
      end;
    end;

    { if the string <> '', write it out }
    if (Str <> nil) and (Str[0] <> #0) then
      iPutIniString := WritePrivateProfileString(Name, Key, Str, Ini^.FName)
    else
      { if the string = '', delete its database entry }
      iPutIniString := WritePrivateProfileString(Name, Key, nil, Ini^.FName);
  end;

  function iSaveIniRecord(Ini : PIniDatabase; SecName : PChar; var Rec) : Integer;
    {-Save an INI record to the database }
  var
    CurItem : PIniDatabaseKey;
    TempRec : Pointer;
    TempStr : PChar;
    Temp    : array[0..5] of Char;

  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iSaveIniRecord := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iSaveIniRecord := ecDatabaseNotPrepared;
        Exit;
      end;

      CurItem := DictionaryHead;
      TempRec := @Rec;
      while (CurItem <> nil) do begin
        if not CurItem^.Index then begin
          if CurItem^.StrType then
            TempStr := PChar(TempRec)
          else
            TempStr := Long2StrZ(Temp, Integer(TempRec^));

          if not iPutIniString(Ini, SecName, CurItem^.KeyName, TempStr) then begin
            iSaveIniRecord := ecIniWrite;
            Exit;
          end;
        end;

        TempRec := AddWordToPtr(TempRec, CurItem^.DataSize);
        CurItem := CurItem^.Next;
      end;
    end;

    iSaveIniRecord := ecOK;
  end;


  function iAddIniRecord(Ini : PIniDatabase; var Rec) : Integer;
    {-Add a record to the database }
  var
    IndexKey  : PIniDatabaseKey;
    IndexName : PChar;
    Code      : Integer;

  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iAddIniRecord := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iAddIniRecord := ecDatabaseNotPrepared;
        Exit;
      end;

      if (NumRecords = MaxDBRecs) then begin
        iAddIniRecord := ecDatabaseFull;
        Exit;
      end;

      IndexKey  := IniIndexKey(Ini);
      IndexName := iGetIniDataString(Ini, Rec, IndexKey);

      if iKeyExists(Ini, IndexName) then begin
        iAddIniRecord := ecRecordExists;
        Exit;
      end;

      { add the entry to the index }
      if not WritePrivateProfileString(dbIndex, IndexName, NonValue, FName) then begin
        iAddIniRecord := ecIniWrite;
        Exit;
      end;

      Code := iSaveIniRecord(Ini, IndexName, Rec);
      if (Code < ecOK) then begin
        iAddIniRecord := Code;
        Exit;
      end;

      Inc(Ini^.NumRecords);
      iAddIniRecord := iUpdateIniRecCount(Ini);
    end;
  end;

  function iUpdIniRecord(Ini : PIniDatabase; Key : PChar; var Rec) : Integer;
    {-Update a record in the database }
  var
    IndexKey  : PIniDatabaseKey;
    IndexName : PChar;
    Code      : Integer;

  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iUpdIniRecord := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iUpdIniRecord := ecDatabaseNotPrepared;
        Exit;
      end;

      IndexKey  := IniIndexKey(Ini);
      IndexName := iGetIniDataString(Ini, Rec, IndexKey);

      if not iKeyExists(Ini, Key) then begin
        iUpdIniRecord := ecRecordNotFound;
        Exit;
      end;

      if (StrIComp(Key, IndexName) <> 0) then begin
        { if the name has changed, first delete the old entry }
        Code := iDelIniRecord(Ini, Key);
        if (Code < ecOK) then begin
          iUpdIniRecord := Code;
          Exit;
        end;

        { add a new entry }
        iUpdIniRecord := iAddIniRecord(Ini, Rec);
      end else
        iUpdIniRecord := iSaveIniRecord(Ini, Key, Rec);
    end;
  end;

  function iDelIniRecord(Ini : PIniDatabase; Key : PChar) : Integer;
    {-Remove a record from the database }
  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iDelIniRecord := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iDelIniRecord := ecDatabaseNotPrepared;
        Exit;
      end;

      if not iKeyExists(Ini, Key) then begin
        iDelIniRecord := ecRecordNotFound;
        Exit;
      end;

      iDelIniRecord := ecIniWrite;

      { delete the index entry }
      if not WritePrivateProfileString(dbIndex, Key, nil, FName) then
        Exit;

      { delete the record }
      if not WritePrivateProfileString(Key, nil, nil, FName) then
        Exit;

      { update the record count }
      Dec(NumRecords);
      iDelIniRecord := iUpdateIniRecCount(Ini);
    end;
  end;

  function iGetIniRecord(Ini : PIniDatabase; Key : PChar; var Rec) : Integer;
    {-Get a record from the database }
  var
    TempRec : Pointer;
    DefRec  : Pointer;
    CurItem : PIniDatabaseKey;

  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iGetIniRecord := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iGetIniRecord := ecDatabaseNotPrepared;
        Exit;
      end;

      if not iKeyExists(Ini, Key) then begin
        iGetIniRecord := ecRecordNotFound;
        Exit;
      end;

      TempRec := @Rec;
      DefRec  := DefaultRecord;

      CurItem := DictionaryHead;
      while (CurItem <> nil) do begin
        if CurItem^.StrType then
          if CurItem^.Index then
            StrCopy(PChar(TempRec), Key)
          else begin
            GetPrivateProfileString(  Key,
                                      CurItem^.KeyName,
                                      PChar(DefRec),
                                      PChar(TempRec),
                                      CurItem^.DataSize,
                                      FName );

            if (StrComp(PChar(TempRec), NonValue) = 0) then
              PChar(TempRec)[0] := #0;
          end
        else
          Integer(TempRec^) := GetPrivateProfileInt(  Key,
                                                      CurItem^.KeyName,
                                                      Integer(DefRec^),
                                                      FName );

        TempRec := AddWordToPtr(TempRec, CurItem^.DataSize);
        DefRec  := AddWordToPtr(DefRec, CurItem^.DataSize);
        CurItem := CurItem^.Next;
      end;

      iGetIniRecord := ecOK;
    end;
  end;

  function iGetIniIndexRecSize(Ini : PIniDatabase) : Integer;
    {-Get the size of the .INI index buffer }
  begin
    iGetIniIndexRecSize := (Ini^.NumRecords * MaxIndexLen) + 1;
  end;

  function iAllocIniIndexRec(     Ini     : PIniDatabase;
                              var IRec    : PChar;
                              var BufSize : Integer) : Integer;
    {-Allocate a buffer for the record index }
  begin
    with Ini^ do begin
      BufSize := iGetIniIndexRecSize(Ini);
      if (BufSize = 0) then begin
        iAllocIniIndexRec := ecDatabaseEmpty;
        Exit;
      end;

      IRec := AllocMem(BufSize);
      iAllocIniIndexRec := ecOK;
    end;
  end;

  procedure iDeallocIniIndexRec(     Ini     : PIniDatabase;
                                 var IRec    : PChar;
                                     BufSize : Integer );
    {-Deallocate an index buffer }
  begin
    FreeMem(IRec, BufSize);
  end;

  function iLoadIniIndex(Ini : PIniDatabase; Buf : PChar; BufSize : Integer) : Integer;
    {-Load the INI index into Buf }
  begin
    if (Ini^.NumRecords = 0) then begin
      iLoadIniIndex := ecDatabaseEmpty;
      Exit;
    end;

    if Ini^.Prepared then
      if (GetPrivateProfileString(dbIndex, nil, nil, Buf, BufSize, Ini^.FName) = 0) then
        iLoadIniIndex := ecIniRead
      else
        iLoadIniIndex := ecOK
    else
      iLoadIniIndex := ecDatabaseNotPrepared;
  end;

  function iNumIniRecs(Ini : PIniDatabase) : Integer;
    {-Return the number of records in an INI database }
  begin
    iNumIniRecs := Ini^.NumRecords;
  end;

  function iWriteToIni(Ini : PIniDatabase; var Rec; Section, IniFile : PChar) : Integer;
    {-Write the record to a user-specified .INI file }
  var
    CurItem : PIniDatabaseKey;
    TempRec : Pointer;
    TempStr : PChar;
    Temp    : array[0..5] of Char;

  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iWriteToIni := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iWriteToIni := ecDatabaseNotPrepared;
        Exit;
      end;

      CurItem := DictionaryHead;
      TempRec := @Rec;
      while (CurItem <> nil) do begin
        if CurItem^.StrType then
          TempStr := PChar(TempRec)
        else
          TempStr := Long2StrZ(Temp, Integer(TempRec^));

        if not WritePrivateProfileString(Section, CurItem^.KeyName, TempStr, IniFile) then begin
          iWriteToIni := ecIniWrite;
          Exit;
        end;

        TempRec := AddWordToPtr(TempRec, CurItem^.DataSize);
        CurItem := CurItem^.Next;
      end;
    end;

    iWriteToIni := ecOK;
  end;

  function iReadFromIni(Ini : PIniDatabase; var Rec; Section, IniFile : PChar) : Integer;
    {-Read the record from a user-specified .INI file }
  var
    TempRec : Pointer;
    DefRec  : Pointer;
    CurItem : PIniDatabaseKey;

  begin
    with Ini^ do begin
      { if there are no items defined, it's an error }
      if (DictionaryHead = nil) then begin
        iReadFromIni := ecNoFieldsDefined;
        Exit;
      end;

      if not Prepared then begin
        iReadFromIni := ecDatabaseNotPrepared;
        Exit;
      end;

      FillChar(Rec, RecordSize, 0);
      TempRec := @Rec;
      DefRec  := DefaultRecord;
      CurItem := DictionaryHead;
      while (CurItem <> nil) do begin
        if CurItem^.StrType then
          GetPrivateProfileString(Section,
                                  CurItem^.KeyName,
                                  PChar(DefRec),
                                  PChar(TempRec),
                                  CurItem^.DataSize,
                                  IniFile)
        else
          Integer(TempRec^) := GetPrivateProfileInt(Section,
                                                    CurItem^.KeyName,
                                                    Integer(DefRec^),
                                                    IniFile);

        TempRec := AddWordToPtr(TempRec, CurItem^.DataSize);
        DefRec  := AddWordToPtr(DefRec, CurItem^.DataSize);
        CurItem := CurItem^.Next;
      end;
    end;

    iReadFromIni := ecOK;
  end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -