project_files/hwc/rtl/system.c
author koda
Wed, 07 Nov 2012 18:04:27 +0000
branchwebgl
changeset 7983 02f36c3e7f6c
child 8047 25a4daa6473c
permissions -rw-r--r--
add xymeng's rtl port
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     1
#include "system.h"
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     2
#include <string.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     3
#include <stdio.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     4
#include <stdlib.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     5
#include <wchar.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     6
#include <math.h>
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     7
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     8
int paramCount;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     9
string255 params[MAX_PARAMS];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    10
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    11
double pi = M_PI;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    12
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    13
string255 fpcrtl_copy(string255 s, Integer index, Integer count) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    14
    string255 result = STRINIT("");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    15
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    16
    if (count < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    17
        return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    18
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    19
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    20
    if (index < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    21
        index = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    22
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    23
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    24
    if (index > s.len) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    25
        return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    26
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    27
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    28
    if (index + count > s.len + 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    29
        count = s.len + 1 - index;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    30
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    31
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    32
    memcpy(result.str, s.str + index - 1, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    33
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    34
    result.str[count] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    35
    result.len = count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    36
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    37
    return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    38
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    39
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    40
void fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    41
    // number of chars to be move
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    42
    int num_move;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    43
    int new_length;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    44
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    45
    string255 temp = *s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    46
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    47
    if (index < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    48
        // in fpc, if index < 1, the string won't be modified
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    49
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    50
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    51
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    52
    if(index > s->len){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    53
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    54
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    55
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    56
    if (count > s->len - index + 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    57
        s->str[index - 1] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    58
        s->len = index - 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    59
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    60
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    61
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    62
    num_move = s->len - index + 1 - count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    63
    new_length = s->len - count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    64
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    65
    memmove(s->str + index - 1, temp.str + index - 1 + count, num_move);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    66
    s->str[new_length] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    67
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    68
    s->len = new_length;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    69
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    70
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    71
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    72
string255 fpcrtl_floatToStr(double n) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    73
    string255 t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    74
    sprintf(t.str, "%f", n);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    75
    t.len = strlen(t.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    76
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    77
    return t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    78
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    79
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    80
void fpcrtl_move__vars(void *src, void *dst, SizeInt count) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    81
    memmove(dst, src, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    82
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    83
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    84
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, string255 str) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    85
    string255 t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    86
    t.len = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    87
    t.str[0] = c;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    88
    t.str[1] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    89
    return fpcrtl_pos(t, str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    90
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    91
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    92
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    93
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    94
    char* p;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    95
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    96
    FIX_STRING(substr);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    97
    FIX_STRING(str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    98
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    99
    if (str.len == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   100
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   101
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   102
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   103
    if (substr.len == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   104
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   105
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   106
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   107
    str.str[str.len] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   108
    substr.str[substr.len] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   109
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   110
    p = strstr(str.str, substr.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   111
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   112
    if (p == NULL) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   113
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   114
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   115
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   116
    return strlen(str.str) - strlen(p) + 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   117
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   118
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   119
Integer fpcrtl_length(string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   120
    return s.len;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   121
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   122
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   123
string255 fpcrtl_lowerCase(string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   124
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   125
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   126
    for (i = 0; i < s.len; i++) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   127
        if (s.str[i] >= 'A' && s.str[i] <= 'Z') {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   128
            s.str[i] += 'a' - 'A';
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   129
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   130
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   131
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   132
    return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   133
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   134
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   135
void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   136
    memset(x, value, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   137
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   138
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   139
void fpcrtl_new__vars(void **p, int size) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   140
    *p = malloc(size);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   141
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   142
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   143
Integer fpcrtl_trunc(extended n) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   144
    return (int) n;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   145
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   146
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   147
LongInt str_to_int(char *src)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   148
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   149
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   150
    int len = strlen(src);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   151
    char *end;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   152
    for(i = 0; i < len; i++)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   153
    {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   154
        if(src[i] == '$'){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   155
            // hex
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   156
            return strtol(src + i + 1, &end, 16);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   157
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   158
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   159
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   160
    // decimal
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   161
    return atoi(src);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   162
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   163
void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongInt *a,
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   164
        LongInt *c) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   165
    *c = 0; // no error
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   166
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   167
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   168
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   169
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   170
void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, Byte *a,
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   171
        LongInt *c) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   172
    *c = 0; // no error
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   173
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   174
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   175
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   176
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   177
void __attribute__((overloadable)) fpcrtl_val__vars(string255 s, LongWord *a,
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   178
        LongInt *c) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   179
    *c = 0; // no error
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   180
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   181
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   182
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   183
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   184
LongInt fpcrtl_random(LongInt l) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   185
    return (LongInt) (rand() / (double) RAND_MAX * l);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   186
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   187
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   188
void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   189
    sprintf(s->str, "%f", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   190
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   191
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   192
void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   193
    sprintf(s->str, "%f", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   194
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   195
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   196
void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   197
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   198
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   199
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   200
void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   201
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   202
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   203
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   204
void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   205
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   206
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   207
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   208
void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   209
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   210
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   211
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   212
void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   213
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   214
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   215
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   216
void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   217
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   218
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   219
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   220
void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   221
    sprintf(s->str, "%llu", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   222
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   223
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   224
void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   225
    sprintf(s->str, "%lld", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   226
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   227
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   228
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   229
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   230
 * XXX No protection currently!
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   231
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   232
void fpcrtl_interlockedIncrement__vars(int *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   233
    (*i)++;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   234
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   235
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   236
void fpcrtl_interlockedDecrement__vars(int *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   237
    (*i)--;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   238
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   239
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   240
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   241
 * This function should be called when entering main
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   242
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   243
void fpcrtl_init(int argc, char** argv) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   244
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   245
    paramCount = argc;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   246
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   247
    printf("ARGC = %d\n", paramCount);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   248
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   249
    for (i = 0; i < argc; i++) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   250
        if (strlen(argv[i]) > 255) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   251
            assert(0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   252
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   253
        strcpy(params[i].str, argv[i]);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   254
        params[i].len = strlen(params[i].str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   255
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   256
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   257
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   258
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   259
int fpcrtl_paramCount() {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   260
    return paramCount - 1; // ignore the first one
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   261
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   262
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   263
string255 fpcrtl_paramStr(int i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   264
    return params[i];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   265
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   266
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   267
int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   268
    //return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   269
    return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   270
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   271
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   272
uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   273
    return (i & 0xFFFFFFFF);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   274
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   275