Annotation of parser3/src/targets/parser/parser.C, revision 1.3

1.1       paf         1: /*
1.3     ! paf         2:   $Id: parser.C,v 1.2 2001/01/30 13:07:31 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())) {
1.3     ! paf        82:                        /*
1.2       paf        83:                        Array acolumns(request.pool());
1.1       paf        84:                        acolumns+="id";
                     85:                        acolumns+="name";
                     86:                        acolumns+="age";
1.3     ! paf        87:                        Table table(request, "_file.cfg", 1, &acolumns);
        !            88:                        */
        !            89:                        Table table(request, "_file.cfg", 1, 0);
1.1       paf        90:                        for(int n=1; n<=5; n++) {
1.3     ! paf        91:                                Array& row=request.pool().make_array(3/*table.columns()->size()*/);
1.2       paf        92:                                char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
1.1       paf        93:                                row+=itoa(n, buf);
                     94:                                row+="paf";
                     95:                                row+="99";
                     96:                                
1.3     ! paf        97:                                table+=&row;
1.1       paf        98:                        }
1.3     ! paf        99:                        /*
        !           100:                        for(int i=0; i<table.columns()->size(); i++) 
        !           101:                                printf("%s\t", table.columns()->get_cstr(i));
1.1       paf       102:                        printf("\n");
1.3     ! paf       103:                        */
        !           104:                        for(table.set_current(0); table.get_current()<table.size(); table.inc_current()) {
1.2       paf       105:                                String line(request.pool());
1.3     ! paf       106:                                for(int i=0; i<5/*table.columns()->size()*/; i++) {
        !           107:                                        /**/
        !           108:                                        String name(request.pool());
        !           109:                                        char *buf=static_cast<char *>(request.pool().malloc(MAX_STRING));
        !           110:                                        name.APPEND(itoa(i, buf), "names file", 0);
1.1       paf       111:                                        //name.APPEND("id", "names file", 0);
1.3     ! paf       112:                                        table.read_item(line, name);
        !           113:                                        /*
        !           114:                                        const char *cstr_name=table.columns()->get_cstr(i);
1.2       paf       115:                                        String name(request.pool());
1.1       paf       116:                                        name.APPEND(cstr_name, 0, 0);
1.3     ! paf       117:                                        table.read_item(line, name);
        !           118:                                        /**/
1.1       paf       119:                                        line.APPEND("\t", 0, 0);
                    120:                                }
                    121:                                printf("%s\n", line.cstr());
                    122:                        }
                    123:                } else {
1.2       paf       124:                        Exception& e=request.exception();
1.3     ! paf       125:                        printf("operator_error occured: %s\n", e.comment());
1.2       paf       126:                        const String *type=e.type();
                    127:                        if(type) {
                    128:                                printf("  type: %s", type->cstr());
                    129:                                const String *code=e.code();
                    130:                                if(code)
                    131:                                        printf(", code: %s", code->cstr());
                    132:                                printf("\n");
                    133:                        }
                    134:                        const String *problem_source=e.problem_source();
1.1       paf       135:                        if(problem_source) {
                    136:                                const Origin& origin=problem_source->origin();
1.3     ! paf       137:                                printf("  '%s' [%s:%d]\n", 
1.1       paf       138:                                        problem_source->cstr(),
                    139:                                        origin.file, origin.line);
                    140:                        }
                    141:                }
                    142:        } else {
1.2       paf       143:                printf("fatal exception occured: %s\n", fatal_exception.comment());
1.1       paf       144:        }
                    145: 
                    146:        return 0;
                    147: }

E-mail: