Beispiel #1
0
int _plkr_AddToTable
    (
    HashTable*  ht,
    char*       key,
    void*       obj
    )
{
    HashTableSlot*  slot;
    int             count;

    if (ht == NULL)
        return (0);

    slot = hashtable_slot (ht, hashtable_hash_index (ht, key));

    for (count = slot->hs_count; count > 0; count -= 1)
        if (hashtable_compare_keys
            (ht, key, slot->hs_entries[count - 1].he_key))
            return (0);

    if (slot->hs_allocated == 0) {
        slot->hs_allocated = HASH_INCREMENT_SIZE;
        slot->hs_entries =
            (HashEntry *) malloc (sizeof (HashEntry) * slot->hs_allocated);
        slot->hs_count = 0;
    }
    else if (slot->hs_count >= slot->hs_allocated)
        slot->hs_entries = (HashEntry *) realloc (slot->hs_entries,
                                                  (slot->hs_allocated +=
                                                   HASH_INCREMENT_SIZE)
                                                  * sizeof (HashEntry));
    slot->hs_entries[slot->hs_count].he_key =
        _plkr_strndup (key, strlen (key));
    slot->hs_entries[slot->hs_count].he_data = obj;
    slot->hs_count += 1;
    ht->ht_nPairs += 1;
    return (1);
}
Beispiel #2
0
static int ReadConfigFile
    (
    char* filename
    )
{
    HashTable*  current_segment = NULL;
    FILE*       fp = fopen (filename, "r");
    char*       ptr;
    char*       str_end;
    char*       str_begin;
    char*       charptr;
    char*       current_option;
    char*       option_value;
    char        linebuf[MAX_LINE_SIZE + 1];
    int         linelen;
    int         len2;
    int         buf_index;
    int         status;
    int         line_number;

    if (fp == NULL) {
        _plkr_message ("Can't open config file %s", filename);
        return 0;
    }

    current_segment = GetOrCreateSegment ("default");
    current_option = NULL;

    status = 1;                 /* optimistic */
    line_number = 0;

    while (1) {

        ptr = fgets (linebuf, sizeof (linebuf) - 1, fp);
        if (ptr == NULL)
            break;

        line_number += 1;
        linebuf[strlen (linebuf) - 1] = 0;      /* strip newline */
        if (linebuf[strlen (linebuf) - 1] == '\r')
            linebuf[strlen (linebuf) - 1] = 0;  /* strip carriage return */

        /* fprintf (stderr, "%s:%d:  line is '%s'\n", filename, line_number, linebuf); */

        linelen = strlen (linebuf);
        for (buf_index = 0; linebuf[buf_index] != 0; buf_index++)
            if (!isspace (linebuf[buf_index]))
                break;

        if (linebuf[buf_index] == 0)
            /* blank line */
            continue;

        if ((strchr (COMMENT_CHARS, linebuf[0]) != NULL) ||
            (strncmp (linebuf, "rem", 3) == 0) ||
            (strncmp (linebuf, "REM", 3) == 0))
            /* comment */
            continue;

        /* At this point we have a valid thing */

        if (linebuf[buf_index] == SEGMENT_LEAD_CHAR) {
            if ((str_end =
                 strchr (linebuf + buf_index + 1,
                         SEGMENT_END_CHAR)) == NULL) {
                /* invalid segment line */
                _plkr_message ("%s:%d:  Invalid segment line '%s'",
                               filename, line_number, linebuf);
                goto error_exit;
            }
            str_begin = linebuf + buf_index + 1;
            for (charptr = str_begin; charptr < str_end; charptr++)
                *charptr = tolower (*charptr);
            *str_end = 0;
            current_segment = GetOrCreateSegment (str_begin);
            /* fprintf (stderr, "Current segment is now %p (%s)\n", current_segment, str_begin); */
            if (current_option)
                free (current_option);
            current_option = NULL;

        }
        else if ((linebuf[0] == ' ' || linebuf[0] == '\t')
                 && current_option != NULL) {
            /* continuation line */
            str_begin =
                (char *) _plkr_RemoveFromTable (current_segment,
                                                current_option);
            for (str_end = linebuf + strlen (linebuf) - 1;
                 str_end > linebuf && isspace (*str_end); str_end--);
            charptr =
                (char *) malloc (strlen (str_begin) +
                                 (str_end - (linebuf + buf_index)) + 2);
            strcpy (charptr, str_begin);
            len2 = strlen (charptr);
            charptr[len2] = '\n';
            strncpy (charptr + len2 + 1, linebuf + buf_index,
                     str_end - (linebuf + buf_index));
            charptr[len2 + (str_end - (linebuf + buf_index)) + 1] = '\0';
            _plkr_AddToTable (current_segment, current_option, charptr);
            free (str_begin);

        }
        else if (strcspn (linebuf, OPTION_SEPARATOR_CHARS) < linelen) {
            /* possible option line */

            for (str_begin = linebuf + buf_index, ptr = str_begin;
                 isalnum (*ptr) || (*ptr == '.') || (*ptr == '_')
                 || (*ptr == '-'); ptr++);
            if (ptr == str_begin) {
                _plkr_message ("%s:%d:  Invalid option line '%s'",
                               filename, line_number, linebuf);
                goto error_exit;
            }

            for (charptr = str_begin; charptr < ptr; charptr++)
                *charptr = tolower (*charptr);
            str_end = ptr;

            while (isspace (*ptr) && (*ptr != '\0'))
                ptr++;

            if (strchr (OPTION_SEPARATOR_CHARS, *ptr) != NULL)
                ptr++;
            else {
                _plkr_message ("%s:%d:  Invalid option line '%s'",
                               filename, line_number, linebuf);
                goto error_exit;
            }

            while (isspace (*ptr) && (*ptr != '\0'))
                ptr++;

            if (*ptr == 0) {
                _plkr_message ("%s:%d:  Invalid option line '%s'",
                               filename, line_number, linebuf);
                goto error_exit;
            }

            if (current_option)
                free (current_option);
            current_option =
                _plkr_strndup (str_begin, str_end - str_begin);

            option_value = _plkr_strndup (ptr, strlen (ptr));

            ptr =
                (char *) _plkr_RemoveFromTable (current_segment,
                                                current_option);
            if (ptr)
                free (ptr);
            _plkr_AddToTable (current_segment, current_option,
                              option_value);
            /* fprintf (stderr, "Added value '%s' for option '%p:%s'\n", option_value, current_segment, current_option); */

        }
        else {
            _plkr_message ("%s:%d:  Bad line '%s'", filename, line_number,
                           linebuf);
            goto error_exit;
        }
    }

  good_exit:

    if (current_option)
        free (current_option);
    fclose (fp);
    return status;

  error_exit:
    status = 0;
    goto good_exit;
}