headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / add-ons / print / ppd / test / TestParser.cpp
blobed85a66ae1df8a74eb8934dd61e407729b2df903
1 /*
2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Michael Pfeiffer <laplace@users.sourceforge.net>
7 */
9 #include "Parser.h"
10 #include "Test.h"
12 #include <StopWatch.h>
13 #include <stdio.h>
15 void TestParser()
17 Parser parser(gPPDFile);
18 if (parser.InitCheck() != B_OK) {
19 fprintf(stderr, "Could not open ppd file %s\n", gPPDFile);
20 return;
23 Statement* statement;
24 do {
25 statement = parser.Parse();
26 if (statement != NULL) {
27 statement->Print();
29 delete statement;
30 } while (statement != NULL);
33 #include "PPDParser.h"
35 static PPD* OpenTestFile(bool all, bool timing)
37 BStopWatch* stopWatch = NULL;
38 if (timing) {
39 stopWatch = new BStopWatch("PPDParser");
41 PPDParser parser(gPPDFile);
43 if (parser.InitCheck() != B_OK) {
44 fprintf(stderr, "Could not open ppd file %s\n", gPPDFile);
45 return NULL;
48 PPD* ppd = all ? parser.ParseAll() : parser.ParseHeader();
50 delete stopWatch;
52 if (ppd == NULL) {
53 fprintf(stderr, "Parser returned NULL\n");
54 fprintf(stderr, "%s\n", parser.GetErrorMessage());
55 return NULL;
58 return ppd;
61 void TestPPDParser(bool all, bool verbose)
63 PPD* ppd = OpenTestFile(all, !verbose);
64 if (ppd == NULL) return;
65 if (verbose) {
66 ppd->Print();
68 delete ppd;
71 void ExtractChildren(StatementList* list, int level);
73 void Indent(int level)
75 for (; level > 0; level --) {
76 printf(" ");
80 void PrintValue(const char* label, Value* arg, int level)
82 Indent(level);
84 if (label != NULL) {
85 printf("%s ", label);
88 if (arg != NULL) {
89 BString* value = arg->GetValue();
90 BString* translation = arg->GetTranslation();
91 if (translation != NULL) {
92 printf("%s", translation->String());
94 if (value != NULL) {
95 printf(" [%s]", value->String());
97 } else {
98 printf("NULL");
101 printf("\n");
104 bool ExtractGroup(Statement* statement, int level)
106 GroupStatement group(statement);
107 if (group.IsOpenGroup()) {
108 const char* translation = group.GetGroupTranslation();
109 Indent(level);
110 if (translation != NULL) {
111 printf("%s", translation);
113 const char* name = group.GetGroupName();
114 if (name != NULL) {
115 printf("[%s]", name);
117 printf("\n");
118 ExtractChildren(statement->GetChildren(), level+1);
119 return true;
121 return false;
124 void ExtractChildren(StatementList* list, int level)
126 if (list == NULL) return;
127 for (int32 i = 0; i < list->Size(); i ++) {
128 Statement* statement = list->StatementAt(i);
129 if (!ExtractGroup(statement, level)) {
130 if (statement->GetType() == Statement::kValue) {
131 PrintValue(NULL, statement->GetOption(), level);
132 } else if (statement->GetType() == Statement::kDefault) {
133 PrintValue("Default", statement->GetValue(), level);
139 void TestExtractUI()
141 PPD* ppd = OpenTestFile(true, false);
142 if (ppd == NULL) return;
144 for (int32 i = 0; i < ppd->Size(); i++) {
145 Statement* statement = ppd->StatementAt(i);
146 ExtractGroup(statement, 0);