Exemple #1
0
string list_toString(list_t lst)
{
    strbuilder_t sb = strbuilder_init();
    strbuilder_append(&sb, "[");
    strbuilder_append(&sb, element_toString(lst.data[0]));
    for(uint16 i = 1; i < lst.size; i++)
    {
        strbuilder_append(&sb, ", ");
        strbuilder_append(&sb, element_toString(lst.data[i]));
    }
    strbuilder_append(&sb, "]");
    string msg = strbuilder_tostr(sb);
    strbuilder_destroy(&sb);
    return msg;
}
string initWriter()
{
	string vidmem = (string) 0xb8000;
    char oldmem[strlen(vidmem)];
    strcpy(oldmem, vidmem);

	paintScreen(screen_color);

	drawFrame(header_background, 0, 0, 80, 4);
	printAt("Q OS Text Editor\r\n", header_foreground, 1, 1);
	printAt("Simple Text Editor built for Q OS by Raph Hennessy & Plankp T",desc_foreground,1,2);

	drawBorder(screen_background, 0, 4, 80, sh - 1);

	cursorY = 5;
	cursorX = 1;
	updateCursor();

    bool inCmdMode = true;
    uint16 curX = 1, curY = 5;
    uint32 index = 0;
    strbuilder_t data = strbuilder_init();
    int k;
    while(true)
    {
        cursorX = curX % sw;
        cursorY = curY;
        updateCursor();

        cursorBoundsCheck(&curX, &curY, &index);
        printStatus(curX, curY, inCmdMode);

        if(inCmdMode)
        {
            k = waitUntilKey(7, 0x10 /*Q*/, 0x17 /*I*/, 0x18 /*O*/, 0x3A /*<CAPS>*/, 0x23 /*H*/, 0x26 /*L*/, 0x2D /*X*/);
            switch(k)
            {
            case 0x10:
                goto end;
            case 0x17:
                inCmdMode = false;
                break;
            case 0x18:
                appendln(&data, &curX, &curY, &index);
                inCmdMode = false;
                break;
            case 0x23:
                moveCursorLeft(&curX, &curY, &index);
                break;
            case 0x26:
                moveCursorRight(&curX, &curY, &index);
                break;
            case 0x2D:
                deleteCharAt(&curX, &curY, &index, &data);
                break;
            }
        } else {
            k = getKeycode() / KC_MAGIC_VAL;
            char charInput = ' ';
            switch(k)
            {
            case 0x01:
                inCmdMode = true;
                break;
            case 0x1C:
				cursorBoundsCheck(&curX, &curY, &index);
                appendln(&data, &curX, &curY, &index);
                break;
            case 0x48:
                curY--;
                curX = 1;
				cursorBoundsCheck(&curX, &curY, &index);
                break;
            case 0x4B:
                moveCursorLeft(&curX, &curY, &index);
                break;
            case 0x4D:
                moveCursorRight(&curX, &curY, &index);
                break;
            case 0x50:
                curY++;
                curX = 1;
                cursorBoundsCheck(&curX, &curY, &index);
                break;
            case 0x0E:
                deleteCharAt(&curX, &curY, &index, &data);
                break;
            default:
                if(k < 59 && k > 0)
                {
                    charInput = retCorrespChar(kbShiftChars[k], kbLowerChars[k]);
                    if(charInput == 0)
                    {
                        break;
                    }
                    insertCharAt(&curX, &curY, &index, &data, charInput);
                }
                break;
            }
        }
    }
end: // Sorry for the mom spaghetti code
    // Must be last line (before any prints)
	strcpy(vidmem, oldmem);
    string msg = strbuilder_tostr(data);
    if(msg == NULL)
    {
        msg = "";
    }
    strbuilder_destroy(&data);
    cursorX = 1;
	cursorY = 5;
    print(msg, black);
    return msg;
}
void test(string args)
{
    args = splitArg(args, 1);
    if(streql(args, "") || streql(args, "-H"))
    {
        print("\nThis file is in charge of testing the data types embedded in Q-OS.",black);
        print("\nAccepted Arguments:\n-list\tTests the list.c file\n-set \ttests the set.c file", black);
        print("\n-strb\ttests the strbuilder.c file\n-y   \tshould return the current year...",black);
    }
    else if(streql(args, "-LIST"))//For testing lists
    {
        newline();
        list_t test_list = list_init();
        test_list.autoShrink = true;

        for(uint8 i = 0; i < 4; i++)
        {
            list_add(&test_list, "1");
            list_add(&test_list, "2");
            list_add(&test_list, "3");
            list_add(&test_list, "4");
            list_add(&test_list, "5");
            list_add(&test_list, "6");
            list_add(&test_list, "7");
            list_add(&test_list, "8");
            list_add(&test_list, "9");
            list_add(&test_list, "10");
            list_add(&test_list, "11");
            list_add(&test_list, "12");
            list_add(&test_list, "13");
            list_add(&test_list, "14");
            list_add(&test_list, "15");
            list_add(&test_list, "16");
        }
        list_add(&test_list, "Pointless");

        println("Done sizing up", white);
        printint(test_list.capt, white);

        element_t t;
        for(uint8 i = 0; i < 64; i++)
        {
            t = list_shift(&test_list);
        }
        println("\nLast item deleted should be \"16\"", white);
        println(t.udata.strdata, white);
        println("\nDeleting all but element \"Pointless\"", white);
        for(uint8 i = 0; i < test_list.size; i++)
        {
            println(list_get(test_list, i), white);
        }
        println("Done resizing up", white);
        printint(test_list.capt, white);
        list_destroy(&test_list);
    }
    else if(streql(args,"-SET"))
    {
        set_t test_set = set_init();
        for(uint8 i = 0; i < 4; i++)
        {
            set_add(&test_set, "0");
            set_add(&test_set, "1");
            set_add(&test_set, "2");
            set_add(&test_set, "3");
            set_add(&test_set, "4");
            set_add(&test_set, "5");
            set_add(&test_set, "6");
            set_add(&test_set, "7");
            set_add(&test_set, "8");
            set_add(&test_set, "9");
            set_add(&test_set, "10");
            set_add(&test_set, "11");
            set_add(&test_set, "12");
            set_add(&test_set, "13");
            set_add(&test_set, "14");
            set_add(&test_set, "15");
            set_add(&test_set, "16");
            print("\nIteration: ", white);
            printint(i, white);
        }
        println("\n\nInsertion::Output should be 17", white);
        printint(test_set.size, white);

        set_t tmp = set_init();
        set_add(&tmp, "Union item");
        set_union(&test_set, &tmp);
        println("\n\nUnion::Output should be 18", white);
        printint(test_set.size, white);

        set_intersect(&test_set, &tmp);
        println("\n\nIntersect::Output should be 1", white);
        printint(test_set.size, white);

        println("\n\nPreparing for diff test", white);
        set_add(&test_set, "1");
        set_add(&test_set, "2");
        set_add(&test_set, "3");
        set_add(&tmp, "2");
        set_add(&tmp, "3");
        set_add(&tmp, "4");
        set_diff(&test_set, &tmp);
        println("Diff::Output should be 2", white);
        printint(test_set.size, white);

        set_destroy(&tmp);
        set_destroy(&test_set);
    }
    else if(streql(args, "-STRB"))
    {
        static const string bak = "Hello, world ";
        static const uint32 bln = 13;
        strbuilder_t test_strb = strbuilder_init();

        strbuilder_append(&test_strb, bak);
        strbuilder_append(&test_strb, "Hello, 2nd world");
        println("\nTesting backup text. Output should 1", red);
        printint(streql(bak, test_strb.prevTxt), green);
        println("\nOutput should be \"Hello, world Hello, 2nd world\"", red);
        println(strbuilder_tostr(test_strb), green);
        println("\nRemoving greeters from first world", red);
        strbuilder_delete(&test_strb, 0, bln);
        println("\nOutput should be \"Hello, 2nd world\"", red);
        println(strbuilder_tostr(test_strb), green);
        strbuilder_flip(&test_strb);
        println("\nOutput should be \"dlrow dn2 ,olleH\"", red);
        println(strbuilder_tostr(test_strb), green);
        list_t tmp = strbuilder_split(test_strb, " ");
        println("\nOutput should be last str split by spaces", red);
        for(uint8 i = 0; i < tmp.size; i++)
        {
            println(list_get(tmp, i), white);
        }
        list_destroy(&tmp);
        strbuilder_destroy(&test_strb);
    }
    else if(streql(args,"-Y"))
    {
       //getTime() test
       printint(getTime("year"),white);
    }
}