int db_entry_read(FILE * f, db_entry * entry) { while (!feof(f)) { char * line; int fieldCount = read_csv_row(f, &line); if (fieldCount == 0) { continue; } else if (fieldCount < 0) { return 1; } if (fieldCount != 10) { free(line); return 1; } entry->type = line; line += strlen(line) + 1; errno = 0; entry->timestamp = strtoull(line, NULL, 10); if (errno || entry->timestamp == 0) { free(entry->type); return 1; } line += strlen(line) + 1; entry->rssi = atoi(line); line += strlen(line) + 1; errno = 0; entry->size = (size_t)strtoull(line, NULL, 10); if (errno || entry->size == 0) { free(entry->type); return 1; } char ** fields[] = {&entry->client, &entry->access_point, &entry->request_path, &entry->request_host, &entry->request_user_agent, &entry->response_headers, NULL}; char *** fieldsPtr = fields; for (; *fieldsPtr; fieldsPtr += 1) { line += strlen(line) + 1; (*(*fieldsPtr)) = line; } return 0; } return -1; }
int main(int argc, char* argv[]) { if (argc < 2){ // missing required argument printf("Usage %s <filename>\n", argv[0]); exit(-1); } int cols = 0; int rows = 0; // open the file to use the standard IO library. FILE *fd = fopen(argv[1], "r"); // read the column names if ( (cols = read_csv_row(fd, names)) < 0){ printf("Failed to read names from CSV file.\n"); exit(-2); }else{ #ifdef DEBUG // #define DEBUG to see debug output // or compile with gcc -DDEBUG int i; for (i=0; i<MAXCOLS; i++){ printf("Col[%03d]: %s\n", i, names[i]); } #endif } // the remaining lines are the rows, read them // into the data array. if ( (rows = read_csv_cols(fd, data)) <= 0){ printf("Error: could not read file!\n"); exit(-3); } fclose(fd); // compute the average for each column. int i; for (i=0; i<cols; i++){ printf("Average for column %50s is %12.6f\n", names[i], average(rows, i, data)); } return 0; }