forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibcm-parse.cpp.html
134 lines (121 loc) · 22.2 KB
/
ibcm-parse.cpp.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>ibcm-parse.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">/* A program that will quickly check if the file names passed in via</span></i>
<i><span style="color:#9A1900"> * command line arguments look like IBCM files. In particular, it</span></i>
<i><span style="color:#9A1900"> * checks if the first four digits on each line are all hexadecimal</span></i>
<i><span style="color:#9A1900"> * digits. It does not program validity checking beyond this. It is</span></i>
<i><span style="color:#9A1900"> * useful to tell the students if, on submission, their programs will</span></i>
<i><span style="color:#9A1900"> * parse correctly in an IBCM simulator.</span></i>
<i><span style="color:#9A1900"> */</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><fstream></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><stdlib.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><ctype.h></span>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><string.h></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span><span style="color:#009900">char</span><span style="color:#990000">*</span> name<span style="color:#990000">);</span>
<span style="color:#009900">bool</span> <b><span style="color:#000000">isEmpty</span></b><span style="color:#990000">(</span>string<span style="color:#990000">&</span> line<span style="color:#990000">);</span>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span> <span style="color:#990000">*</span>argv<span style="color:#990000">[])</span> <span style="color:#FF0000">{</span>
<span style="color:#008080">string</span> line<span style="color:#990000">;</span>
<span style="color:#009900">int</span> linenum <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#009900">bool</span> allowComments <span style="color:#990000">=</span> <b><span style="color:#0000FF">false</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// make sure they gave some command-line parameters</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> argc <span style="color:#990000">==</span> <span style="color:#993399">1</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": no input files"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">int</span> i <span style="color:#990000">=</span> <span style="color:#993399">1</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> argc<span style="color:#990000">;</span> i<span style="color:#990000">++</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// print the help description</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span> <span style="color:#FF0000">"-help"</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]);</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// a comment has a '#' as the first character on a line or</span></i>
<i><span style="color:#9A1900">// a comment has a '//' as the first two characters on a line</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span><b><span style="color:#000000">strcmp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">],</span><span style="color:#FF0000">"-allowcomments"</span><span style="color:#990000">)</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
allowComments <span style="color:#990000">=</span> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> argv<span style="color:#990000">[</span>i<span style="color:#990000">][</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'-'</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span>i<span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": no such argument"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]);</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// open the file</span></i>
<span style="color:#008080">ifstream</span> <b><span style="color:#000000">file</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span>i<span style="color:#990000">]);</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span>file<span style="color:#990000">.</span><b><span style="color:#000000">is_open</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span>i<span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": no such file"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">2</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// read in the entire file...</span></i>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span> file<span style="color:#990000">.</span><b><span style="color:#000000">good</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// read in a line from the file</span></i>
<b><span style="color:#000000">getline</span></b> <span style="color:#990000">(</span>file<span style="color:#990000">,</span> line<span style="color:#990000">);</span>
linenum<span style="color:#990000">++;</span>
<i><span style="color:#9A1900">// is it the last line of the file?</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">(</span>line<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000">==</span> <span style="color:#993399">0</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(!</span>file<span style="color:#990000">.</span><b><span style="color:#000000">good</span></b><span style="color:#990000">())</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">break</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// is the line empty or all whitespace?</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <b><span style="color:#000000">isEmpty</span></b><span style="color:#990000">(</span>line<span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// is it a `//` comment?</span></i>
<b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> allowComments <span style="color:#990000">&&</span> <span style="color:#990000">(</span>line<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000">>=</span> <span style="color:#993399">2</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span>
<span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'/'</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span> <span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'/'</span><span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// is it a `#` comment?</span></i>
<b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> allowComments <span style="color:#990000">&&</span> <span style="color:#990000">(</span>line<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000">>=</span> <span style="color:#993399">1</span><span style="color:#990000">)</span> <span style="color:#990000">&&</span>
<span style="color:#990000">(</span>line<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000">==</span> <span style="color:#FF0000">'#'</span><span style="color:#990000">)</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">continue</span></b><span style="color:#990000">;</span>
<i><span style="color:#9A1900">// is the line too short?</span></i>
<b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> line<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000"><</span> <span style="color:#993399">4</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span>i<span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">" has too short a line on line number "</span> <span style="color:#990000"><<</span> linenum <span style="color:#990000"><<</span> <span style="color:#FF0000">"("</span> <span style="color:#990000"><<</span> line<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">")"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">3</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// are the first four digits hex characters?</span></i>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span> <span style="color:#009900">int</span> j <span style="color:#990000">=</span> <span style="color:#993399">0</span><span style="color:#990000">;</span> j <span style="color:#990000"><</span> <span style="color:#993399">4</span><span style="color:#990000">;</span> j<span style="color:#990000">++</span> <span style="color:#990000">)</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> <span style="color:#990000">!</span><b><span style="color:#000000">isxdigit</span></b><span style="color:#990000">(</span>line<span style="color:#990000">[</span>j<span style="color:#990000">])</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">0</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">": "</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span>i<span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">" invalid hexadecimal digit '"</span> <span style="color:#990000"><<</span> line<span style="color:#990000">[</span>j<span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">"' on line "</span> <span style="color:#990000"><<</span> linenum <span style="color:#990000"><<</span> <span style="color:#FF0000">" character "</span> <span style="color:#990000"><<</span> <span style="color:#990000">(</span>j<span style="color:#990000">+</span><span style="color:#993399">1</span><span style="color:#990000">)</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">3</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// all done with this file!</span></i>
file<span style="color:#990000">.</span><b><span style="color:#000000">close</span></b><span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#993399">0</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">void</span> <b><span style="color:#000000">printHelp</span></b><span style="color:#990000">(</span><span style="color:#009900">char</span> <span style="color:#990000">*</span>name<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">static</span></b> <span style="color:#009900">bool</span> helpPrinted<span style="color:#990000">;</span> <i><span style="color:#9A1900">// Static values are initialized to 0 or 0-equivalent</span></i>
<b><span style="color:#0000FF">if</span></b><span style="color:#990000">(</span>helpPrinted<span style="color:#990000">)</span> <b><span style="color:#0000FF">return</span></b><span style="color:#990000">;</span>
helpPrinted <span style="color:#990000">=</span> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Usage: "</span> <span style="color:#990000"><<</span> name <span style="color:#990000"><<</span> <span style="color:#FF0000">" [option] <inputfile>"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Options:"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-allowcomments]</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">Allows file specified by <inputfile> to contain"</span> <span style="color:#990000"><<</span> endl
<span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t\t\t\t</span><span style="color:#FF0000">lines beginning with `//` and `#`."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"</span><span style="color:#CC33CC">\t</span><span style="color:#FF0000">[-help]</span><span style="color:#CC33CC">\t\t\t</span><span style="color:#FF0000">Prints this help message."</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<span style="color:#009900">bool</span> <b><span style="color:#000000">isEmpty</span></b><span style="color:#990000">(</span>string<span style="color:#990000">&</span> line<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// is the line empty?</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> line<span style="color:#990000">.</span><b><span style="color:#000000">empty</span></b><span style="color:#990000">()</span> <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// does the line contain spaces or carriage returns?</span></i>
<b><span style="color:#0000FF">else</span></b> <b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> line<span style="color:#990000">.</span><b><span style="color:#000000">find_first_not_of</span></b><span style="color:#990000">(</span><span style="color:#FF0000">' '</span><span style="color:#990000">)</span> <span style="color:#990000">!=</span> line<span style="color:#990000">.</span>npos <span style="color:#990000">&&</span>
line<span style="color:#990000">.</span><b><span style="color:#000000">find_first_not_of</span></b><span style="color:#990000">(</span><span style="color:#FF0000">'</span><span style="color:#CC33CC">\n</span><span style="color:#FF0000">'</span><span style="color:#990000">)</span> <span style="color:#990000">!=</span> line<span style="color:#990000">.</span>npos <span style="color:#990000">&&</span>
line<span style="color:#990000">.</span><b><span style="color:#000000">find_first_not_of</span></b><span style="color:#990000">(</span><span style="color:#FF0000">'</span><span style="color:#CC33CC">\r</span><span style="color:#FF0000">'</span><span style="color:#990000">)</span> <span style="color:#990000">!=</span> line<span style="color:#990000">.</span>npos<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">false</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// otherwise, the line is basically empty</span></i>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>