CS-PROJECTS / c08_gis_system / buildIndex.c
buildIndex.c
Raw
#include "arrayList.h"
#include "FIDIndex.h"
#include "StringHashTable.h"
#include "nextField.h"
#include "buildIndex.h"
#include "GISParse.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH 500

/**
 * table points to a StringHashTable struct, list points to an arrayList struct,
 * and gis points to a GIS file
 * 
 * This method parses throguh a GIS file and populates hash table, defining the 
 * keys as a combination between the name and state abreviation of the particular record 
 * as well as storing each offset on which this key is found 
 */
void build_name(StringHashTable *table, FILE *gis)
{
    char *gisLine = calloc(MAX_LINE_LENGTH - 1, sizeof(char));
    while (fgets(gisLine, MAX_LINE_LENGTH - 1, gis) != NULL)
    {
        uint32_t pos = ftell(gis) - strlen(gisLine);
        char *key = get_name_and_state(gisLine);
        StringHashTable_addEntry(table, key, pos);
    }
    free(gisLine);
}

/** 
 * list points to an arrayList struct and gis points to a GIS file.
 * 
 * The method parses through a GIS file and popualtes the arrayList with FIDIndex
 * structs that hold the filed ID and offset of each record in the file
 */
void build_FID(arrayList *list, FILE *gis)
{
    char *gisLine = calloc(MAX_LINE_LENGTH - 1, sizeof(char));
    while (fgets(gisLine, MAX_LINE_LENGTH - 1, gis) != NULL)
    {
        uint32_t pos = ftell(gis) - strlen(gisLine);
        char *ID = nextField(gisLine);
        FIDIndex *entry = FIDIndex_create(ID, pos);
        AL_insert(list, entry);
    }
    fseek(gis, 0, SEEK_SET);
}