confdemo.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. {
  2. This file is part of the Free Component Library
  3. JSON Config file demo
  4. Copyright (c) 2007 by Michael Van Canneyt [email protected]
  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 confdemo;
  12. {$mode objfpc}{$H+}
  13. uses
  14. Classes,
  15. { add your units here }
  16. jsonconf;
  17. Procedure TestConf;
  18. Var
  19. C : TJSONConfig;
  20. L : TStrings;
  21. I : Integer;
  22. begin
  23. // TJSONConf is component, so needs an owner.
  24. C:=TJSONConfig.Create(nil);
  25. Try
  26. // Set filename. This will read the file.
  27. C.FileName:='sample.conf';
  28. // Set an integer value "a" equal to 1 in the root object
  29. C.SetValue('/a',1);
  30. // Set a integer value "a" equal to 2 in the object "b" below root.
  31. C.SetValue('b/a',2);
  32. // Set a string value "b" equal to 1 in the object "b" below root.
  33. C.SetValue('b/b','Some String');
  34. // Set a float value "c" equal to 1.23 in the object "b" below root.
  35. C.SetValue('b/c',1.23);
  36. // Set a boolean value "d" equal to "False" in the object "b" below root.
  37. C.SetValue('b/d',False);
  38. // Read values:
  39. // Integer. If none found, 0 is returned)
  40. Writeln('/a :',C.GetValue('/a',0));
  41. // String. If none found, a default 'XYZ' is returned)
  42. Writeln('/b/b :',C.GetValue('/b/b','XYZ'));
  43. // Float. If none found, 0 is returned)
  44. Writeln('/b/c :',C.GetValue('/b/c',0));
  45. // Boolean. If none found, true is returned)
  46. Writeln('/b/d :',C.GetValue('/b/d',true));
  47. // You can open a key. All paths are then relative to the open key.
  48. // The default open key is the root key.
  49. // The second element determines if the key should b created if it does not exist.
  50. C.OpenKey('/b',False);
  51. // Read relative to b
  52. Writeln('a, relative to key (/b):',C.GetValue('a',0));
  53. // Absolute paths disregard the open key
  54. Writeln('/a, absolute:',C.GetValue('/a',0));
  55. // Reset or closekey reset the open key to the root key.
  56. C.OpenKey('/b/c/d/e',True);
  57. C.SetValue('q','Q is good for you');
  58. // Opening keys also works relative:
  59. C.OpenKey('/b',False);
  60. Writeln('a, in b : ',C.GetValue('a',0));
  61. C.OpenKey('c/d/e',False);
  62. Writeln('q, in /b, then c/d/e : ',C.GetValue('q',''));
  63. C.ResetKey;
  64. C.OpenKey('/b2',True);
  65. C.OpenKey('/b3',True);
  66. L:=TStringList.Create;
  67. try
  68. // You can enumerate keys below a certain key:
  69. C.EnumSubKeys('/',L);
  70. Writeln('Found ',L.Count,' keys below root key: ');
  71. For I:=0 to L.Count-1 do
  72. Writeln(i+1,': ',L[I]);
  73. // You can also enumerate the values below a certain key:
  74. L.Clear;
  75. C.EnumValues('/b',L);
  76. Writeln('Found ',L.Count,' values below "/b" key: ');
  77. For I:=0 to L.Count-1 do
  78. Writeln(i+1,': ',L[I]);
  79. finally
  80. L.Free;
  81. end;
  82. // Write all in-memory changes to disk
  83. C.Flush;
  84. Finally
  85. C.Free;
  86. end;
  87. end;
  88. begin
  89. TestConf;
  90. end.