Changeset 1924

Show
Ignore:
Timestamp:
04/03/07 23:30:21 (1 year ago)
Author:
karpet
Message:

global init/cleanup functions to help reduce duplication

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/Makefile.am

    r1913 r1924  
    2424                        times.c \ 
    2525                        words.c \ 
     26                        swish.c \ 
    2627                        $(myheaders)  
    2728 
  • libswish3/trunk/src/libswish3/config.c

    r1923 r1924  
    5858static void     add_multi_node_to_cv(xmlNode * node, swish_ConfigValue * cv); 
    5959 
    60 static int meta_abbrev_len = 625;   /* 26 * 26 - 1 */ 
    61 static char * meta_abbrev[] = { 
    62 "Aa", "Ab", "Ac", "Ad", "Ae", "Af", "Ag", "Ah", "Ai", "Aj", "Ak", "Al", "Am",  
    63 "An", "Ao", "Ap", "Aq", "Ar", "As", "At", "Au", "Av", "Aw", "Ax", "Ay", "Az",  
    64 "Ba", "Bb", "Bc", "Bd", "Be", "Bf", "Bg", "Bh", "Bi", "Bj", "Bk", "Bl", "Bm",  
    65 "Bn", "Bo", "Bp", "Bq", "Br", "Bs", "Bt", "Bu", "Bv", "Bw", "Bx", "By", "Bz",  
    66 "Ca", "Cb", "Cc", "Cd", "Ce", "Cf", "Cg", "Ch", "Ci", "Cj", "Ck", "Cl", "Cm",  
    67 "Cn", "Co", "Cp", "Cq", "Cr", "Cs", "Ct", "Cu", "Cv", "Cw", "Cx", "Cy", "Cz",  
    68 "Da", "Db", "Dc", "Dd", "De", "Df", "Dg", "Dh", "Di", "Dj", "Dk", "Dl", "Dm",  
    69 "Dn", "Do", "Dp", "Dq", "Dr", "Ds", "Dt", "Du", "Dv", "Dw", "Dx", "Dy", "Dz",  
    70 "Ea", "Eb", "Ec", "Ed", "Ee", "Ef", "Eg", "Eh", "Ei", "Ej", "Ek", "El", "Em",  
    71 "En", "Eo", "Ep", "Eq", "Er", "Es", "Et", "Eu", "Ev", "Ew", "Ex", "Ey", "Ez",  
    72 "Fa", "Fb", "Fc", "Fd", "Fe", "Ff", "Fg", "Fh", "Fi", "Fj", "Fk", "Fl", "Fm",  
    73 "Fn", "Fo", "Fp", "Fq", "Fr", "Fs", "Ft", "Fu", "Fv", "Fw", "Fx", "Fy", "Fz",  
    74 "Ga", "Gb", "Gc", "Gd", "Ge", "Gf", "Gg", "Gh", "Gi", "Gj", "Gk", "Gl", "Gm",  
    75 "Gn", "Go", "Gp", "Gq", "Gr", "Gs", "Gt", "Gu", "Gv", "Gw", "Gx", "Gy", "Gz",  
    76 "Ha", "Hb", "Hc", "Hd", "He", "Hf", "Hg", "Hh", "Hi", "Hj", "Hk", "Hl", "Hm",  
    77 "Hn", "Ho", "Hp", "Hq", "Hr", "Hs", "Ht", "Hu", "Hv", "Hw", "Hx", "Hy", "Hz",  
    78 "Ia", "Ib", "Ic", "Id", "Ie", "If", "Ig", "Ih", "Ii", "Ij", "Ik", "Il", "Im",  
    79 "In", "Io", "Ip", "Iq", "Ir", "Is", "It", "Iu", "Iv", "Iw", "Ix", "Iy", "Iz",  
    80 "Ja", "Jb", "Jc", "Jd", "Je", "Jf", "Jg", "Jh", "Ji", "Jj", "Jk", "Jl", "Jm",  
    81 "Jn", "Jo", "Jp", "Jq", "Jr", "Js", "Jt", "Ju", "Jv", "Jw", "Jx", "Jy", "Jz",  
    82 "Ka", "Kb", "Kc", "Kd", "Ke", "Kf", "Kg", "Kh", "Ki", "Kj", "Kk", "Kl", "Km",  
    83 "Kn", "Ko", "Kp", "Kq", "Kr", "Ks", "Kt", "Ku", "Kv", "Kw", "Kx", "Ky", "Kz",  
    84 "La", "Lb", "Lc", "Ld", "Le", "Lf", "Lg", "Lh", "Li", "Lj", "Lk", "Ll", "Lm",  
    85 "Ln", "Lo", "Lp", "Lq", "Lr", "Ls", "Lt", "Lu", "Lv", "Lw", "Lx", "Ly", "Lz",  
    86 "Ma", "Mb", "Mc", "Md", "Me", "Mf", "Mg", "Mh", "Mi", "Mj", "Mk", "Ml", "Mm",  
    87 "Mn", "Mo", "Mp", "Mq", "Mr", "Ms", "Mt", "Mu", "Mv", "Mw", "Mx", "My", "Mz",  
    88 "Na", "Nb", "Nc", "Nd", "Ne", "Nf", "Ng", "Nh", "Ni", "Nj", "Nk", "Nl", "Nm",  
    89 "Nn", "No", "Np", "Nq", "Nr", "Ns", "Nt", "Nu", "Nv", "Nw", "Nx", "Ny", "Nz",  
    90 "Oa", "Ob", "Oc", "Od", "Oe", "Of", "Og", "Oh", "Oi", "Oj", "Ok", "Ol", "Om",  
    91 "On", "Oo", "Op", "Oq", "Or", "Os", "Ot", "Ou", "Ov", "Ow", "Ox", "Oy", "Oz",  
    92 "Pa", "Pb", "Pc", "Pd", "Pe", "Pf", "Pg", "Ph", "Pi", "Pj", "Pk", "Pl", "Pm",  
    93 "Pn", "Po", "Pp", "Pq", "Pr", "Ps", "Pt", "Pu", "Pv", "Pw", "Px", "Py", "Pz",  
    94 "Qa", "Qb", "Qc", "Qd", "Qe", "Qf", "Qg", "Qh", "Qi", "Qj", "Qk", "Ql", "Qm",  
    95 "Qn", "Qo", "Qp", "Qq", "Qr", "Qs", "Qt", "Qu", "Qv", "Qw", "Qx", "Qy", "Qz",  
    96 "Ra", "Rb", "Rc", "Rd", "Re", "Rf", "Rg", "Rh", "Ri", "Rj", "Rk", "Rl", "Rm",  
    97 "Rn", "Ro", "Rp", "Rq", "Rr", "Rs", "Rt", "Ru", "Rv", "Rw", "Rx", "Ry", "Rz",  
    98 "Sa", "Sb", "Sc", "Sd", "Se", "Sf", "Sg", "Sh", "Si", "Sj", "Sk", "Sl", "Sm",  
    99 "Sn", "So", "Sp", "Sq", "Sr", "Ss", "St", "Su", "Sv", "Sw", "Sx", "Sy", "Sz",  
    100 "Ta", "Tb", "Tc", "Td", "Te", "Tf", "Tg", "Th", "Ti", "Tj", "Tk", "Tl", "Tm",  
    101 "Tn", "To", "Tp", "Tq", "Tr", "Ts", "Tt", "Tu", "Tv", "Tw", "Tx", "Ty", "Tz",  
    102 "Ua", "Ub", "Uc", "Ud", "Ue", "Uf", "Ug", "Uh", "Ui", "Uj", "Uk", "Ul", "Um",  
    103 "Un", "Uo", "Up", "Uq", "Ur", "Us", "Ut", "Uu", "Uv", "Uw", "Ux", "Uy", "Uz",  
    104 "Va", "Vb", "Vc", "Vd", "Ve", "Vf", "Vg", "Vh", "Vi", "Vj", "Vk", "Vl", "Vm",  
    105 "Vn", "Vo", "Vp", "Vq", "Vr", "Vs", "Vt", "Vu", "Vv", "Vw", "Vx", "Vy", "Vz",  
    106 "Wa", "Wb", "Wc", "Wd", "We", "Wf", "Wg", "Wh", "Wi", "Wj", "Wk", "Wl", "Wm",  
    107 "Wn", "Wo", "Wp", "Wq", "Wr", "Ws", "Wt", "Wu", "Wv", "Ww", "Wx", "Wy", "Wz",  
    108 "Xa", "Xb", "Xc", "Xd", "Xe", "Xf", "Xg", "Xh", "Xi", "Xj", "Xk", "Xl", "Xm",  
    109 "Xn", "Xo", "Xp", "Xq", "Xr", "Xs", "Xt", "Xu", "Xv", "Xw", "Xx", "Xy", "Xz",  
    110 "Ya", "Yb", "Yc", "Yd", "Ye", "Yf", "Yg", "Yh", "Yi", "Yj", "Yk", "Yl", "Ym",  
    111 "Yn", "Yo", "Yp", "Yq", "Yr", "Ys", "Yt", "Yu", "Yv", "Yw", "Yx", "Yy", "Yz",  
    112 "Za", "Zb", "Zc", "Zd", "Ze", "Zf", "Zg", "Zh", "Zi", "Zj", "Zk", "Zl", "Zm",  
    113 "Zn", "Zo", "Zp", "Zq", "Zr", "Zs", "Zt", "Zu", "Zv", "Zw", "Zx", "Zy", "Zz"  
    114 }; 
    115  
    11660 
    11761static void 
     
    160104 
    161105    swish_xfree(config); 
    162      
    163     swish_mem_debug(); 
    164 
    165  
    166  
    167  
    168 /* init memory stuff, get global debug and env vars, and verify locale is correct */ 
     106
     107 
     108 
     109 
     110/* init memory stuff, env vars, and verify locale is correct */ 
    169111 
    170112swish_Config  * 
     
    176118    xmlHashTablePtr c, metas, parsers, index, prop, alias, parsewords; 
    177119 
    178  
    179     /* init memory */ 
    180     swish_init_memory(); 
    181  
    182120    /* verify locale */ 
    183121    swish_verify_utf8_locale(); 
    184  
    185     /* set global vars */ 
    186     setenv("SWISH_DEBUG", "0", 0); 
    187     SWISH_DEBUG = strtol(getenv("SWISH_DEBUG"), (char **) NULL, 10); 
    188122 
    189123    if (SWISH_DEBUG) 
  • libswish3/trunk/src/libswish3/libswish3.h

    r1923 r1924  
    127127 
    128128 
     129/* global init and cleanup functions -- call these in every linking program */ 
     130void        swish_init(); 
     131void        swish_cleanup(); 
     132 
    129133 
    130134/* utils */ 
     
    351355 
    352356/* word functions */ 
     357void                swish_init_words(); 
    353358swish_WordList *    swish_init_WordList(); 
    354359void                swish_free_WordList(swish_WordList * list); 
     
    360365                                      int base_word_pos,  
    361366                                      int offset ); 
     367 
     368swish_WordList *    swish_tokenize_utf8_string(    
     369                                      xmlChar * str,  
     370                                      xmlChar * metaname,  
     371                                      xmlChar * context, 
     372                                      int maxwordlen,  
     373                                      int minwordlen,  
     374                                      int base_word_pos,  
     375                                      int offset ); 
     376 
     377swish_WordList *    swish_tokenize_ascii_string(    
     378                                      xmlChar * str,  
     379                                      xmlChar * metaname,  
     380                                      xmlChar * context, 
     381                                      int maxwordlen,  
     382                                      int minwordlen,  
     383                                      int base_word_pos,  
     384                                      int offset ); 
     385 
     386 
    362387size_t              swish_add_to_wordlist(  swish_WordList * list,  
    363388                                            xmlChar * word, 
  • libswish3/trunk/src/libswish3/mem.c

    r1923 r1924  
    3737{ 
    3838    memcount = 0; 
    39      
    40     if (SWISH_DEBUG) 
    41         return; 
    4239         
    43     /* init the global env vars, but don't override if already set */ 
    44     setenv("SWISH_DEBUG", "0", 0); 
    45     SWISH_DEBUG = strtol(getenv("SWISH_DEBUG"), (char**)NULL, 10); 
    4640} 
    4741 
  • libswish3/trunk/src/libswish3/parser.c

    r1921 r1924  
    5656 
    5757extern int      errno; 
    58  
    59 /* 
    60  * SWISH_DEBUG is set globally here in swish_parse_stdin() and/or 
    61  * swish_parse_file() we define it as extern in other .c files 
    62  */ 
    63  
    64 int             SWISH_DEBUG            = 0; 
     58extern int      SWISH_DEBUG; 
     59 
    6560int             SWISH_PARSER_ERROR     = 0; 
    6661int             SWISH_PARSER_WARNING   = 0; 
     
    158153swish_init_parser() 
    159154{ 
    160     xmlInitParser(); 
    161      
    162     /* global var that scripts can check to determine what version of Swish they are 
    163      * using. the second 0 indicates that it will not override it if already set */ 
    164     setenv("SWISH3", "1", 0); 
    165     setenv("SWISH_DEBUG", "0", 0); 
    166          
     155    xmlInitParser();         
    167156    xmlSubstituteEntitiesDefault(1);    /* resolve text entities */ 
     157    get_env_vars(); 
    168158} 
    169159 
     
    11961186{ 
    11971187    /* init the global env vars, but don't override if already set */ 
    1198     setenv("SWISH_DEBUG", "0", 0); 
    1199     SWISH_DEBUG = (int)strtol(getenv("SWISH_DEBUG"), (char**)NULL, 10); 
    12001188 
    12011189    setenv("SWISH_PARSER_ERROR", "0", 0); 
     
    12101198    if (SWISH_DEBUG) 
    12111199    { 
    1212         SWISH_PARSER_ERROR = 1
    1213         SWISH_PARSER_WARNING = 1
    1214         SWISH_PARSER_FATAL = 1
     1200        SWISH_PARSER_ERROR      = SWISH_DEBUG
     1201        SWISH_PARSER_WARNING    = SWISH_DEBUG
     1202        SWISH_PARSER_FATAL      = SWISH_DEBUG
    12151203    } 
    12161204} 
     
    12411229    min_headers = 2; 
    12421230 
    1243     get_env_vars(); 
    1244      
    12451231    ln = swish_xmalloc(SWISH_MAXSTRLEN + 1); 
    12461232    head_buf = xmlBufferCreateSize((SWISH_MAX_HEADERS * SWISH_MAXSTRLEN) + SWISH_MAX_HEADERS); 
     
    14021388 
    14031389 
    1404     get_env_vars(); 
    1405  
    14061390    head = buf_to_head(buf); 
    14071391 
     
    14611445    double          curTime = swish_time_elapsed(); 
    14621446    char           *etime; 
    1463  
    1464     get_env_vars(); 
    14651447 
    14661448    swish_ParseData *parse_data    = init_parse_data(config, user_data); 
  • libswish3/trunk/src/libswish3/words.c

    r1923 r1924  
    4040static int      is_ignore_word(wint_t c); 
    4141static int      bytes_in_char(wint_t c); 
    42 static void     set_debug(); 
    43  
    44 /**********************************************************************************************/ 
    45  
    46 /* we have our own set_debug here because we might be calling these tokenizing 
    47    functions without ever calling swish_init_config() 
    48  */ 
    49   
    50 static void set_debug() 
    51 
    52     if (SWISH_DEBUG) 
     42static void     make_ascii_tables(); 
     43 
     44 
     45static int initialized = 0; 
     46 
     47void 
     48swish_init_words() 
     49
     50    if (initialized) 
    5351        return; 
    5452         
    55     setenv("SWISH_DEBUG", "0", 0); 
    56     /* init the global env var, but don't override if already set */ 
    57     SWISH_DEBUG = strtol(getenv("SWISH_DEBUG"), (char**)NULL, 10); 
     53    make_ascii_tables(); 
     54    initialized = 1; 
    5855} 
    5956 
     
    233230 
    234231 
    235 static swish_WordList * 
    236 tokenize_utf8_string( 
     232swish_WordList * 
     233swish_tokenize_utf8_string( 
    237234               xmlChar * str, 
    238235               xmlChar * metaname, 
     
    416413*************************************************/ 
    417414 
    418 static int ascii_tables_created = 0; 
    419415static char ascii_word_table[128]; 
    420416static char ascii_start_table[128]; 
     
    443439             
    444440    } 
    445     ascii_tables_created = 1; 
    446441} 
    447442 
     
    458453 
    459454 
    460 static swish_WordList * 
    461 tokenize_ascii_string( 
     455swish_WordList * 
     456swish_tokenize_ascii_string( 
    462457               xmlChar * str, 
    463458               xmlChar * metaname, 
     
    481476    if (SWISH_DEBUG > 10) 
    482477        swish_debug_msg("parsing string: '%s' into words", str); 
    483  
    484      
    485     /* build tables if this is first time through */ 
    486     if (!ascii_tables_created) 
    487         make_ascii_tables(); 
    488478 
    489479 
     
    638628{ 
    639629 
    640     set_debug();    /* in case this is called without ever swish_init_config() */ 
     630    if (!initialized) 
     631    { 
     632        swish_warn_err("swish_init_words() was not explicitly called -- initializing...."); 
     633        swish_init_words(); 
     634    } 
    641635 
    642636    if (swish_is_ascii( str )) 
    643637    { 
    644638        //swish_debug_msg("%s is ascii", str); 
    645         return tokenize_ascii_string(   str,  
     639        return swish_tokenize_ascii_string(   str,  
    646640                                        metaname,  
    647641                                        context,  
     
    654648    { 
    655649        //swish_debug_msg("%s is utf8", str); 
    656         return tokenize_utf8_string(    str,  
     650        return swish_tokenize_utf8_string(    str,  
    657651                                        metaname,  
    658652                                        context,  
  • libswish3/trunk/src/swish_lint.c

    r1923 r1924  
    4242int             twords = 0; 
    4343 
     44extern int SWISH_DEBUG; 
     45 
    4446static struct option longopts[] = 
    4547{ 
     
    4749    {"debug",  required_argument, 0, 'd'}, 
    4850    {"help",   no_argument, 0, 'h'}, 
    49     {"version", no_argument, 0, 'v'}, 
    5051    {0, 0, 0, 0} 
    5152}; 
     
    5960void swish_version() 
    6061{ 
    61     printf("libswish3 version %s\n", SWISH_VERSION); 
     62    printf("libswish3 version %s\n", SWISH_LIB_VERSION); 
     63    printf("swish version %s\n", SWISH_VERSION); 
    6264} 
    6365 
     
    6870    char * descr = "swish_lint is an example program for using SwishParser\n"; 
    6971    printf("swish_lint [opts] [- | file(s)]\n"); 
    70     printf("opts:\n --config conf_file.xml\n --debug\n --help\n --version\n"); 
     72    printf("opts:\n --config conf_file.xml\n --debug [lvl]\n --help\n"); 
    7173    printf("\n%s\n", descr); 
    7274    libxml2_version(); 
     
    8587    twords += parse_data->docinfo->nwords; 
    8688 
    87     if (debug
     89    if (SWISH_DEBUG
    8890    { 
    8991        swish_debug_docinfo(parse_data->docinfo); 
     
    105107    double          startTime = swish_time_elapsed(); 
    106108     
    107     swish_init_parser(); 
    108  
    109     swish_Config * config = swish_init_config(); 
    110  
    111     /* setting this \after\ make_char_tables() causes weird error... 
    112      *  
    113      * xmlSubstituteEntitiesDefault(1); resolve text entities */ 
    114  
     109    xmlChar        *config_file = NULL; 
     110     
     111    swish_init(); 
     112 
     113    swish_Config * config; 
    115114 
    116115    while ((ch = getopt_long(argc, argv, "c:d:f:h", longopts, &option_index)) != -1) 
     
    135134 
    136135            //printf("optarg = %s\n", optarg); 
    137             config = swish_add_config((xmlChar *) optarg, config); 
     136            config_file = swish_xstrdup( optarg ); 
    138137            break; 
    139138             
    140         case 'v': 
    141             libxml2_version(); 
    142             swish_version(); 
    143             break; 
    144  
    145139 
    146140        case 'd': 
     
    150144                err(1, "-d option requires a positive integer as argument\n"); 
    151145 
    152             setenv("SWISH_DEBUG", optarg, 1); 
    153             debug = (int) strtol(getenv("SWISH_DEBUG"), (char **) NULL, 10); 
    154             /* printf("debug at level %d\n", debug); */ 
    155  
     146            SWISH_DEBUG = (int) strtol(optarg, (char **) NULL, 10); 
    156147            break; 
    157148 
     
    169160 
    170161    } 
     162     
     163    config = swish_init_config(); 
     164 
     165    if (config_file != NULL) 
     166    { 
     167        config = swish_add_config(config_file, config); 
     168    } 
    171169 
    172170    i = optind; 
     
    180178    } 
    181179     
    182     if (debug == 20) 
     180    if (SWISH_DEBUG == 20) 
    183181    { 
    184182        swish_debug_config(config);    
     
    219217     
    220218    swish_free_config( config ); 
    221     swish_free_parser(); 
    222     swish_mem_debug(); 
     219 
     220    if (config_file != NULL) 
     221        swish_xfree(config_file); 
     222     
     223    swish_cleanup();     
    223224 
    224225    return (0); 
  • libswish3/trunk/src/swish_words.c

    r1921 r1924  
    7070    int             max = 255; 
    7171    int             min = 1; 
    72  
     72     
    7373    swish_WordList *list; 
    7474 
     
    122122    } 
    123123 
     124    swish_init();   /* call after we have set optional debug flag */ 
     125 
    124126    i = optind; 
    125127         
     
    133135 
    134136    } 
     137     
     138    swish_cleanup(); 
    135139 
    136140    return (0);