comparison tests/agcl/parsifal/sort.syn @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 {
2 class String {
3 char *value;
4 public:
5 String();
6 String(unsigned char *);
7 int operator < (const String &);
8 };
9 }
10
11 char = 33..127 - '<' - '>'
12 blank = 32 + '\t'
13 eof = 0
14
15 [
16 context type = unsigned char *
17 ]
18
19 (unsigned char *) words
20 -> word =PCB.pointer;
21 -> words, blank..., word =PCB.pointer;
22
23 word = char...
24
25 (String) text
26 -> words:p, blank?... =String(p);
27
28 (String) address
29 -> '<', blank?..., words:w, blank?..., '>' =w;
30
31 line
32 -> blank?..., text:t =map[t] = t;
33 -> blank?..., text, address:a =map[a] = String();
34 -> blank?..., address:a, text =map[a] = String();
35
36 grammar
37 -> line?..., eof
38
39 {
40 #include <string.h>
41 #include <stdio.h>
42 #include "agmap.h"
43
44
45 #define GET_CONTEXT CONTEXT = PCB.pointer
46
47 AgMap<String, String> map;
48
49 String::String() : value(new char[PCB.pointer - CONTEXT + 1]) {
50 int length = PCB.pointer - CONTEXT;
51 strncpy(value, CONTEXT, length);
52 value[length] = 0;
53 }
54
55 String::String(unsigned char *p) : value(new char[p - CONTEXT + 1]) {
56 int length = p - CONTEXT;
57 strncpy(value, CONTEXT, length);
58 value[length] = 0;
59 }
60
61 int operator < (const String &s) {
62 return strcmp(value, s.value) < 0;
63 }
64
65 enum ConditionCode {
66 success,
67 incorrectArgumentCount,
68 cannotOpenInput,
69 cannotSeek,
70 ftellFailure,
71 insufficientMemory,
72 readError,
73 parseError
74 };
75
76 int main(int argc, char *argv[]) {
77
78 /* Check for enough arguments */
79 if (argc != 2) {
80 printf("Usage: %s <filename>\n", argv[0]);
81 return incorrectArgumentCount;
82 }
83
84 /* Open input file */
85 FILE *input = fopen(argv[1],"r");
86 if (input == NULL) {
87 printf("Cannot open %s\n", argv[1]);
88 return cannotOpenInput;
89 }
90
91 /* find out how big the file is */
92 if (fseek(input, SEEK_SET, SEEK_END)) {
93 printf("Seek error on %s\n", argv[1]);
94 return cannotSeek;
95 }
96 long fileLength = ftell(input);
97 if (fileLength < 0 ) { // -1L is error return
98 printf("Error getting file length (%d) of %s\n", fileLength, argv[1]);
99 return ftellFailure;
100 }
101 /* fseek to beginning of file */
102 if (fseek(input, 0, SEEK_SET)) {
103 printf("Seek error on %s\n", argv[1]);
104 return cannotSeek;
105 }
106
107 /* Allocate storage for input string */
108 char *scriptString = new char[(size_t) fileLength + 1];
109 if (scriptString == NULL) {
110 printf("Insufficient memory\n");
111 return insufficientMemory;
112 }
113
114 /* read file */
115 size_t stringLength = fread(scriptString, 1, (unsigned)fileLength, input);
116 if (stringLength == 0) {
117 printf("Unable to read %s\n", argv[1]);
118 delete [] scriptString;
119 fclose(input);
120 return readError;
121 }
122 scriptString[stringLength] = 0; // Terminate string with null
123 fclose(input);
124
125 PCB.pointer = scriptString;
126 sort();
127
128
129
130 return success;
131 }
132
133 }