ddltable.c

来自「db.* (pronounced dee-be star) is an adva」· C语言 代码 · 共 2,236 行 · 第 1/5 页

C
2,236 行
字号
    if ((dfile = vtfopen(ddlp_g.ddlfile, ddlp_g.u_flag ? DB_TEXT("rb") : DB_TEXT("r"))) == NULL)    {        vstprintf(ddlp_g.msg,            DB_TEXT("Error (errno=%d): unable to open database ddlp_g.schema file\n    '%s'"),            errno, ddlp_g.ddlfile);        ddlp_abort(ddlp_g.msg);        return;    }    while (! ok && (ch != DB_TEOF)) /* in case we can't find 'database' word */    {        ch = vgettc(dfile);        if (vistalpha(ch) && start_of_line)        {            tempbuff[i++] = (DB_TCHAR) ch;            if (i == 8)            {                if (vtstricmp(tempbuff, key) == 0)                {                    ok = 1;                    continue;                }                for (j = 0; j < i; j++)                    vputtc((DB_TINT) tempbuff[j], ddlp_g.ifile);                i = 0;                start_of_line = FALSE;            }        }        else        {            for (j = 0; j < i; j++)                vputtc((DB_TINT) tempbuff[j], ddlp_g.ifile);            if (!vistspace(ch))                start_of_line = FALSE;            i = 0;            vputtc(ch, ddlp_g.ifile);            if (ch == (DB_TINT) DB_TEXT('/'))            {                ch = vgettc(dfile);                if (ch == (DB_TINT) DB_TEXT('*'))                {                    vputtc(ch, ddlp_g.ifile);                    do                    {                        while ((ch = vgettc(dfile)) != (DB_TINT) DB_TEXT('*'))                            vputtc(ch, ddlp_g.ifile);                        vputtc(ch, ddlp_g.ifile);                        if ((ch = vgettc(dfile)) != (DB_TINT) DB_TEXT('/'))                            vungettc(ch, dfile);                    } while (ch != (DB_TINT) DB_TEXT('/'));                    vputtc(ch, ddlp_g.ifile);                }                else if (ch == (DB_TINT) DB_TEXT('/'))                {                    vputtc(ch, ddlp_g.ifile);                    while ((ch = vgettc(dfile)) != (DB_TINT) DB_TEXT('\n'))                        vputtc(ch, ddlp_g.ifile);                    vputtc(ch, ddlp_g.ifile);                    start_of_line = TRUE;                }            }            else            {                if (ch == (DB_TINT) DB_TEXT('\n'))                    start_of_line = TRUE;            }        }    }    /* If we can't find 'database' in ddlp_g.schema file */    if (ch == DB_TEOF && ! ok)    {        vstprintf(ddlp_g.msg,            DB_TEXT("Error: unable to find 'database' keyword in ddlp_g.schema file\n'%s'"),            ddlp_g.ddlfile);        ddlp_abort(ddlp_g.msg);        return;    }    /* close the input file */    fclose(dfile);}static unsigned short htodbs(unsigned short value){#ifndef BYTE_ORDER    unsigned short x;    int host;    x = 0x01234u;    host = *((unsigned char*)&x) == 0x34 ? LITTLE_ENDIAN : BIG_ENDIAN;#else    int host = BYTE_ORDER;#endif    if (ddlp_g.byte_order == HOST_ENDIAN || host == ddlp_g.byte_order)        return value;    // reverse byte order    return (value >> 8 & 0x00FFu) | (value << 8 & 0xFF00u);}static unsigned long htodbl(unsigned long value){#ifndef BYTE_ORDER    unsigned short x;    int host;    x = 0x01234u;    host = *((unsigned char*)&x) == 0x34 ? LITTLE_ENDIAN : BIG_ENDIAN;#else    int host = BYTE_ORDER;#endif    if (ddlp_g.byte_order == HOST_ENDIAN || host == ddlp_g.byte_order)        return value;    // reverse byte order    return (value >> 24 & 0x000000FFu)        |  (value >>  8 & 0x0000FF00u)        |  (value <<  8 & 0x00FF0000u)        |  (value << 24 & 0xFF000000u);}static int write_short(PSP_FH file, unsigned short value){    unsigned short buf = htodbs(value);    return psp_fileWrite(file, &buf, sizeof(short));}static int write_long(PSP_FH file, unsigned long value){    unsigned long buf = htodbl(value);    return psp_fileWrite(file, &buf, sizeof(unsigned long));}/* create record struct declarations */void pr_structs (void){    int i, n;    struct record_info *r;    struct field_info *d;    for (i = 0, d = ddlp_g.field_list, r = ddlp_g.rec_list; r; r = r->next_rec, ++i)    {        /* generate output only if not system record, and fields are present */        if (vtstricmp(r->rec_name, DB_TEXT("system")) == 0 || r->rt.rt_fdtot <= 0)            continue;        vftprintf(ddlp_g.ifile, DB_TEXT("struct %s {\n"), r->rec_name);        while (d && d->fd.fd_rec == i)        {            if (d->fd.fd_type != 'k')                d = pr_decl(d);            d = d->next_field;        }        if (ddlp_g.cur_struct)        {            vftprintf(ddlp_g.ifile, DB_TEXT("   } %s"), ddlp_g.cur_struct->fld_name);            for (n = 0; ((n < MAXDIMS) && (ddlp_g.cur_struct->fd.fd_dim[n])); ++n)            {                if (ddlp_g.cur_struct->dim_const[n])                    vftprintf(ddlp_g.ifile, DB_TEXT("[%s]"), ddlp_g.cur_struct->dim_const[n]->const_name);                else                    vftprintf(ddlp_g.ifile, DB_TEXT("[%d]"), ddlp_g.cur_struct->fd.fd_dim[n]);            }            vftprintf(ddlp_g.ifile, DB_TEXT(";\n"));            ddlp_g.cur_struct = NULL;        }        vftprintf(ddlp_g.ifile, DB_TEXT("};\n\n"));    }}/* create compound key struct declarations */static void pr_compound_keys (void){    int i, j;    struct record_info *r;    struct field_info *d, *cf;    struct ddlkey_info *k;    for (i = 0, d = ddlp_g.field_list, k = ddlp_g.key_list; d; d = d->next_field, ++i)    {        if (d->fd.fd_type != 'k')            continue;        if (!ddlp_g.d_flag)        {            vftprintf(ddlp_g.ifile, DB_TEXT("struct %s {\n"), d->fld_name);        }        else        {            for (j = 0, r = ddlp_g.rec_list; j < d->fd.fd_rec; r = r->next_rec, ++j)                ;            vftprintf(ddlp_g.ifile, DB_TEXT("struct %s_%s {\n"), r->rec_name, d->fld_name);        }        while (k && k->kt.kt_key == i)        {            int nFld;            /* find field entry */            for ( nFld = 0, cf = ddlp_g.field_list;                     nFld != k->kt.kt_field;                    cf = cf->next_field, ++nFld)            {                ;            }            pr_decl(cf);            k = k->next_key;        }        vftprintf(ddlp_g.ifile, DB_TEXT("};\n\n"));    }}void write_dm_header (void){    DB_TCHAR temp[FILENMLEN];    int      j;    /* construct include file name */    if (ddlp_g.ifn[0] == 0) {        vtstrncpy(ddlp_g.ifn, ddlp_g.db_name, FILENMLEN - 4);        ddlp_g.ifn[FILENMLEN - 4] = 0;        vtstrcat(ddlp_g.ifn, DB_TEXT(".h"));    }    /* open database include file */    if ((ddlp_g.ifile = vtfopen(ddlp_g.ifn, ddlp_g.u_flag ? DB_TEXT("wb") : DB_TEXT("w"))) == NULL)    {        vstprintf(ddlp_g.msg, DB_TEXT("Error (errno=%d): unable to create include file '%s'"),            errno, ddlp_g.ifn);        ddlp_abort(ddlp_g.msg);        return;    }    vtstrupr(vtstrcpy(temp, ddlp_g.ifn));    /* make it valid C identifier [a-zA-Z_][a-zA-Z0-9_]*/    if (temp[0] && temp[0] != '_' && !isalpha(temp[0]))        temp[0] = '_';    for(j = 1; temp[j]; j++) {        if (temp[j] != '_' && !isalnum(temp[j]))        temp[j] = '_';    }    vftprintf(ddlp_g.ifile, DB_TEXT("#ifndef %s\n#define %s\n\n"), temp, temp);    vftprintf(ddlp_g.ifile, DB_TEXT("/* db.* %s */\n\n"), DBSTAR_VERSION);    write_header();    if (ddlp_g.abort_flag)        return;    vftprintf(ddlp_g.ifile,        DB_TEXT("\n/* database %s record/key structure declarations */\n\n"), ddlp_g.db_name);    /* create record struct declarations */    pr_structs();    /* create compound key struct declarations */    pr_compound_keys();    /* print constants */    pr_constants();    /* external schema definition */    if (ddlp_g.c_flag)        vftprintf(ddlp_g.ifile, "\nextern DB_SCHEMA %s_dbd;\n", ddlp_g.db_name);    vftprintf(ddlp_g.ifile, DB_TEXT("\n#endif    /* %s */\n"), temp);    fclose(ddlp_g.ifile);}static int detect_dbd_ver(){    struct file_info *f;    int v = 300;    /* determine format (v3.01 if new keywords used, otherwise v3.00) */    /* if any filenames are longer than 47 chars, use db.* 5.0 format  */    for (f = ddlp_g.file_list; f; f = f->next_file)    {        if (vtstrlen(f->ft.ft_name) > V3_FILENMLEN - 1)        {            v = 500;            break;        }        if (f->ft.ft_initial + f->ft.ft_next + f->ft.ft_pctincrease)            v = 301;    }#if defined(UNICODE)    if (ddlp_g.u_flag)        v = -500;#endif    return v;}/* Write out database tables*/void write_tables (void){    struct file_info *f;    struct record_info *r;    struct field_info *d;    struct set_info *s;    struct member_info *m;    struct sort_info *o;    struct ddlkey_info *k;    DB_TCHAR tobjname[NAMELEN + 1];  /* add one to handle '\n' */    PSP_FH tfile;                   /* table file descr */    DB_TCHAR tfn[FILENMLEN];         /* table file name */    int v = detect_dbd_ver();    char *ver;#if defined(UNICODE)    char cobjname[NAMELEN + 1];#endif    if (!ddlp_g.db_name[0])        /* Parsing error! But UNIX open() will create ".dbd" successfully, */        /* so we need to stop here before open_x() is called */    {        vstprintf(ddlp_g.msg, DB_TEXT("Parsing Error: unable to create dictionary file '%s'"),            tfn);        ddlp_abort(ddlp_g.msg);        return;    }    /* generate table file name */    vtstrncpy(tfn, ddlp_g.db_name, FILENMLEN - 6);    vtstrcat(tfn, DB_TEXT(".dbd"));    /* unlink it if it exists */    psp_fileRemove(tfn);    /* open output file */    tfile = psp_fileOpen(tfn, O_RDWR | O_CREAT, 0);    if (!tfile)    {        vstprintf(ddlp_g.msg, DB_TEXT("Error (errno=%d): unable to create dictionary file '%s'"),            psp_errno(), tfn);        ddlp_abort(ddlp_g.msg);        return;    }    switch(v) {    case  300: ver = dbd_VERSION_300; break;    case  301: ver = dbd_VERSION_301; break;    case  500: ver = dbd_VERSION_500; break;#if defined(UNICODE)    case -500: ver = dbd_VERSION_u500; break;#endif    default:        vstprintf(ddlp_g.msg, DB_TEXT("internal error: unsupported DBD version '%d'"), v);        ddlp_abort(ddlp_g.msg);        return;    }    /* output dictionary version */    psp_fileWrite(tfile, ver, DBD_COMPAT_LEN);    /* output page size */    write_short(tfile, max_page_size);    /* output file sizes */    write_short(tfile, ddlp_g.tot_files);    write_short(tfile, ddlp_g.tot_records);    write_short(tfile, ddlp_g.tot_fields);    write_short(tfile, ddlp_g.tot_sets);    write_short(tfile, ddlp_g.tot_members);    write_short(tfile, ddlp_g.tot_sort_fields);    write_short(tfile, ddlp_g.tot_comkeyflds);    /* output file table */    switch (v)    {        case 301:        {            V301_FILE_ENTRY fe;            for (f = ddlp_g.file_list; f; f = f->next_file)            {                memset(&fe, 0, sizeof(V301_FILE_ENTRY));                ttoa(f->ft.ft_name, fe.v3_ft_name, V3_FILENMLEN);                fe.v3_ft_desc    = 0;                fe.v3_ft_status  = 'c';                fe.v3_ft_type    = f->ft.ft_type;                fe.v3_ft_slots   = htodbs(f->ft.ft_slots);                fe.v3_ft_slsize  = htodbs(f->ft.ft_slsize);                fe.v3_ft_pgsize  = htodbs(f->ft.ft_pgsize);                fe.v3_ft_flags   = htodbs(f->ft.ft_flags);                fe.v3_ft_pctincrease = htodbs(f->ft.ft_pctincrease);                fe.v3_ft_initial = htodbl(f->ft.ft_initial);                fe.v3_ft_next    = htodbl(f->ft.ft_next);                psp_fileWrite(tfile, &fe, sizeof(V301_FILE_ENTRY));            }            break;        }        case 500:        {            V500_FILE_ENTRY fe;            for (f = ddlp_g.file_list; f; f = f->next_file)            {                memset(&fe, 0, sizeof(V500_FILE_ENTRY));                ttoa(f->ft.ft_name, fe.v5_ft_name, FILENMLEN);                fe.v5_ft_desc    = 0;                fe.v5_ft_status  = 'c';                fe.v5_ft_type    = f->ft.ft_type;                fe.v5_ft_slots   = htodbs(f->ft.ft_slots);                fe.v5_ft_slsize  = htodbs(f->ft.ft_slsize);                fe.v5_ft_pgsize  = htodbs(f->ft.ft_pgsize);                fe.v5_ft_flags   = htodbs(f->ft.ft_flags);                fe.v5_ft_pctincrease = htodbs(f->ft.ft_pctincrease);                fe.v5_ft_initial = htodbl(f->ft.ft_initial);                fe.v5_ft_next    = htodbl(f->ft.ft_next);                psp_fileWrite(tfile, &fe, sizeof(V500_FILE_ENTRY));            }            break;        }        case -500:            for (f = ddlp_g.file_list; f; f = f->next_file) {                /*                ** here FILE_ENTRY structure was written. I suppose it is a                ** kind of mistake, and U500_FILE_ENTRY should be used instead.                ** More over inittab() reads U500_FILE_ENTRY, so despite                ** it makes dbd files different I fix the bug.                */#if 1                U500_FILE_ENTRY fe;                memset(&fe, 0, sizeof(U500_FILE_ENTRY));                ttow(f->ft.ft_name, fe.v5_ft_name, FILENMLEN);                fe.v5_ft_desc = 0;                fe.v5_ft_index = 0;                fe.v5_ft_status = 'c';                fe.v5_ft_type = f->ft.ft_type;                fe.v5_ft_slots = htodbs(f->ft.ft_slots);                fe.v5_ft_slsize  = htodbs(f->ft.ft_slsize);                fe.v5_ft_pgsize  = htodbs(f->ft.ft_pgsize);                fe.v5_ft_flags   = htodbs(f->ft.ft_flags);                fe.v5_ft_pctincrease = htodbs(f->ft.ft_pctincrease);                fe.v5_ft_initial = htodbl(f->ft.ft_initial);                fe.v5_ft_next    = htodbl(f->ft.ft_next);                psp_fileWrite(tfile, &fe, sizeof(U500_FILE_ENTRY));#else /* compatible version */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?