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

1.1       paf         1: /*
1.6     ! paf         2:   $Id: parser.C,v 1.5 2001/02/11 11:27:26 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.4       paf        12: #include "pa_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)) {
1.4       paf        22:                Pool pool(fatal_exception);
1.2       paf        23:                
                     24:                char *file="file1";
1.5       paf        25:                String& string=*new(pool) String(pool);
1.2       paf        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";
1.5       paf        36:                Hash& hash=*new(pool) Hash(pool, false);
1.2       paf        37:                String key1=string;
                     38:                key1.APPEND("1", key1_file, 1);
1.5       paf        39:                String& value1=*new(pool) String(pool);
1.2       paf        40:                value1.APPEND("i'm value1\n", file, 1);
1.5       paf        41:                String& value2=*new(pool) String(pool);
1.2       paf        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:                
1.5       paf        51:                String& a=*new(pool) String(pool);      a.APPEND("fi", file, 1); a.APPEND("rst", file, 2);
                     52:                String& b=*new(pool) String(pool);      b.APPEND("fir", file, 1); b.APPEND("st", file, 2);
1.2       paf        53:                printf(a==b?"eq\n":"ne\n");
                     54:                
                     55:                
1.5       paf        56:                Array& array=*new(pool) Array(pool, 2);
1.2       paf        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:                
1.5       paf        65:                Array& a1=*new(pool) Array(pool);
1.2       paf        66:                a1+="first";
1.5       paf        67:                Array& a2=*new(pool) Array(pool);
1.2       paf        68:                a2+="second";
1.5       paf        69:                Array& asum=*new(pool) Array(pool);
1.2       paf        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.4       paf        76:                Pool request_pool(fatal_exception);
1.2       paf        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.5       paf        91:                                Array& row=*new(request.pool()) Array(request.pool(), 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:                        }
1.6     ! paf       123: 
        !           124: 
        !           125:                        String it(request.pool());
        !           126:                        it.APPEND("ab.cd[zzz]", 0,0);
        !           127:                        String_iterator si(it);
        !           128:                        /*si++;
        !           129:                        si++;
        !           130:                        si++;
        !           131:                        si++;
        !           132:                        si++;
        !           133:                        si++;
        !           134:                        */
        !           135:                        /*bool found=si.skip_to('.');
        !           136:                        si++;
        !           137:                        */
        !           138:                        si++;
        !           139:                        Char_types types;
        !           140:                        types.set(' ', 1);
        !           141:                        types.set('[', 2);
        !           142:                        types.set(']', 3);
        !           143:                        int type=si.skip_to(types);
        !           144:                        si++;
        !           145: 
        !           146: 
1.1       paf       147:                } else {
1.2       paf       148:                        Exception& e=request.exception();
1.3       paf       149:                        printf("operator_error occured: %s\n", e.comment());
1.2       paf       150:                        const String *type=e.type();
                    151:                        if(type) {
                    152:                                printf("  type: %s", type->cstr());
                    153:                                const String *code=e.code();
                    154:                                if(code)
                    155:                                        printf(", code: %s", code->cstr());
                    156:                                printf("\n");
                    157:                        }
                    158:                        const String *problem_source=e.problem_source();
1.1       paf       159:                        if(problem_source) {
                    160:                                const Origin& origin=problem_source->origin();
1.3       paf       161:                                printf("  '%s' [%s:%d]\n", 
1.1       paf       162:                                        problem_source->cstr(),
                    163:                                        origin.file, origin.line);
                    164:                        }
                    165:                }
                    166:        } else {
1.2       paf       167:                printf("fatal exception occured: %s\n", fatal_exception.comment());
1.1       paf       168:        }
                    169: 
                    170:        return 0;
                    171: }

E-mail: