1/*
2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 *		Michael Pfeiffer <laplace@users.sourceforge.net>
7 */
8
9#include "Parser.h"
10#include "Test.h"
11
12#include <StopWatch.h>
13#include <stdio.h>
14
15void TestParser()
16{
17	Parser parser(gPPDFile);
18	if (parser.InitCheck() != B_OK) {
19		fprintf(stderr, "Could not open ppd file %s\n", gPPDFile);
20		return;
21	}
22
23	Statement* statement;
24	do {
25		statement = parser.Parse();
26		if (statement != NULL) {
27			statement->Print();
28		}
29		delete statement;
30	} while (statement != NULL);
31}
32
33#include "PPDParser.h"
34
35static PPD* OpenTestFile(bool all, bool timing)
36{
37	BStopWatch* stopWatch = NULL;
38	if (timing) {
39		stopWatch = new BStopWatch("PPDParser");
40	}
41	PPDParser parser(gPPDFile);
42
43	if (parser.InitCheck() != B_OK) {
44		fprintf(stderr, "Could not open ppd file %s\n", gPPDFile);
45		return NULL;
46	}
47
48	PPD* ppd = all ? parser.ParseAll() : parser.ParseHeader();
49
50	delete stopWatch;
51
52	if (ppd == NULL) {
53		fprintf(stderr, "Parser returned NULL\n");
54		fprintf(stderr, "%s\n", parser.GetErrorMessage());
55		return NULL;
56	}
57
58	return ppd;
59}
60
61void TestPPDParser(bool all, bool verbose)
62{
63	PPD* ppd = OpenTestFile(all, !verbose);
64	if (ppd == NULL) return;
65	if (verbose) {
66		ppd->Print();
67	}
68	delete ppd;
69}
70
71void ExtractChildren(StatementList* list, int level);
72
73void Indent(int level)
74{
75	for (; level > 0; level --) {
76		printf("  ");
77	}
78}
79
80void PrintValue(const char* label, Value* arg, int level)
81{
82	Indent(level);
83
84	if (label != NULL) {
85		printf("%s ", label);
86	}
87
88	if (arg != NULL) {
89		BString* value = arg->GetValue();
90		BString* translation = arg->GetTranslation();
91		if (translation != NULL) {
92			printf("%s", translation->String());
93		}
94		if (value != NULL) {
95			printf(" [%s]", value->String());
96		}
97	} else {
98		printf("NULL");
99	}
100
101	printf("\n");
102}
103
104bool ExtractGroup(Statement* statement, int level)
105{
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);
112		}
113		const char* name = group.GetGroupName();
114		if (name != NULL) {
115			printf("[%s]", name);
116		}
117		printf("\n");
118		ExtractChildren(statement->GetChildren(), level+1);
119		return true;
120	}
121	return false;
122}
123
124void ExtractChildren(StatementList* list, int level)
125{
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);
134			}
135		}
136	}
137}
138
139void TestExtractUI()
140{
141	PPD* ppd = OpenTestFile(true, false);
142	if (ppd == NULL) return;
143
144	for (int32 i = 0; i < ppd->Size(); i++) {
145		Statement* statement = ppd->StatementAt(i);
146		ExtractGroup(statement, 0);
147	}
148}
149