dbe_edit.c

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

C
531
字号
/* Edit database address stored in record ***********************************/int edit_dba(char *ptr, int check, DB_TASK *task){    DB_TCHAR    dba_str[20], prompt_str[60];    DB_ADDR     dba;    short       fno, c_fno;    DB_ULONG    slot;    /* Display old value */    memcpy(&dba, ptr, DB_ADDR_SIZE);    d_decode_dba(dba, &fno, &slot);    vstprintf(dba_str, decimal ? DB_TEXT("[%d:%lu]") : DB_TEXT("[%x:%lx]"),              fno, slot);    vtstrcpy(prompt_str, dbe_getstr(M_EDITCV));    vtstrcat(prompt_str, dba_str);    vtstrcat(prompt_str, DB_TEXT("\n"));    dbe_out(prompt_str, STDOUT);    /* Get new value */    dbe_getline(dbe_getstr(M_EDITNV), prompt_str, sizeof(prompt_str) / sizeof(DB_TCHAR));    gettoken(prompt_str, dba_str, sizeof(dba_str) / sizeof(DB_TCHAR));    /* Nothing entered - don't change current value */    if (!(dba_str[0]))        return (0);    if (getdba(dba_str, &dba) < 0)        return (BAD_DBA);    /* Check that it's a valid address (not for owner / member pointers) */    if (check)    {        d_decode_dba(dba, &fno, &slot);        d_decode_dba(task->curr_rec, &c_fno, &slot);        /* Zero always valid file number - for delete chain */        if ((fno != 0) && (fno != c_fno))            return (BAD_FILE);    }    memcpy(ptr, &dba, DB_ADDR_SIZE);    return (0);}/* Edit stored optional keys*/int edit_opt(char *ptr, DB_TASK *task){    register int   i, fld;    short          rec;    long           ln;    int            n, first, last, count, len, bytes, offset, error;    DB_TCHAR       hex_line[40], ascii_line[40], line[80], *pt;    char          *p;    error = 0;    ascii_line[0] = hex_line[0] = 0;    /* Get record type */    if ((rec = getrtype(slot.buffer, task)) == -1)    {        return (error = BAD_TYPE);    }    /* Count optional keys */    first = task->record_table[rec].rt_fields;    last = first + task->record_table[rec].rt_fdtot;    for (count = 0, fld = first; fld < last; fld++)    {        if (task->field_table[fld].fd_flags & OPTKEYMASK)            count++;    }    if (count == 0)        return (ERR_NOPT);    /* Display current values, bit map in hex_line, names in ascii_line */    offset = task->record_table[rec].rt_flags & TIMESTAMPED ?        RECHDRSIZE + 2 * sizeof(DB_ULONG) : RECHDRSIZE;    p = ptr + offset;    len = bytes = (count + BITS_PER_BYTE - 1) / BITS_PER_BYTE;    for (fld = first; fld < last || len > 0; fld++)    {        if (fld < last)        {            /* Each name on a new line, followed by stored / not stored */            if (!(task->field_table[fld].fd_flags & OPTKEYMASK))                continue;            vtstrcpy(ascii_line, task->field_names[fld]);            vtstrcat(ascii_line,                     dbe_getstr(testopt(fld, ptr, task) ? M_FLAGKS : M_FLAGKN));        }        else        {            ascii_line[0] = 0;        }        if (len > 0)        {            n = len > 8 ? 8 : len;            do_hexstr(p, hex_line, n);            p += n;            len -= n;        }        else        {            hex_line[0] = 0;        }        vstprintf(line, DB_TEXT("%-30.30s  %s\n"), hex_line, ascii_line);        dbe_out(line, STDOUT);    }    /* Get new values */    dbe_getline(dbe_getstr(M_EDITNV), line, sizeof(line) / sizeof(DB_TCHAR));    ptr += offset;    pt = gettoken(line, hex_line, sizeof(hex_line) / sizeof(DB_TCHAR));    /* Assume new value entered as a bit mask */    for (i = 0; i < bytes && hex_line[0]; i++)    {        if ((ln = gethex(hex_line)) < 0)        {            error = BAD_HEX;            break;        }        pt = gettoken(pt, hex_line, sizeof(hex_line) / sizeof(DB_TCHAR));    }    /* It is a valid bit mask - can update current record */    if (error >= 0)    {        pt = gettoken(line, hex_line, sizeof(hex_line) / sizeof(DB_TCHAR));        for (i = 0; i < bytes && hex_line[0]; i++)        {            ln = gethex(hex_line);            ptr[i] = (char) (0xFF & ln);            pt = gettoken(pt, hex_line, sizeof(hex_line) / sizeof(DB_TCHAR));        }    }    else    {        /* It isn't a bit mask - is it a comma-separated list of names ? */        pt = gettoken(line, ascii_line, sizeof(ascii_line) / sizeof(DB_TCHAR));        while (ascii_line[0])        {            if ((fld = getfld(ascii_line, &n, task)) < 0)                return (error);            pt = gettoken(pt, ascii_line, sizeof(ascii_line) / sizeof(DB_TCHAR));        }        /* Valid comma-separated list - update current record */        for (i = 0; i < bytes; i++)            ptr[i] = 0;        pt = gettoken(line, ascii_line, sizeof(ascii_line) / sizeof(DB_TCHAR));        while (ascii_line[0])        {            if ((fld = getfld(ascii_line, &n, task)) < 0)                return (error);            n = (((task->field_table[fld].fd_flags & OPTKEYMASK)                    >> OPTKEYSHIFT) & OPTKEYNDX) - 1;            if (n >= 0)            {                ptr[n / BITS_PER_BYTE] |=                    1 << (BITS_PER_BYTE - n % BITS_PER_BYTE - 1);            }            pt = gettoken(pt, ascii_line, sizeof(ascii_line) / sizeof(DB_TCHAR));        }    }    return (0);}/* Edit a number (e.g. membership count)*/int edit_num(char *ptr){    long  num;    long  old, new;    int   error;    /* Don't want to update current record till input has been validated */    memcpy(&num, ptr, sizeof(long));    old = num;    if ((error = edit_long(old, &new)) < 0)        return (error);    num = new;    memcpy(ptr, &num, sizeof(long));    return (0);}/* Edit a number - may put rubbish in new*/int edit_long(long old, long *new){    DB_TCHAR num_str[20], prompt_str[60];    /* Display old value */    vstprintf(num_str, decimal ? DB_TEXT("%ld") : DB_TEXT("%lx"), (long) old);    vtstrcpy(prompt_str, dbe_getstr(M_EDITCV));    vtstrcat(prompt_str, num_str);    vtstrcat(prompt_str, DB_TEXT("\n"));    dbe_out(prompt_str, STDOUT);    /* Get new value */    dbe_getline(dbe_getstr(M_EDITNV), prompt_str,        sizeof(prompt_str) / sizeof(DB_TCHAR));    gettoken(prompt_str, num_str, sizeof(num_str) / sizeof(DB_TCHAR));    /* Anything entered ? */    if (!(num_str[0]))    {        *new = old;    }    else if (decimal)       /* Base 10 or 16 ? */    {        if ((*new = getlong(num_str)) < 0L)            return (BAD_NUM);    }    else    {        if ((*new = gethex(num_str)) < 0L)            return (BAD_HEX);    }    return (0);}/* Edit delete chain pointer, current file*/int edit_dchain(DB_TASK *task){    int      error;    F_ADDR   dchain;    long     old, new;    if ((error = read_dchain(&dchain, task)) < 0)        return (error);    old = dchain;    if ((error = edit_long(old, &new)) < 0)        return (error);    dchain = new;    return (write_dchain(dchain, task));}/* Edit next slot number, current file*/int edit_nextslot(DB_TASK *task){    int      error;    DB_ULONG nextslot;    long     old, new;    if ((error = read_nextslot(&nextslot, task)) < 0)        return (error);    old = nextslot;    if ((error = edit_long(old, &new)) < 0)        return (error);    nextslot = new;    return (write_nextslot(nextslot, task));}

⌨️ 快捷键说明

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