testgdbm.pp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. {
  2. Copyright (c) 1999-2000 by Michael Van Canneyt, member of
  3. the Free Pascal development team
  4. Test raw gdbm header translations.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program testgdbm;
  12. {$mode objfpc}
  13. {$h+}
  14. uses sysutils,gdbm;
  15. Var
  16. dbf : pgdbm_file;
  17. Key,Data : TDatum;
  18. A,B : String;
  19. i : longint;
  20. begin
  21. dbf:=gdbm_open('test.dat',512,GDBM_NEWDB,432,nil);
  22. If dbf=Nil then
  23. Writeln('Error when creating database');
  24. for I:=1 to 10 do
  25. begin
  26. A:=Format('data for string %d',[i]);
  27. B:=Format('string %d',[i]);
  28. Data.dptr:=Pchar(A);
  29. Data.dsize:=length(A);
  30. key.dptr:=pchar(B);
  31. key.dsize:=length(B);
  32. if gdbm_store(dbf,key,data,gdbm_insert)<>0 then
  33. Writeln('Error inserting data')
  34. else
  35. Writeln('Inserted string ',i)
  36. end;
  37. key:=gdbm_firstkey(dbf);
  38. I:=0;
  39. While key.dptr<>nil do
  40. begin
  41. inc(i);
  42. data:=gdbm_fetch(dbf,key);
  43. writeln('Data for key ',i,' (',key.dptr,') : ',data.dptr);
  44. key:=gdbm_nextkey(dbf,key);
  45. end;
  46. gdbm_close(dbf);
  47. end.