#include #include #include #include "wweather.h" #ifndef NULL #define NULL (void *)0 #endif int wcount = 0; /** Create a print rep of the passed in weather struct * @param w a weather struct pointer. * **/ void wprinter(WeatherData* w) { char timee[55]; timeToString(timee, w->time); char tempe[55]; tempToString(tempe, w->temperature); printf("Time:%s Temp:%s\n", timee, tempe); } /** * Parse a line of text into a weather struct * @param line -- the text to be parsed * @param w -- pointer to a weather struct, then internals will be changed * **/ WeatherData* parse(char* line) { //printf("%s", line); WeatherData *ret = malloc(sizeof(WeatherData)); char *c = strtok(line, " \t"); char *c2 = strtok(NULL, " \t"); ret->time = makeTime(c, c2); c = strtok(NULL, " \t"); c2 = strtok(NULL, " \t"); //printf("toTemp ||%s||%s||\n", c, c2); ret->temperature = makeTemperature(atoi(c), c2); c = strtok(NULL, " \t"); c2 = strtok(NULL, " \t"); ret->dewPoint = makeTemperature(atoi(c), c2); c = strtok(NULL, " \t"); ret->relHum = atoi(c); c = strtok(NULL, " \t"); c2 = strtok(NULL, " \t"); char *c3 = strtok(NULL, "\t"); ret->wind = makeWind(c, atoi(c2), c3); return ret; } /** * Read a file containing weather data and parse that file * into the weather array * @param the name of the file to be read * @return the number of lines of weather data that were read * **/ int readFile(char* fileName) { weather = malloc(200 * sizeof(WeatherData *)); char line[256]; FILE *f = fopen(fileName, "r"); if (f==NULL) { fprintf(stderr, "Could not open %s -- quitting\n", fileName); return -1; } #ifndef MAIN_ARRAY WeatherData **cWeather = weather; #endif int c = 0; while (NULL != fgets(line, 256, f)) { if (strlen(line)>0) { #ifdef MAIN_ARRAY weather[c] = parse(line); #else *cWeather = parse(line); cWeather++; #endif c++; } } printf("read %d weather objects\n", c); wcount = c; fclose(f); return c; } /** * Weather destructor * @param the weather struct to be destroyed * **/ void freeWeather(WeatherData * ww) { freeTime(ww->time); freeTemperature(ww->temperature); freeTemperature(ww->dewPoint); freeWind(ww->wind); free(ww); } /** * free the array and its contents * **/ void freeAllWeather() { for (int i = 0; i < wcount; i++) { freeWeather(weather[i]); } free(weather); }