Skip to content

Instantly share code, notes, and snippets.

@pearswj
Created January 18, 2012 14:54
Show Gist options
  • Save pearswj/1633351 to your computer and use it in GitHub Desktop.
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.
/* 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[10];
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;
}
/* copy: copy 'from' into 'to'; assume 'to' is big enough */
/*void copy(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}*/
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;
//printf("%s\n", dow);
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\n",newtime);
printf("%s\n", date);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment