Created
January 18, 2012 14:54
-
-
Save pearswj/1633351 to your computer and use it in GitHub Desktop.
A short C program to parse tab separated UoS module timetable data to Google .csv format.
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
/* for converting tab separated UoS module timetable data to Google .csv */ | |
/* Notes: */ | |
// - issue with week number ranges. Duplicate lines to remove ranges before running. | |
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAXLINE 1000 /* maximum input line size */ | |
int getline(char line[], int maxline); | |
//void copy(char to[], char from[]) | |
void getdate1(char dow[], char week[], char date[]); | |
int j=0; | |
/* read tab separated values into separate (jth) string and print in special format */ | |
int main () | |
{ | |
int len; | |
char line[10][MAXLINE]; | |
char date[11]; | |
date[10] = '\0'; | |
printf("Subject,Start Date,Start Time,End Time,Location,Description\n"); | |
while ((len = getline(line[j], MAXLINE)) > 0) | |
if (j == 0) { | |
getdate1(line[3], line[6], date); | |
printf("\"%s %s\",\"%s\",\"%s\",\"%s\",\"%s, %s\",\"Lecture for %s - %s with %s at %s\"\n",line[0], line[2], date, line[4], line[5], line[7], line[8], line[0], line[1], line[9], line[7]); | |
} | |
return 0; | |
} | |
/* getline: read a line into s, return length */ | |
int getline(char s[], int lim) | |
{ | |
int i; | |
char c; | |
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n' && c!='\t'; ++i) | |
s[i] = c; | |
if (c == '\t') | |
++j; | |
if (c == '\n') | |
j = 0; | |
s[i] = '\0'; | |
return i; | |
} | |
/* getdate: read week number and day and work out date */ | |
void getdate1(char dow[], char week[], char date[]) | |
{ | |
int weeknum, wday, days_to_add; | |
struct tm * newtime; | |
struct tm str_time; | |
time_t time_of_day; | |
if (dow[0] == 'M') | |
wday = 1; | |
if (dow[0] == 'T' && dow[1] == 'u') | |
wday = 2; | |
if (dow[0] == 'W') | |
wday = 3; | |
if (dow[0] == 'T' && dow[1] == 'h') | |
wday = 4; | |
if (dow[0] == 'F') | |
wday = 5; | |
if (dow[0] == 'S' && dow[1] == 'a') | |
wday = 6; | |
if (dow[0] == 'S' && dow[1] == 'u') | |
wday = 0; | |
weeknum = atoi(week); | |
days_to_add = weeknum * 7 + wday; | |
str_time.tm_year = 2011-1900; | |
str_time.tm_mon = 9-1; | |
str_time.tm_mday = 18+days_to_add; | |
str_time.tm_hour = 0; | |
str_time.tm_min = 0; | |
str_time.tm_sec = 0; | |
str_time.tm_isdst = 0; | |
time_of_day = mktime(&str_time); | |
newtime = localtime(&time_of_day); | |
strftime(date,10,"%d/%m/%Y",newtime); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment