Skip to content

Instantly share code, notes, and snippets.

@SevereOverfl0w
Created January 14, 2015 17:26
Show Gist options
  • Save SevereOverfl0w/ee09a9bc8959eec1419d to your computer and use it in GitHub Desktop.
Save SevereOverfl0w/ee09a9bc8959eec1419d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include <stdlib.h>
/**
* This is an enumeration of day.
*/
enum day {
SATURDAY = 1,
SUNDAY = 2,
MONDAY = 3,
TUESDAY = 4,
WEDNESDAY = 5,
THURSDAY = 6,
FRIDAY = 7
};
/**
* This is an enumeration level.
* Each level represents a different field of study.
*/
enum level {
UNDERGRADUATE = 1,
GRADUATE = 2,
DOCTORATE = 3
};
/**
* Structure for the name of the instructor.
*/
typedef struct {
char name[1024];
char surname[1024];
} name_t;
/**
* Structure for the location of each class.
*/
typedef struct {
int time;
day d;
int classNumber;
char faculty[];
} location_t;
/**
* The main structure of the program.
*/
typedef struct {
name_t name;
char course[];
int courseID;
location_t loc[];
int isCombined;
level lvl;
} info_t;
// Global regular expression.
regex_t regex;
int main() {
int status;
status = regcomp(&regex, "[a-z0-9 :-]+", REG_ICASE | REG_EXTENDED);
if (status) {
printf("Regex Exception. Could not compile regex.\n");
getchar();
return 0;
}
FILE *input = NULL;
input = fopen("info.csv", "r");
if (input == NULL) {
printf("IOStream exception. File status.\n");
getchar();
return 0;
}
char line[1024];
int i = 0;
do {
regmatch_t match[50];
//fscanf(input, "%[a-zA-Z0-9 :,-]s", line);
fgets(line, 1000, input);
if (feof(input)) {
break;
}
printf("Reading line %s\n", line);
status = regexec(&regex, line, 50, match, 0);
if (status) {
char errbuf[1024];
regerror(status, &regex, errbuf, sizeof(errbuf));
printf("REGEX exception. %s.\n", errbuf);
getchar();
return 0;
}
char substr[20];
memset(substr, 0, 20);
printf("%d : %d\n", match[i].rm_so, match[i].rm_eo);
strncpy(substr, line + match[i].rm_so, (size_t) match[i].rm_eo);
//strcat(substr, "\0");
printf("%s\n", substr);
//i++;
setbuf(input, NULL);
} while (!feof(input));
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment