Skip to content

Instantly share code, notes, and snippets.

@SevereOverfl0w
Created January 14, 2015 17:58
Show Gist options
  • Save SevereOverfl0w/77813d443c1e42e85a9f to your computer and use it in GitHub Desktop.
Save SevereOverfl0w/77813d443c1e42e85a9f to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <regex.h>
#include <string.h>
#include <stdlib.h>
/* The following is the size of a buffer to contain any error messages
* encountered when the regular expression is compiled. */
#define MAX_ERROR_MSG 0x1000
/*
* Match the string in "to_match" against the compiled regular
* expression in "r".
* */
static int match_regex (regex_t * r, const char * to_match)
{
/* "P" is a pointer into the string which points to the end of the
* previous match. */
const char * p = to_match;
/* "N_matches" is the maximum number of matches allowed. */
const int n_matches = 10;
/* "M" contains the matches found. */
regmatch_t m[n_matches];
while (1) {
int i = 0;
int nomatch = regexec (r, p, n_matches, m, 0);
if (nomatch) {
printf ("No more matches.\n");
return nomatch;
}
for (i = 0; i < n_matches; i++) {
int start;
int finish;
if (m[i].rm_so == -1) {
break;
}
start = m[i].rm_so + (p - to_match);
finish = m[i].rm_eo + (p - to_match);
if (i == 0) {
printf ("$& is ");
}
else {
printf ("$%d is ", i);
}
printf ("'%.*s' (bytes %d:%d)\n", (finish - start),
to_match + start, start, finish);
}
p += m[0].rm_eo;
}
return 0;
}
/**
* 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);
match_regex(&regex, line);
//strcat(substr, "\0");
//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