Skip to content

Instantly share code, notes, and snippets.

@Cynede
Created December 4, 2012 09:46
Show Gist options
  • Save Cynede/4202250 to your computer and use it in GitHub Desktop.
Save Cynede/4202250 to your computer and use it in GitHub Desktop.
//fails
private void button1_Click(object sender, RoutedEventArgs e) {
string[] pony = new string[] {"hello", "world"};
if (t.n_write(pony, 2) == -1) {
MessageBox.Show(t.errorMessage);
}
@Cynede
Copy link
Author

Cynede commented Dec 4, 2012

// works

let write p =
                (* TESTING WRITE *)
    if t.n_write([|"hello"; "world"|],2) = 0 then
        printfn "done"
    else
        printfn "error"

@Cynede
Copy link
Author

Cynede commented Dec 4, 2012

// testing

    int  todo::n_write(cli::array<System::String ^> ^ input, int argc) {
        int result;
        argv = (char**)malloc(sizeof(char*) * (3));
        argv[0] = "";
        argv[1] = "test";
        try {
            result = todo_write(argv, 2); //argc + 1);
            return result;
            }
        catch(char* error) {
            errorMessage = fromchar(error);
            return -1;
            }

@Cynede
Copy link
Author

Cynede commented Dec 4, 2012

int todo_write(char** argv, int argc) {
    char first = 0;
    int last = 0;
    int argi;
    char* text;
    char* ending;
    int useending = 0;
    int limit = 200;
    if (prelude() == -1) return -1;
    ///<Summary>
    ///Getting options from local database
    ///<Summary>
#ifdef WIN32
    sprintf_s(queries[ind++], 255, "SELECT option, text FROM OPTIONS WHERE option = 12 or option = 13");
#else
    sprintf(queries[ind++], "SELECT option, text FROM OPTIONS WHERE option = 12 or option = 13");
#endif
    retval = sqlite3_prepare_v2(handle, queries[ind - 1], -1, &stmt, 0);
    if (retval) {
        printf("Sync data Failed, run initdb first\n\r");
        return -1;
        }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        if (strcmp((const char*)sqlite3_column_text(stmt, 0), "13") == 0) {
            ending = (char*)calloc(200, sizeof(char));
#ifdef WIN32
            sprintf_s(ending, 200, "%s", sqlite3_column_text(stmt, 1));
#else
            sprintf(ending, "%s", sqlite3_column_text(stmt, 1));
#endif
            }
        else if (strcmp((const char*)sqlite3_column_text(stmt, 0), "12") == 0) {
            useending = atoi((const char*)sqlite3_column_text(stmt, 1));
            }
        }
    ///<Summary>
    ///Writing to local database
    ///<Summary>
#ifdef WIN32
    sprintf_s(queries[ind++], 255, "SELECT COALESCE(MAX(id),0) FROM TODO");
#else
    sprintf(queries[ind++], "SELECT COALESCE(MAX(id),0) FROM TODO");
#endif
    retval = sqlite3_prepare_v2(handle, queries[ind - 1], -1, &stmt, 0);
    if (retval) {
        printf("Inserting data to DB Failed, run initdb first\n\r");
        return -1;
        }
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        last = atoi((const char*)sqlite3_column_text(stmt, 0));
        }
    text = (char*)calloc(200, sizeof(char));
    if (useending == 1) {
        limit = 200 - strlen(ending);
        }
    for (argi = 1; argi < argc; argi++) {
        if (strlen(text) + strlen(argv[argi]) + sizeof(char)  >= (unsigned int)limit) {
            break;
            }
        else {
            if ((strcmp(argv[argi], "--motivate") == 0)) {
                useending = 1;
                }
            else if (strcmp(argv[argi], "--first") == 0) {
                first = 1;
                }
            else {
#ifdef WIN32
                strcat_s(text, 200, argv[argi]);
                strcat_s(text, 200, " ");
#else
                strcat(text, argv[argi]);
                strcat(text, " ");
#endif
                }
            }
        }
    if (useending == 1) {
#ifdef WIN32
        strcat_s(text, 200, ending);
#else
        strcat(text, ending);
#endif
        }
    if (first == 1) {
        sql("UPDATE TODO SET id = id + 1000000000");
        sql("UPDATE TODO SET id = id - (1000000000 - 1)");
#ifdef WIN32
        sprintf_s(queries[ind++], 255, "INSERT INTO TODO VALUES(0,'%s')", text);
#else
        sprintf(queries[ind++], "INSERT INTO TODO VALUES(0,'%s')", text);
#endif
        }
    else {
#ifdef WIN32
        sprintf_s(queries[ind++], 255, "INSERT INTO TODO VALUES(%d,'%s')", last + 1, text);
#else
        sprintf(queries[ind++], "INSERT INTO TODO VALUES(%d,'%s')", last + 1, text);
#endif
        }
    retval = sqlite3_exec(handle, queries[ind - 1], 0, 0, 0);
    if (retval) {
        printf("Task were not added! (shit happens)\n\r");
        return -1;
        }
    free(text);
    free(ending);
    timeUpdate(time(0));
    return 0;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment