Created
June 6, 2013 07:19
-
-
Save rafkhan/5719861 to your computer and use it in GitHub Desktop.
Garbage summative assignment.
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char* file_to_string(FILE *fp); | |
int count_delim(char *str); | |
void tokenize(int *indices, char *str); | |
void isort(int* arr, int len); | |
#define F(x) void func_##x() {printf("In function %d\n",x);} | |
F(0); | |
F(1); | |
F(2); | |
F(3); | |
F(4); | |
F(5); | |
F(6); | |
F(7); | |
F(8); | |
F(9); | |
int main(int argc, char **argv) { | |
FILE *fp = fopen("file.txt", "r"); | |
if(fp == NULL) { | |
fprintf(stderr, "Null file pointer"); | |
exit(-1); | |
} | |
char *file_content = file_to_string(fp); | |
if(file_content == NULL) { | |
fprintf(stderr, "Null file contents\n"); | |
exit(-1); | |
} | |
fclose(fp); | |
int len = count_delim(file_content); | |
int indices[len]; | |
tokenize(indices, file_content); | |
free(file_content); | |
void (*fptr[10])(void); | |
fptr[0] = &func_0; | |
fptr[1] = &func_1; | |
fptr[2] = &func_2; | |
fptr[3] = &func_3; | |
fptr[4] = &func_4; | |
fptr[5] = &func_5; | |
fptr[6] = &func_6; | |
fptr[7] = &func_7; | |
fptr[8] = &func_8; | |
fptr[9] = &func_9; | |
for(int i = 0; i < len; i++) { | |
fptr[indices[i]](); | |
} | |
return 0; | |
} | |
int count_delim(char *str) { | |
int i; | |
int count = 0; | |
int size = strlen(str); | |
for(i = 0; i < size; i++) { | |
if(str[i] == ',') { | |
count++; | |
} | |
} | |
return count; | |
} | |
char* file_to_string(FILE *fp) { | |
char *buffer = NULL; | |
if (fseek(fp, 0L, SEEK_END) == 0) { | |
long buf_size = ftell(fp); | |
if (buf_size == -1) {/*error*/} | |
buffer = (char *) malloc(sizeof(char) * (buf_size + 1)); | |
if(buffer == NULL) {/*error*/} | |
if (fseek(fp, 0L, SEEK_SET) == 0) {/*error */} | |
size_t new_len = fread(buffer, sizeof(char), buf_size, fp); | |
if (new_len == 0) { | |
//error | |
} else { | |
buffer[new_len + 1] = '\0'; /* Just to be safe. */ | |
} | |
} | |
return buffer; | |
} | |
void tokenize(int *indices, char *str) { | |
char *token; | |
int i = 0; | |
token = strtok(str, ","); | |
while(token != NULL) { | |
indices[i] = atoi(token); | |
i++; | |
token = strtok(NULL, ","); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment