// File: namevalu.cpp // Author: Tyler Devore // Description: This file contains the NameValueList class methods. // change: added fill(). -Dave Hershberger #include "namevalu.h" #include #include // function: fill // fills a NameValueList with name-value pairs from a file int NameValueList::read(char *filename) { FILE *file_ptr; // pointer to configuration file char value[BUF]; // value string read from file char string[BUF]; // name string read from file // a subsystem. file_ptr = fopen(filename, "r"); if (!file_ptr) return 0; while(!feof(file_ptr)) { string[0] = '\0'; if(fscanf(file_ptr, "%s", string) == EOF) // get first string continue; if(string[0] == '*' || string[0] == '#'){ // * or # marks a comment. fgets(string, BUF, file_ptr); // therefore, grab a continue; // line and ignore it. } value[0] = '\0'; fscanf(file_ptr, "%s", value); // get next string. // add the pair to the current list add(string, value); } fclose(file_ptr); return 1; } // function: add // description: adds a name value pair to the list void NameValueList::add(char str[], char val[]) { NameValueListeLem* temp = new NameValueListeLem; // create a new element temp->next = first(); // link to list strcpy(temp->string, str); strcpy(temp->value, val); h = temp; // update head of list } // function: print // description: used for testing NameValueList. prints values in list. void NameValueList::print() { NameValueListeLem* temp = first(); while(temp!=0) // detect end of NameValueList { printf("%s -> %s\n", temp->string, temp->value); temp = temp->next; } } // function: release // description: returns memory in NameValueList to free store void NameValueList::release() // elements are returned to free store { while(first() != 0) del(); }