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
/* --------------------------------------------------------------------------------- | |
SHEF2CSV by Will Pearson | |
A simple C program for converting tab separated UoS module timetable data to Google | |
.csv format. | |
Usage: redirect stdin/stdout, e.g. | |
$ ./shef2csv < input.txt > output.csv | |
Notes: | |
- issue with week number ranges. Duplicate lines to remove ranges before running. | |
- no special handling of empty cells | |
To Do: | |
- convert "lastname,firstname" to "firstname lastname" | |
--------------------------------------------------------------------------------- */ | |
#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 getdate1(char dow[], char week[], char date[]); | |
int j=0; // string counter | |
/* 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'; // date string terminated with null character | |
/* print header row */ | |
printf("Subject,Start Date,Start Time,End Date,End Time,Location,Description\n"); | |
while ((len = getline(line[j], MAXLINE)) > 0) | |
if (j == 0) { // i.e. a full line has been read... | |
getdate1(line[3], line[6], date); | |
/* Example format: "CIV400 LECT","08/05/2012","14:00","16:00","MAPP-LT07, | |
Mappin Building","Lecture for CIV400 - Portfolio with Huang,Wei at MAPP-LT07" */ | |
printf("\"%s %s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s, %s\"," | |
"\"%s for %s - %s with %s at %s\"\n",line[0], line[2], date, line[4], date, | |
line[5], line[7], line[8], line[2], 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') // begin next string after tab entry | |
++j; | |
if (c == '\n') // reset string counter on newline entry | |
j = 0; | |
s[i] = '\0'; // terminate string | |
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; | |
/* Day */ | |
if (dow[0] == 'M') // Monday | |
wday = 1; | |
if (dow[0] == 'T' && dow[1] == 'u') // Tuesday | |
wday = 2; | |
if (dow[0] == 'W') // Wednesday | |
wday = 3; | |
if (dow[0] == 'T' && dow[1] == 'h') // Thursday | |
wday = 4; | |
if (dow[0] == 'F') // Friday | |
wday = 5; | |
if (dow[0] == 'S' && dow[1] == 'a') // Saturday | |
wday = 6; | |
if (dow[0] == 'S' && dow[1] == 'u') // Sunday | |
wday = 0; | |
/* Week */ | |
weeknum = atoi(week); | |
days_to_add = weeknum * 7 + wday; | |
/* Week '0' begins 18/09/2011 */ | |
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; | |
/* Normalise date and save in dd/mm/yyyy format */ | |
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