예제 #1
0
파일: index.c 프로젝트: klihub/murphy
int mdb_index_print(mdb_table_t *tbl, char *buf, int len)
{
#define PRINT(args...)  if (e > p) p += snprintf(p, e-p, args)
    mdb_index_t *ix;
    const char  *sep;
    char        *p, *e;
    int          i;

    MDB_CHECKARG(tbl && buf && len > 0, 0);

    ix = &tbl->index;

    MDB_PREREQUISITE(MDB_INDEX_DEFINED(ix), 0);

    e = (p = buf) + len;

    PRINT("index columns: ");

    for (i = 0, sep = "";   i < ix->ncolumn;   i++, sep = ",")
        PRINT("%s%02d", sep, ix->columns[i]);

    PRINT("\n    type    offset length\n    ---------------------"
          "\n    %-7s   %4d   %4d\n",
          mqi_data_type_str(ix->type), ix->offset, ix->length);

    return p - buf;

#undef PRINT
}
예제 #2
0
파일: result.c 프로젝트: jeeb/murphy
mql_result_t *mql_result_string_create_column_list(int               ncol,
        mqi_column_def_t *defs)
{
#define INDEX   0
#define NAME    1
#define TYPE    2
#define LENGTH  3
#define FLDS    4

    static const char *hstr[FLDS]  = {"index", " name" , "type", "length"};
    static int         hlen[FLDS]  = {   5   ,     5   ,    4  ,     6   };
    static int         align[FLDS] = {  +1   ,    -1   ,   -1  ,    +1   };

    result_string_t  *rslt;
    mqi_column_def_t *def;
    const char       *typstr[MQI_COLUMN_MAX];
    int               namlen[MQI_COLUMN_MAX];
    int               typlen[MQI_COLUMN_MAX];
    int               fldlen[FLDS];
    int               linlen;
    int               len;
    int               offs;
    char             *p, *q;
    int               i, j;

    MDB_CHECKARG(ncol > 0 && ncol < MQI_COLUMN_MAX && defs, NULL);

    memcpy(fldlen, hlen, sizeof(fldlen));

    for (i = 0;  i < ncol;  i++) {
        def = defs + i;

        typstr[i] = mqi_data_type_str(def->type);

        if ((len = (namlen[i] = strlen(def->name)) + 1) > fldlen[NAME])
            fldlen[NAME] = len;

        if ((len = (typlen[i] = strlen(typstr[i]))) > fldlen[TYPE])
            fldlen[TYPE] = len;
    }

    for (linlen = FLDS, i = 0;  i < FLDS;  linlen += fldlen[i++])
        ;

    len = linlen * (ncol+2) + 1;

    /*
     * allocate and initialize the result structure
     */
    if (!(rslt = calloc(1, sizeof(result_string_t) + len))) {
        errno = ENOMEM;
        return NULL;
    }

    rslt->type = mql_result_string;
    rslt->length = len;

    p = rslt->string;
    memset(p, ' ', linlen * (ncol+2));

    /*
     * labels
     */
    for (q = p, j = 0;   j < FLDS;   q += (fldlen[j++] + 1)) {
        offs = (align[j] < 0) ? 0 : fldlen[j] - hlen[j];
        memcpy(q + offs, hstr[j], hlen[j]);
    }

    p += linlen;
    *(p-1) = '\n';

    /*
     * separator line
     */
    memset(p, '-', linlen);
    p += linlen;
    *(p-1) = '\n';


    /*
     * data lines
     */
    for (i = 0;  i < ncol;  i++, p += linlen) {
        def = defs + i;
        snprintf(p, linlen+1, "%*d %s%-*s %-*s %*d\n",
                 fldlen[INDEX], i,
                 def->flags&MQI_COLUMN_KEY ? "*":" ", fldlen[NAME]-1,def->name,
                 fldlen[TYPE], typstr[i],
                 fldlen[LENGTH], def->length);
    }

    return (mql_result_t *)rslt;

#undef FLDS
#undef LENGTH
#undef TYPE
#undef NAME
#undef INDEX
}