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