2
0

dumpyaml.pp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2024 by Michael Van Canneyt [email protected]
  4. YAML Parser & Dumping data demo
  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 dumpyaml;
  12. uses sysutils, fpyaml.types, fpyaml.data, fpyaml.parser;
  13. var
  14. minimal : Boolean;
  15. Procedure DumpYAMLValue(Y : TYAMLData; aPrefix : String); forward;
  16. function YAMLToString(Y : TYAMLData) : String;
  17. begin
  18. if Minimal then
  19. begin
  20. if (Y is TYAMLScalar) then
  21. Result:=Y.AsString
  22. else
  23. Result:=Y.ClassName;
  24. if TYAMLTagType.FromString(Y.Tag)=yttString then
  25. Result:='"'+Result+'"';
  26. end
  27. else
  28. if (Y is TYAMLScalar) then
  29. Result:=Format('scalar<%s> (%s)',[Y.AsString,Y.Tag])
  30. else
  31. Result:=Format('%s<%s>',[Y.ClassName,Y.Tag]);
  32. end;
  33. Procedure DumpYAMLMapping(Y : TYAMLMapping; aPrefix : String);
  34. Var
  35. I : Integer;
  36. lKey : String;
  37. begin
  38. if Minimal then
  39. Writeln('{')
  40. else
  41. Writeln('Mapping {');
  42. For I:=0 to Y.Count-1 do
  43. begin
  44. lKey:=YamlToString(Y.Key[I]);
  45. if not Minimal then
  46. lKey:=format('Key[%d] %s',[i,lKey]);
  47. Write(aPrefix+' ',lKey+': ');
  48. DumpYAMLValue(Y[i],' '+aPrefix);
  49. end;
  50. Writeln(aPrefix,'}');
  51. end;
  52. Procedure DumpYAMLSequence(Y : TYAMLSequence; aPrefix : String);
  53. Var
  54. I : Integer;
  55. begin
  56. if Minimal then
  57. Writeln('[')
  58. else
  59. Writeln('Sequence [');
  60. For I:=0 to Y.Count-1 do
  61. begin
  62. Write(aPrefix+' ');
  63. if Minimal then
  64. Write('- ' )
  65. else
  66. Write(format('Item[%d] :',[i]));
  67. DumpYAMLValue(Y[i],' '+aPrefix);
  68. end;
  69. Writeln(aPrefix,']');
  70. end;
  71. Procedure DumpYAMLValue(Y : TYAMLData; aPrefix : String);
  72. begin
  73. if (Y is TYAMLMapping) then
  74. DumpYAMLMapping(TYAMLMapping(Y),aPrefix)
  75. else if (Y is TYAMLSequence) then
  76. DumpYAMLSequence(TYAMLSequence(Y),aPrefix)
  77. else
  78. Writeln(YAMLToString(Y));
  79. end;
  80. Procedure DumpDocument(Y : TYAMLDocument);
  81. var
  82. I : Integer;
  83. begin
  84. For I:=0 to Y.Count-1 do
  85. DumpYAMLValue(Y[i],'');
  86. end;
  87. Procedure DumpYAML(Yaml : TYAMLStream);
  88. var
  89. Y : TYAMLData;
  90. I : Integer;
  91. begin
  92. Writeln('YAML Stream with ',YAML.Count,' items');
  93. For I:=0 to YAML.Count-1 do
  94. begin
  95. Y:=YAML[I];
  96. if Y is TYAMLDocument then
  97. begin
  98. Writeln('Document ',I,':');
  99. DumpDocument(TYAMLDocument(Y));
  100. end
  101. else
  102. Writeln('Item ',I,' : ',Y.ClassName,' : ',YAMLToString(Y));
  103. end;
  104. end;
  105. var
  106. YAML: TYAMLStream;
  107. P : TYAMLParser;
  108. begin
  109. MiniMal:=ParamStr(1)='-m';
  110. if (ParamStr(1+Ord(Minimal))='') or (ParamStr(1)='-h') then
  111. begin
  112. Writeln('Usage : ',ExtractFileName(ParamStr(0)),' [-m] inputfile');
  113. Writeln('-m : output minimal structural info');
  114. Halt(Ord(ParamStr(1)<>'-h'));
  115. end;
  116. YAML:=Nil;
  117. P:=TYAMLParser.Create(Paramstr(1+Ord(Minimal)));
  118. try
  119. YAML:=P.Parse;
  120. DumpYaml(YAML);
  121. finally
  122. YAML.Free;
  123. P.Free;
  124. end;
  125. end.