project_files/hwc/rtl/system.c
author unc0rr
Thu, 26 Jun 2014 21:43:36 +0400
changeset 10342 16122539d2ea
parent 10134 81d733899e06
child 10625 125e120165aa
permissions -rw-r--r--
Fix build, and also make protocol a bit more consistent and flexible (only in docs though, to be implemented)
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
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    43
    result.len = count;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    44
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    45
    return result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    46
}
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    47
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    48
astring fpcrtl_copyA(astring s, Integer index, Integer count) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    49
    astring result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    50
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    51
    result.len = 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    52
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    53
    if (count < 1) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    54
        return result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    55
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    56
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    57
    if (index < 1) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    58
        index = 1;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    59
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    60
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    61
    if (index > s.len) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    62
        return result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    63
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    64
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    65
    if (index + count > s.len + 1) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    66
        count = s.len + 1 - index;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    67
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    68
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
    69
    memcpy(result.s + 1, s.s + index, count);
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    70
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    71
    result.len = count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    72
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    73
    return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    74
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    75
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
    76
void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    77
    // number of chars to be move
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    78
    int num_move;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    79
    int new_length;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    80
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    81
    if (index < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    82
        // in fpc, if index < 1, the string won't be modified
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    83
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    84
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    85
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    86
    if(index > s->len){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    87
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    88
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    89
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    90
    if (count > s->len - index + 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    91
        s->len = index - 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    92
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    93
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    94
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    95
    num_move = s->len - index + 1 - count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    96
    new_length = s->len - count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    97
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
    98
    memmove(s->str + index - 1, s->str + index - 1 + count, num_move);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    99
    s->str[new_length] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   100
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   101
    s->len = new_length;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   102
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   103
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   104
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   105
void __attribute__((overloadable)) fpcrtl_delete__vars(astring *s, SizeInt index, SizeInt count) {
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   106
    // number of chars to be move
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   107
    int num_move;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   108
    int new_length;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   109
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   110
    if (index < 1) {
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   111
        // in fpc, if index < 1, the string won't be modified
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   112
        return;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   113
    }
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   114
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   115
    if(index > s->len){
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   116
        return;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   117
    }
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   118
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   119
    if (count > s->len - index + 1) {
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   120
        s->len = index - 1;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   121
        return;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   122
    }
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   123
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   124
    num_move = s->len - index + 1 - count;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   125
    new_length = s->len - count;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   126
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   127
    memmove(s->s + index, s->s + index + count, num_move);
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   128
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   129
    s->len = new_length;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   130
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   131
}
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   132
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   133
string255 fpcrtl_floatToStr(double n) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   134
    string255 t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   135
    sprintf(t.str, "%f", n);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   136
    t.len = strlen(t.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   137
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   138
    return t;
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
void fpcrtl_move__vars(void *src, void *dst, SizeInt count) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   142
    memmove(dst, src, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   143
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   144
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   145
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, string255 str) {
10134
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   146
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   147
    unsigned char* p;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   148
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   149
    if (str.len == 0) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   150
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   151
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   152
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   153
    FIX_STRING(str);
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   154
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   155
    p = strchr(str.str, c);
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   156
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   157
    if (p == NULL) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   158
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   159
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   160
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   161
    return p - (unsigned char*)&str.s;
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   162
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   163
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   164
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   165
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   166
    unsigned char* p;
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   167
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   168
    if (str.len == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   169
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   170
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   171
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   172
    if (substr.len == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   173
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   174
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   175
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   176
    FIX_STRING(substr);
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   177
    FIX_STRING(str);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   178
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   179
    p = strstr(str.str, substr.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   180
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   181
    if (p == NULL) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   182
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   183
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   184
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   185
    return p - (unsigned char*)&str.s;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   186
}
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   187
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   188
Integer __attribute__((overloadable)) fpcrtl_pos(Char c, astring str) {
10134
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   189
    unsigned char* p;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   190
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   191
    if (str.len == 0) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   192
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   193
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   194
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   195
    p = strchr(str.s + 1, c);
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   196
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   197
    if (p == NULL) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   198
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   199
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   200
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   201
    return p - (unsigned char*)&str.s;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   202
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   203
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   204
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   205
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   206
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   207
    unsigned char* p;
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   208
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   209
    if (str.len == 0) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   210
        return 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   211
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   212
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   213
    if (substr.len == 0) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   214
        return 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   215
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   216
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   217
    FIX_STRING(substr);
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   218
    str.s[str.len] = 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   219
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   220
    p = strstr(str.s + 1, substr.str);
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   221
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   222
    if (p == NULL) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   223
        return 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   224
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   225
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   226
    return p - (unsigned char *)&str.s;
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   227
}
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   228
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   229
Integer fpcrtl_length(string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   230
    return s.len;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   231
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   232
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   233
Integer fpcrtl_lengthA(astring s)
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   234
{
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   235
    return s.len;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   236
}
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   237
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   238
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   239
string255 fpcrtl_lowerCase(string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   240
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   241
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   242
    for (i = 0; i < s.len; i++) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   243
        if (s.str[i] >= 'A' && s.str[i] <= 'Z') {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   244
            s.str[i] += 'a' - 'A';
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
    return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   249
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   250
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   251
void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   252
    memset(x, value, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   253
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   254
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   255
void fpcrtl_new__vars(void **p, int size) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   256
    *p = malloc(size);
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
Integer fpcrtl_trunc(extended n) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   260
    return (int) n;
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
LongInt str_to_int(char *src)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   264
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   265
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   266
    int len = strlen(src);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   267
    char *end;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   268
    for(i = 0; i < len; i++)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   269
    {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   270
        if(src[i] == '$'){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   271
            // hex
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   272
            return strtol(src + i + 1, &end, 16);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   273
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   274
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   275
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   276
    // decimal
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   277
    return atoi(src);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   278
}
8850
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   279
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   280
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
   281
{
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   282
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   283
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   284
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   285
8850
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   286
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
   287
{
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   288
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   289
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   290
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   291
8850
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   292
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
   293
{
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   294
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   295
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   296
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   297
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   298
LongInt fpcrtl_random(LongInt l) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   299
    return (LongInt) (rand() / (double) RAND_MAX * l);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   300
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   301
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   302
void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   303
    sprintf(s->str, "%f", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   304
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   305
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   306
void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   307
    sprintf(s->str, "%f", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   308
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   309
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   310
void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   311
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   312
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   313
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   314
void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   315
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   316
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   317
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   318
void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   319
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   320
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   321
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   322
void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   323
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   324
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   325
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   326
void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   327
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   328
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   329
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   330
void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   331
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   332
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   333
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   334
void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   335
    sprintf(s->str, "%llu", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   336
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   337
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   338
void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   339
    sprintf(s->str, "%lld", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   340
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   341
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   342
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   343
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   344
 * XXX No protection currently!
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   345
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   346
void fpcrtl_interlockedIncrement__vars(int *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   347
    (*i)++;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   348
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   349
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   350
void fpcrtl_interlockedDecrement__vars(int *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   351
    (*i)--;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   352
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   353
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   354
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   355
 * This function should be called when entering main
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   356
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   357
void fpcrtl_init(int argc, char** argv) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   358
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   359
    paramCount = argc;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   360
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   361
    printf("ARGC = %d\n", paramCount);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   362
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   363
    for (i = 0; i < argc; i++) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   364
        if (strlen(argv[i]) > 255) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   365
            assert(0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   366
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   367
        strcpy(params[i].str, argv[i]);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   368
        params[i].len = strlen(params[i].str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   369
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   370
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   371
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   372
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   373
int fpcrtl_paramCount() {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   374
    return paramCount - 1; // ignore the first one
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   375
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   376
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   377
string255 fpcrtl_paramStr(int i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   378
    return params[i];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   379
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   380
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   381
int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   382
    //return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   383
    return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   384
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   385
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   386
uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   387
    return (i & 0xFFFFFFFF);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   388
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   389