Created
September 17, 2008 16:30
-
-
Save hakobe/11257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1回生向け課題のめも | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <errno.h> | |
const int DEFAULT_RECORDS_LIST_SIZE = 16; | |
const int MAX_BUF_SIZE = 256; | |
typedef struct { | |
char code[10]; | |
char name[40]; | |
int price; | |
} Record; | |
typedef struct { | |
Record **list; | |
int size; | |
int _mem_size; | |
} Records; | |
void usage() { | |
fprintf(stderr, "usage: kadai1-3 inputfile\n"); | |
} | |
void error(const char *msg) { | |
if (errno) { | |
fprintf(stderr, "kadai1-3: %s: %s \n", msg, strerror(errno)); | |
} | |
else { | |
fprintf(stderr, "kadai1-3: %s\n", msg); | |
} | |
usage(); | |
} | |
Record *create_record(char const *code, char const *name, int price) { | |
Record *record = NULL; | |
if ( (record = (Record *)(malloc(sizeof(Record)))) == NULL) { | |
error("Memory allocation error"); | |
return NULL; | |
} | |
strncpy(record->code, code, strlen(code)); | |
strncpy(record->name, name, strlen(name)); | |
record->price = price; | |
return record; | |
} | |
Records *create_records() { | |
Records *records; | |
records = (Records *)malloc(sizeof(Records)); | |
if (records == NULL) { | |
error("Memory allocation error"); | |
return NULL; | |
}; | |
records->list = (Record **)calloc(DEFAULT_RECORDS_LIST_SIZE, sizeof(Record *)); | |
if (records->list == NULL) { | |
free(records); | |
error("Memory allocation error"); | |
return NULL; | |
} | |
records->_mem_size = DEFAULT_RECORDS_LIST_SIZE; | |
records->size = 0; | |
return records; | |
} | |
Record *add_record(Records *records, Record *record) { | |
if (!(records->size < records->_mem_size)) { | |
// TODO | |
// realloc | |
// update _mem_size | |
} | |
records->list[records->size] = record; | |
records->size++; | |
return record; | |
} | |
Record *search_record(Records *records, char *keyword) { | |
Record *record; | |
int i; | |
for (i = 0; i < records->size; i++) { | |
record = records->list[i]; | |
if (strncmp(keyword, record->code, MAX_BUF_SIZE) == 0) { | |
return record; | |
} | |
} | |
return NULL; | |
} | |
void free_records(Records *records) { | |
int i; | |
for (i = 0; i < records->size; i++) { | |
free(records->list[i]); | |
} | |
free(records->list); | |
free(records); | |
} | |
Records *load_records(FILE *i_fp) { | |
int i; | |
char buf[MAX_BUF_SIZE]; | |
char code[MAX_BUF_SIZE], name[MAX_BUF_SIZE]; | |
int price; | |
Records *records; | |
Record *record; | |
records = create_records(); | |
if (records == NULL) { | |
return NULL; | |
}; | |
while ( fgets(buf, MAX_BUF_SIZE, i_fp) != NULL ) { | |
i = 0; | |
sscanf(buf, "%s%*[ ]%s%*[ ]%d", code, name, &price); | |
record = create_record(code, name, price); | |
if (records == NULL) { | |
free_records(records); | |
return NULL; | |
}; | |
record = add_record(records, record); | |
if (record == NULL) { | |
free_records(records); | |
return NULL; | |
}; | |
} | |
return records; | |
} | |
int main (int argc, char *argv[]) { | |
char buf[MAX_BUF_SIZE]; | |
char *i_filename; | |
FILE *i_fp; | |
Records *records; | |
Record *record; | |
errno = 0; | |
if (argc < 2) { | |
error("Too few arugments"); | |
return EXIT_FAILURE; | |
} | |
i_filename = argv[1]; | |
i_fp = fopen(i_filename, "r"); | |
if( i_fp == NULL) { | |
error("File open error"); | |
return EXIT_FAILURE; | |
} | |
records = load_records(i_fp); | |
if (records == NULL) { | |
fclose(i_fp); | |
return EXIT_FAILURE; | |
} | |
printf("keyword: "); | |
fgets(buf, MAX_BUF_SIZE, stdin); | |
buf[strlen(buf) - 1] = '\0'; | |
record = search_record(records, buf); | |
if (record) { | |
printf("code:\t%s\n", record->code); | |
printf("name:\t%s\n", record->name); | |
printf("price:\t%d\n", record->price); | |
printf("\n"); | |
} | |
else { | |
printf("Not Found.\n"); | |
} | |
free_records(records); | |
fclose(i_fp); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment