Newer
Older
Microsoft / pdb / BLDPDBIX.C
@tundra tundra on 24 May 2012 5 KB Initial revision
/* BLDPDBIX.C - Build PDB Index File From Raw Data File
                Last Modified: 07-25-90
                By T.A. Daneliuk
*/

#include    <stdio.h>
#include    <bplus.h>
#include    <string.h>

#define     DEBUG   0                   /* 0= No Debug */

#define     DUP     1                   /* Allow Duplicate Keys */  
#define     CR      0x0d                /* Carriage Return */
#define     LF      0x0a                /* LineFeed */
#define     EOR     LF                  /* End-Of-Record */

int         KEY1 =   0;                  /* Offset Of First Key In Record */
int         KEY2 =  46;                  /* Offset Of 2nd Key In Record */
char    data_file[255] = "P:\\PDBASE";
char    idx_file[255]  = "P:\\PDBASE.IDX";
char    work[100];                      /* Working Buffer */
unsigned long    offset;                /* Record Position In File */

static  void    mk_idx(), bld_idx(), add_keys();
static  int     get_rec();

/*============================================================================*/

main(argc,argv)
int     argc;
char    **argv;

{
    int z;

    printf("PDB - Version 1.2, Copyright (c) 1988, 1990 T.A. Daneliuk - TundraWare!\n");
    printf("Released To The Public Domain - No Warranties Expressed Or Implied!\n\n");

    z = 0;
    while ((z++ < argc) && (argv[z][0] == '-'))
        {
        switch (argv[z][1])
            {
            case 'i':
                strcpy(idx_file, argv[z]+2);
                break;
            case 'd':
                strcpy(data_file, argv[z]+2); 
                break;
            default:
                printf("Invalid Command Line Switch, Ignored!\n");
            }            
        }

    mk_idx();
    bld_idx();    
}

/*============================================================================*/

static void mk_idx()                    /* Create The Index File */
{
    IX_DESC node;
 
    printf("\nCreating Index File %s For Data File %s ...",idx_file, data_file);
    make_index(idx_file, &node, DUP);
    printf("\nIndex File %s Created.\n",idx_file);
    close_index(&node);
 }

/*============================================================================*/

static void bld_idx()                   /* Build Index File */

{
    FILE    *fp;
    IX_DESC node;
    ENTRY   entry;
    
    fp = fopen(data_file, "rb");        /* Open Data File */
    if (fp == NULL)
        {
        printf("\n Cannot Open %s, Aborting ...\n", data_file);
        return;
        }       
    offset = 0;

    open_index(idx_file, &node, DUP);

    printf("\nBuilding Index Records ...\n");
    while (get_rec(fp) == 0)            /* Get Record 'Till None Left */
        {
#if DEBUG
        printf("%s\n", work);
#endif
        if (strlen(work) <= KEY2)       /* Improper Record Length */
            {
            printf("Bad Record At Position %u:\n", offset);
            printf("%s\n", work);
            printf("Record NOT Added To Index List!\n");
            }
        else
            {
            add_keys(&entry, &node);        /* Add Keys */
            offset += strlen(work);         /* Update Record Pointer */
            }
        }
        if (strlen(work) <= KEY2)       /* Improper Record Length */
            {
            printf("Bad Record At Position %lu:\n", offset);
            printf("%s\n", work);
            printf("Record NOT Added To Index List!\n");
            }
        else
            {
            add_keys(&entry, &node);        /* Add Keys */
            offset += strlen(work);         /* Update Record Pointer */
            }

    printf("\nIndex Build Complete.\n");

    close_index(&node);
    fclose(fp);
}

/*============================================================================*/

static int get_rec(fp)                  /* Read A Record */
FILE    *fp;

{
    int c, x;
    x=0;                                /* Index Into Work Buffer */

    c = fgetc(fp);
    while ((c != EOF) && (c != EOR))    /* Get A Record */
        {
        work[x++] = c;
        c = fgetc(fp);
        }
    if (c == EOR)
        {
        work[x++] = c;
        work[x]   = '\0';               /* Terminate Record String */
        return (0);                     /* Return With Good Status */
        }
    else                                /* At End-Of-File */
        {
        work[x] = '\0';
        return (1);
        }
}

/*============================================================================*/

static void add_keys(entry, node)        /* Add Keys */
ENTRY   *entry;
IX_DESC *node;

{
    int x, y, z, flag;

    x = KEY1;
    y = 0;
    entry->recptr = offset;
    while ((z = work[x++]) != ' ')
        entry->key[y++] = toupper(z);
    entry->key[y] = '\0';
#if DEBUG
    printf("%s\n", entry->key);
#endif    
    add_key(entry, node);

    if (strcmp(work+strlen(work)-6, ".zip\r\n") != 0)
        flag = 0;
    else
        {
        flag = 1;
        x = strlen(work)-3;
        while ((work[x] != '/') && (x > KEY2))
            x--; 
        }
    if (work[x] == '/')
        x++;

    y = 0; 
    if (flag)                                   /* A .ZIP File Member */
        {
        entry->recptr = offset;
        while ((z = work[x++]) != CR)
            entry->key[y++] = toupper(z);
        entry->key[y] = '\0';
#if DEBUG
    printf("%s\n", entry->key);
#endif    
        add_key(entry, node);
        }

}

/*============================================================================*/