project_files/hwc/rtl/misc.c
author unc0rr
Mon, 10 Feb 2014 00:43:03 +0400
changeset 10127 7f29a65aa1e4
parent 10015 4feced261c68
child 10128 0f6878b5395a
permissions -rw-r--r--
It compiles \o/ Doesn't link yet though, need to implement new rtl functions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10015
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     1
#include "misc.h"
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     2
#include <stdio.h>
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     3
#include <stdarg.h>
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     4
#include <string.h>
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     5
#include <assert.h>
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     6
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     7
char strbuf[512];
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     8
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
     9
void fpcrtl_assert(int i)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    10
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    11
    if(!i){
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    12
        assert(0);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    13
    }
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    14
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    15
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    16
// EFFECTS: return the nearest integer of the given number
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    17
int fpcrtl_round(double number)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    18
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    19
    return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    20
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    21
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    22
void fpcrtl_printf(const char* format, ...)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    23
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    24
#ifdef FPCRTL_DEBUG
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    25
    va_list args;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    26
    va_start (args, format);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    27
    vprintf (format, args);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    28
    va_end (args);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    29
#endif
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    30
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    31
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    32
//
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    33
//void fpcrtl_check_string(string255 str)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    34
//{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    35
//#ifdef FPCRTL_DEBUG
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    36
//    int len = strlen(str.str);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    37
//    if(len != str.len){
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    38
//        printf("String %s internal inconsistency error. Length should be %d but actually is %d.\n", str.str, len, str.len);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    39
//    }
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    40
//    //assert(len == str.len);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    41
//#endif
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    42
//}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    43
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    44
string255 fpcrtl_strconcat(string255 str1, string255 str2)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    45
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    46
    //printf("str1 = %d, %d\n", str1.len, strlen(str1.str));
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    47
    //printf("str2 = %d, %d\n", str2.len, strlen(str2.str));
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    48
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    49
#ifdef FPCRTL_DEBUG
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    50
    if(str1.len + (int)(str2.len) > 255){
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    51
        printf("String overflow\n");
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    52
        printf("str1(%d): %s\nstr2(%d): %s\n", str1.len, str1.str, str2.len, str2.str);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    53
        printf("String will be truncated.\n");
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    54
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    55
        strbuf[0] = 0;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    56
        strcpy(strbuf, str1.str);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    57
        strcat(strbuf, str2.str);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    58
        memcpy(str1.str, strbuf, 255);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    59
        str1.str[254] = 0;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    60
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    61
        return str1;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    62
    }
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    63
#endif
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    64
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    65
    memcpy(&(str1.str[str1.len]), str2.str, str2.len);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    66
    str1.str[str1.len + str2.len] = 0;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    67
    str1.len += str2.len;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    68
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    69
    return str1;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    70
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    71
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    72
string255 fpcrtl_strappend(string255 s, char c)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    73
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    74
    s.str[s.len] = c;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    75
    s.str[s.len + 1] = 0;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    76
    s.len ++;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    77
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    78
    return s;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    79
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    80
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    81
string255 fpcrtl_strprepend(char c, string255 s)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    82
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    83
    FIX_STRING(s);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    84
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    85
    memmove(s.str + 1, s.str, s.len + 1); // also move '/0'
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    86
    s.str[0] = c;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    87
    s.len++;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    88
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    89
    return s;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    90
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    91
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    92
string255 fpcrtl_chrconcat(char a, char b)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    93
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    94
    string255 result;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    95
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    96
    result.len = 2;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    97
    result.str[0] = a;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    98
    result.str[1] = b;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
    99
    result.str[2] = 0;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   100
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   101
    return result;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   102
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   103
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   104
bool fpcrtl_strcompare(string255 str1, string255 str2)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   105
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   106
    //printf("str1 = %d, %d\n", str1.len, strlen(str1.str));
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   107
    //printf("str2 = %d, %d\n", str2.len, strlen(str2.str));
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   108
    FIX_STRING(str1);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   109
    FIX_STRING(str2);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   110
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   111
    if(strcmp(str1.str, str2.str) == 0){
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   112
        return true;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   113
    }
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   114
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   115
    return false;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   116
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   117
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   118
bool fpcrtl_strcomparec(string255 a, char b)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   119
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   120
    FIX_STRING(a);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   121
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   122
    if(a.len == 1 && a.str[0] == b){
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   123
        return true;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   124
    }
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   125
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   126
    return false;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   127
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   128
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   129
bool fpcrtl_strncompare(string255 a, string255 b)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   130
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   131
    return !fpcrtl_strcompare(a, b);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   132
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   133
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   134
//char* fpcrtl_pchar(string255 s)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   135
//{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   136
//    return s.str;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   137
//}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   138
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   139
string255 fpcrtl_pchar2str(char *s)
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   140
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   141
    string255 result;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   142
    int t = strlen(s);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   143
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   144
    if(t > 255){
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   145
        printf("pchar2str, length > 255\n");
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   146
        assert(0);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   147
    }
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   148
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   149
    result.len = t;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   150
    memcpy(result.str, s, t);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   151
    result.str[t] = 0;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   152
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   153
    return result;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   154
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   155
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   156
string255 fpcrtl_make_string(const char* s) {
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   157
    string255 result;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   158
    strcpy(result.str, s);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   159
    result.len = strlen(s);
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   160
    return result;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   161
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   162
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   163
#ifdef EMSCRIPTEN
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   164
GLenum glewInit()
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   165
{
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   166
    return GLEW_OK;
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   167
}
4feced261c68 partial merge of the webgl branch
koda
parents: 7983
diff changeset
   168
#endif