Merge stever@zizzer:/bk/m5 into isabel.reinhardt.house:/z/stever/bk/m5
[gem5.git] / test / initest.cc
1 /*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <iostream>
30 #include <fstream>
31 #include <string>
32 #include <vector>
33
34 #include "base/inifile.hh"
35 #include "base/cprintf.hh"
36
37 using namespace std;
38
39 char *progname;
40
41 void
42 usage()
43 {
44 cout << "Usage: " << progname << " <ini file>\n";
45 exit(1);
46 }
47
48 #if 0
49 char *defines = getenv("CONFIG_DEFINES");
50 if (defines) {
51 char *c = defines;
52 while ((c = strchr(c, ' ')) != NULL) {
53 *c++ = '\0';
54 count++;
55 }
56 count++;
57 }
58
59 #endif
60
61 int
62 main(int argc, char *argv[])
63 {
64 IniFile simConfigDB;
65
66 progname = argv[0];
67
68 vector<char *> cppArgs;
69
70 vector<char *> cpp_options;
71 cpp_options.reserve(argc * 2);
72
73 for (int i = 1; i < argc; ++i) {
74 char *arg_str = argv[i];
75
76 // if arg starts with '-', parse as option,
77 // else treat it as a configuration file name and load it
78 if (arg_str[0] == '-') {
79
80 // switch on second char
81 switch (arg_str[1]) {
82 case 'D':
83 case 'U':
84 case 'I':
85 // cpp options: record & pass to cpp. Note that these
86 // cannot have spaces, i.e., '-Dname=val' is OK, but
87 // '-D name=val' is not. I don't consider this a
88 // problem, since even though gnu cpp accepts the
89 // latter, other cpp implementations do not (Tru64,
90 // for one).
91 cppArgs.push_back(arg_str);
92 break;
93
94 case '-':
95 // command-line configuration parameter:
96 // '--<section>:<parameter>=<value>'
97
98 if (!simConfigDB.add(arg_str + 2)) {
99 // parse error
100 ccprintf(cerr,
101 "Could not parse configuration argument '%s'\n"
102 "Expecting --<section>:<parameter>=<value>\n",
103 arg_str);
104 exit(0);
105 }
106 break;
107
108 default:
109 usage();
110 }
111 }
112 else {
113 // no '-', treat as config file name
114
115 if (!simConfigDB.loadCPP(arg_str, cppArgs)) {
116 cprintf("Error processing file %s\n", arg_str);
117 exit(1);
118 }
119 }
120 }
121
122 string value;
123
124 #define FIND(C, E) \
125 if (simConfigDB.find(C, E, value)) \
126 cout << ">" << value << "<\n"; \
127 else \
128 cout << "Not Found!\n"
129
130 FIND("General", "Test2");
131 FIND("Junk", "Test3");
132 FIND("Junk", "Test4");
133 FIND("General", "Test1");
134 FIND("Junk2", "test3");
135 FIND("General", "Test3");
136
137 cout << "\n";
138
139 simConfigDB.dump();
140
141 return 0;
142 }