Skip to content

Instantly share code, notes, and snippets.

@pearswj
Created January 18, 2012 14:54

Revisions

  1. pearswj revised this gist Jan 24, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions shef2csv.c
    Original file line number Diff line number Diff line change
    @@ -39,16 +39,16 @@ int main ()
    date[10] = '\0'; // date string terminated with null character

    /* print header row */
    printf("Subject,Start Date,Start Time,End Time,Location,Description\n");
    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 for %s - %s with %s at %s\"\n",line[0], line[2], date, line[4],
    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]);
    }

  2. pearswj revised this gist Jan 19, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions shef2csv.c
    Original file line number Diff line number Diff line change
    @@ -48,8 +48,8 @@ int main ()
    /* 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\","
    "\"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]);
    "\"%s for %s - %s with %s at %s\"\n",line[0], line[2], date, line[4],
    line[5], line[7], line[8], line[2], line[0], line[1], line[9], line[7]);
    }

    return 0;
  3. pearswj revised this gist Jan 19, 2012. 1 changed file with 44 additions and 19 deletions.
    63 changes: 44 additions & 19 deletions shef2csv.c
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,22 @@
    /* for converting tab separated UoS module timetable data to Google .csv */

    /* Notes: */
    // - issue with week number ranges. Duplicate lines to remove ranges before running.
    /* ---------------------------------------------------------------------------------
    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>
    @@ -11,25 +26,30 @@
    #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;
    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[10] = '\0'; // date string terminated with null character

    /* print header row */
    printf("Subject,Start Date,Start Time,End Time,Location,Description\n");

    while ((len = getline(line[j], MAXLINE)) > 0)
    if (j == 0) {
    if (j == 0) { // i.e. a full line has been read...
    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]);

    /* 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\","
    "\"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;
    @@ -44,13 +64,13 @@ int getline(char s[], int lim)
    for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n' && c!='\t'; ++i)
    s[i] = c;

    if (c == '\t')
    if (c == '\t') // begin next string after tab entry
    ++j;

    if (c == '\n')
    if (c == '\n') // reset string counter on newline entry
    j = 0;

    s[i] = '\0';
    s[i] = '\0'; // terminate string

    return i;
    }
    @@ -63,24 +83,28 @@ void getdate1(char dow[], char week[], char date[])
    struct tm str_time;
    time_t time_of_day;

    if (dow[0] == 'M')
    /* Day */
    if (dow[0] == 'M') // Monday
    wday = 1;
    if (dow[0] == 'T' && dow[1] == 'u')
    if (dow[0] == 'T' && dow[1] == 'u') // Tuesday
    wday = 2;
    if (dow[0] == 'W')
    if (dow[0] == 'W') // Wednesday
    wday = 3;
    if (dow[0] == 'T' && dow[1] == 'h')
    if (dow[0] == 'T' && dow[1] == 'h') // Thursday
    wday = 4;
    if (dow[0] == 'F')
    if (dow[0] == 'F') // Friday
    wday = 5;
    if (dow[0] == 'S' && dow[1] == 'a')
    if (dow[0] == 'S' && dow[1] == 'a') // Saturday
    wday = 6;
    if (dow[0] == 'S' && dow[1] == 'u')
    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;
    @@ -89,6 +113,7 @@ void getdate1(char dow[], char week[], char date[])
    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);
  4. pearswj revised this gist Jan 18, 2012. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions shef2csv.c
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,8 @@
    /* for converting tab separated UoS module timetable data to Google .csv */

    /* To Do:
    - issue with week number ranges. Duplicate lines to remove ranges before running.
    - add week numbers with "date commencing" in 2d array. Write search function
    to find matching week number and date.
    - do same with dow. Add dow number (Sunday = 0) to date.
    */
    /* Notes: */
    // - issue with week number ranges. Duplicate lines to remove ranges before running.


    #include <stdio.h>
    #include <time.h>
  5. pearswj revised this gist Jan 18, 2012. 1 changed file with 10 additions and 19 deletions.
    29 changes: 10 additions & 19 deletions shef2csv.c
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    /* for converting tab separated UoS module timetable data to Google .csv */

    /*
    Notes:
    /* To Do:
    - issue with week number ranges. Duplicate lines to remove ranges before running.
    */
    - add week numbers with "date commencing" in 2d array. Write search function
    to find matching week number and date.
    - do same with dow. Add dow number (Sunday = 0) to date.
    */

    #include <stdio.h>
    #include <time.h>
    @@ -22,7 +24,8 @@ int main ()
    {
    int len;
    char line[10][MAXLINE];
    char date[10];
    char date[11];
    date[10] = '\0';

    printf("Subject,Start Date,Start Time,End Time,Location,Description\n");

    @@ -55,25 +58,14 @@ int getline(char s[], int lim)
    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;
    }*/

    /* 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;

    //printf("%s\n", dow);


    if (dow[0] == 'M')
    wday = 1;
    if (dow[0] == 'T' && dow[1] == 'u')
    @@ -102,6 +94,5 @@ void getdate1(char dow[], char week[], char date[])

    time_of_day = mktime(&str_time);
    newtime = localtime(&time_of_day);
    strftime(date,10,"%d/%m/%Y\n",newtime);
    printf("%s\n", date);
    strftime(date,10,"%d/%m/%Y",newtime);
    }
  6. pearswj created this gist Jan 18, 2012.
    107 changes: 107 additions & 0 deletions shef2csv.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    /* 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);
    }