Annotation of parser3/src/targets/parser/parser.C, revision 1.2
1.1 paf 1: /*
1.2 ! paf 2: $Id: parser.C,v 1.1 2001/01/30 11:52:22 paf Exp $
1.1 paf 3: */
4:
5: #include <stdio.h>
6: #include <stdlib.h>
7:
8: #include "pa_hash.h"
9: #include "pa_array.h"
10: #include "pa_table.h"
11: #include "pa_common.h"
1.2 ! paf 12: #include "pool.h"
1.1 paf 13:
14: char *itoa(int n, char *buf){
15: snprintf(buf,MAX_STRING,"%d",n);
16: return buf;
17: }
18:
19: int main(int argc, char *argv[]) {
1.2 ! paf 20: Exception fatal_exception;
! 21: if(EXCEPTION_TRY(fatal_exception)) {
! 22: parser_Pool pool(fatal_exception);
! 23:
! 24: char *file="file1";
! 25: String& string=pool.make_string();
! 26: string.APPEND("Hello, ", file, 1);
! 27: string.APPEND("w", file, 2);
! 28: string.APPEND("o", file, 3);
! 29: string.APPEND("r", file, 4);
! 30: string.APPEND("l", file, 5);
! 31: string.APPEND("d", file, 6);
! 32: string.APPEND("!\n ", file, 7);
! 33: printf(string.cstr());
! 34:
! 35: char *key1_file="key1_file";
! 36: Hash& hash=pool.make_hash();
! 37: String key1=string;
! 38: key1.APPEND("1", key1_file, 1);
! 39: String& value1=pool.make_string();
! 40: value1.APPEND("i'm value1\n", file, 1);
! 41: String& value2=pool.make_string();
! 42: value2.APPEND("i'm value2\n", file, 1);
! 43: hash.put(key1, &value1);
! 44: char *key2_file="key2_file";
! 45: String key2=string;
! 46: key2.APPEND("2", key2_file, 1);
! 47: hash.put(key2, &value2);
! 48: String *found_value=(String*)hash.get(key2);
! 49: printf(found_value?found_value->cstr():"not found\n");
! 50:
! 51: String& a=pool.make_string(); a.APPEND("fi", file, 1); a.APPEND("rst", file, 2);
! 52: String& b=pool.make_string(); b.APPEND("fir", file, 1); b.APPEND("st", file, 2);
! 53: printf(a==b?"eq\n":"ne\n");
! 54:
! 55:
! 56: Array& array=pool.make_array(2);
! 57: array+="first";
! 58: array+="second";
! 59: array+="third";
! 60: printf("%s-%s-%s\n",
1.1 paf 61: array.get_cstr(0),
62: array.get_cstr(1),
63: array.get_cstr(2));
1.2 ! paf 64:
! 65: Array& a1=pool.make_array();
! 66: a1+="first";
! 67: Array& a2=pool.make_array();
! 68: a2+="second";
! 69: Array& asum=pool.make_array();
! 70: asum.append_array(a1);
! 71: asum.append_array(a2);
! 72: printf("%s-%s\n",
1.1 paf 73: asum.get_cstr(0),
74: asum.get_cstr(1));
75:
1.2 ! paf 76: parser_Pool request_pool(fatal_exception);
! 77: Request request(request_pool);
! 78:
! 79: Exception operator_exception;
! 80: Local_request_exception subst(request, operator_exception);
! 81: if(EXCEPTION_TRY(request.exception())) {
! 82: Array acolumns(request.pool());
1.1 paf 83: acolumns+="id";
84: acolumns+="name";
85: acolumns+="age";
86: Table named_table(request, "_file.cfg", 1, &acolumns);
87: for(int n=1; n<=5; n++) {
1.2 ! paf 88: Array& row=request.pool().make_array(named_table.columns()->size());
! 89: char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.1 paf 90: row+=itoa(n, buf);
91: row+="paf";
92: row+="99";
93:
94: named_table+=&row;
95: }
96:
97: for(int i=0; i<named_table.columns()->size(); i++)
98: printf("%s\t", named_table.columns()->get_cstr(i));
99: printf("\n");
100: for(named_table.set_current(0); named_table.get_current()<named_table.size(); named_table.inc_current()) {
1.2 ! paf 101: String line(request.pool());
1.1 paf 102: for(int i=0; i<named_table.columns()->size(); i++) {
1.2 ! paf 103: //String name(request.pool());
! 104: //char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.1 paf 105: //name.APPEND(itoa(i, buf), "names file", 0);
106: //name.APPEND("id", "names file", 0);
107: //named_table.read_item(line, name);
108: const char *cstr_name=named_table.columns()->get_cstr(i);
1.2 ! paf 109: String name(request.pool());
1.1 paf 110: name.APPEND(cstr_name, 0, 0);
111: named_table.read_item(line, name);
112: line.APPEND("\t", 0, 0);
113: }
114: printf("%s\n", line.cstr());
115: }
116: } else {
117: printf("operator_error occured: \n");
1.2 ! paf 118: Exception& e=request.exception();
! 119: const String *type=e.type();
! 120: if(type) {
! 121: printf(" type: %s", type->cstr());
! 122: const String *code=e.code();
! 123: if(code)
! 124: printf(", code: %s", code->cstr());
! 125: printf("\n");
! 126: }
! 127: const String *problem_source=e.problem_source();
1.1 paf 128: if(problem_source) {
129: const Origin& origin=problem_source->origin();
130: printf(" origin: '%s', file '%s', line %d\n",
131: problem_source->cstr(),
132: origin.file, origin.line);
133: }
1.2 ! paf 134: printf(" comment: %s\n", e.comment());
1.1 paf 135: }
136: } else {
1.2 ! paf 137: printf("fatal exception occured: %s\n", fatal_exception.comment());
1.1 paf 138: }
139:
140: return 0;
141: }
E-mail: