project_files/hwc/rtl/system.c
author Wuzzy <Wuzzy2@mail.ru>
Tue, 23 Apr 2019 15:32:04 +0200
changeset 14833 8ed0c3761640
parent 14254 06f2dc4deab2
permissions -rw-r--r--
Change theme to Nature if changing from background-only or hidden theme to a generated map type
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>
11660
e15eb10f1703 Work around emscripten not finding math.h
koda
parents: 10910
diff changeset
     6
#include "pmath.h"
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
     7
8053
koda
parents: 8047
diff changeset
     8
#ifndef M_PI
koda
parents: 8047
diff changeset
     9
// some math.h do not have M_PI macros
koda
parents: 8047
diff changeset
    10
# define M_PI    3.14159265358979323846  /* pi */
koda
parents: 8047
diff changeset
    11
# define M_PI_2  1.57079632679489661923  /* pi/2 */
koda
parents: 8047
diff changeset
    12
# define M_PI_4  0.78539816339744830962  /* pi/4 */
koda
parents: 8047
diff changeset
    13
# define M_PIl   3.1415926535897932384626433832795029L  /* pi */
koda
parents: 8047
diff changeset
    14
# define M_PI_2l 1.5707963267948966192313216916397514L  /* pi/2 */
koda
parents: 8047
diff changeset
    15
# define M_PI_4l 0.7853981633974483096156608458198757L  /* pi/4 */
koda
parents: 8047
diff changeset
    16
#endif
koda
parents: 8047
diff changeset
    17
koda
parents: 8047
diff changeset
    18
double pi = M_PI;
koda
parents: 8047
diff changeset
    19
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    20
int paramCount;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    21
string255 params[MAX_PARAMS];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    22
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    23
string255 fpcrtl_copy(string255 s, Integer index, Integer count) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    24
    string255 result = STRINIT("");
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    25
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    26
    if (count < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    27
        return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    28
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    29
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    30
    if (index < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    31
        index = 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    32
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    33
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    34
    if (index > s.len) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    35
        return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    36
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    37
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    38
    if (index + count > s.len + 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    39
        count = s.len + 1 - index;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    40
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    41
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    42
    memcpy(result.str, s.str + index - 1, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    43
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    44
    result.len = count;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    45
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    46
    return result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    47
}
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    48
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    49
astring fpcrtl_copyA(astring s, Integer index, Integer count) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    50
    astring result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    51
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    52
    result.len = 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    53
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    54
    if (count < 1) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    55
        return result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    56
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    57
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    58
    if (index < 1) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    59
        index = 1;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    60
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    61
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    62
    if (index > s.len) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    63
        return result;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    64
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    65
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    66
    if (index + count > s.len + 1) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    67
        count = s.len + 1 - index;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    68
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    69
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
    70
    memcpy(result.s + 1, s.s + index, count);
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
    71
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    72
    result.len = count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    73
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    74
    return result;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    75
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
    76
13925
7173b702e8db tag both as overloadable - seems to satisfy clang 3.4
nemo
parents: 13885
diff changeset
    77
void __attribute__((overloadable)) fpcrtl_insert__vars(string255 *src, string255 *dst, SizeInt index) {
10838
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    78
    int num_insert;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    79
    int num_shift;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    80
    int num_preshift;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    81
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    82
    // nothing to do if empty string is inserted or index invalid
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    83
    if ((src->len == 0) || (index < 1) || (index > 255)) {
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    84
        return;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    85
    }
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    86
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    87
    num_insert = src->len;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    88
    // number of chars from start of destination string to end of insertion
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    89
    num_preshift = index - 1 + num_insert;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    90
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    91
    // don't overflow on insert
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    92
    if (num_preshift > 255) {
13885
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
    93
        num_preshift = 255;
10838
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    94
        num_insert = 255 - (index - 1);
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    95
        num_shift = 0;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    96
    }
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    97
    // shift trailing chars
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    98
    else {
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
    99
        // number of bytes to be shifted
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   100
        num_shift = dst->len - (index - 1);
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   101
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   102
        if (num_shift > 0) {
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   103
            // don't overflow when shifting
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   104
            if (num_shift + num_preshift > 255)
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   105
                num_shift = 255 - num_preshift;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   106
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   107
            // time to move some bytes!
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   108
            memmove(dst->str + num_preshift, dst->str + index - 1, num_shift);
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   109
        }
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   110
    }
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   111
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   112
    // actual byte insertion
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   113
    memmove(dst->str + index - 1, src->str, num_insert);
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   114
    // store new length
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   115
    dst->len = num_shift + num_preshift;
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   116
}
2abe883c9c21 teach pas2c how to insert() shortstrings
sheepluva
parents: 10625
diff changeset
   117
13885
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   118
void __attribute__((overloadable)) fpcrtl_insert__vars(astring *src, astring *dst, SizeInt index) {
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   119
    int num_insert;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   120
    int num_shift;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   121
    int num_preshift;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   122
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   123
    // nothing to do if empty string is inserted or index invalid
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   124
    if ((src->len == 0) || (index < 1) || (index > MAX_ANSISTRING_LENGTH)) {
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   125
        return;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   126
    }
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   127
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   128
    num_insert = src->len;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   129
    // number of chars from start of destination string to end of insertion
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   130
    num_preshift = index - 1 + num_insert;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   131
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   132
    // don't overflow on insert
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   133
    if (num_preshift > MAX_ANSISTRING_LENGTH) {
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   134
        num_preshift = MAX_ANSISTRING_LENGTH;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   135
        num_insert = MAX_ANSISTRING_LENGTH - (index - 1);
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   136
        num_shift = 0;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   137
    }
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   138
    // shift trailing chars
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   139
    else {
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   140
        // number of bytes to be shifted
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   141
        num_shift = dst->len - (index - 1);
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   142
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   143
        if (num_shift > 0) {
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   144
            // don't overflow when shifting
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   145
            if (num_shift + num_preshift > MAX_ANSISTRING_LENGTH)
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   146
                num_shift = MAX_ANSISTRING_LENGTH - num_preshift;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   147
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   148
            // time to move some bytes!
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   149
            memmove(dst->str + num_preshift, dst->str + index - 1, num_shift);
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   150
        }
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   151
    }
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   152
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   153
    // actual byte insertion
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   154
    memmove(dst->str + index - 1, src->str, num_insert);
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   155
    // store new length
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   156
    dst->len = num_shift + num_preshift;
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   157
}
5f819b90d479 - Fix insert() for strings in pas2c
unc0rr
parents: 11660
diff changeset
   158
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   159
void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   160
    // number of chars to be move
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   161
    int num_move;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   162
    int new_length;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   163
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   164
    if (index < 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   165
        // in fpc, if index < 1, the string won't be modified
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   166
        return;
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
    if(index > s->len){
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   170
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   171
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   172
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   173
    if (count > s->len - index + 1) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   174
        s->len = index - 1;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   175
        return;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   176
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   177
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   178
    num_move = s->len - index + 1 - count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   179
    new_length = s->len - count;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   180
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   181
    memmove(s->str + index - 1, s->str + index - 1 + count, num_move);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   182
    s->str[new_length] = 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   183
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   184
    s->len = new_length;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   185
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   186
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   187
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   188
void __attribute__((overloadable)) fpcrtl_delete__vars(astring *s, SizeInt index, SizeInt count) {
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   189
    // number of chars to be move
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   190
    int num_move;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   191
    int new_length;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   192
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   193
    if (index < 1) {
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   194
        // in fpc, if index < 1, the string won't be modified
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   195
        return;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   196
    }
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   197
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   198
    if(index > s->len){
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   199
        return;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   200
    }
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   201
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   202
    if (count > s->len - index + 1) {
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   203
        s->len = index - 1;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   204
        return;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   205
    }
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   206
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   207
    num_move = s->len - index + 1 - count;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   208
    new_length = s->len - count;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   209
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   210
    memmove(s->s + index, s->s + index + count, num_move);
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   211
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   212
    s->len = new_length;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   213
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   214
}
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   215
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   216
string255 fpcrtl_floatToStr(double n) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   217
    string255 t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   218
    sprintf(t.str, "%f", n);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   219
    t.len = strlen(t.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   220
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   221
    return t;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   222
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   223
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   224
void fpcrtl_move__vars(void *src, void *dst, SizeInt count) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   225
    memmove(dst, src, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   226
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   227
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   228
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
   229
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   230
    unsigned char* p;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   231
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   232
    if (str.len == 0) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   233
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   234
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   235
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   236
    FIX_STRING(str);
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   237
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   238
    p = strchr(str.str, c);
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   239
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   240
    if (p == NULL) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   241
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   242
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   243
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   244
    return p - (unsigned char*)&str.s;
7983
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
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   248
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   249
    unsigned char* p;
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   250
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   251
    if (str.len == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   252
        return 0;
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
    if (substr.len == 0) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   256
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   257
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   258
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   259
    FIX_STRING(substr);
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   260
    FIX_STRING(str);
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   261
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   262
    p = strstr(str.str, substr.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   263
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   264
    if (p == NULL) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   265
        return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   266
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   267
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   268
    return p - (unsigned char*)&str.s;
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   269
}
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   270
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   271
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
   272
    unsigned char* p;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   273
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   274
    if (str.len == 0) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   275
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   276
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   277
14164
0aeea29ef890 Introduce FIX_STRINGA, use it fpcrtl_pos
unc0rr
parents: 13925
diff changeset
   278
    FIX_STRINGA(str);
0aeea29ef890 Introduce FIX_STRINGA, use it fpcrtl_pos
unc0rr
parents: 13925
diff changeset
   279
14254
06f2dc4deab2 Remove erroneous code duplicating FIX_STRING functionality, adjust ansistring array size
unc0rr
parents: 14195
diff changeset
   280
    p = strchr(str.str, c);
10134
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   281
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   282
    if (p == NULL) {
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   283
        return 0;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   284
    }
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   285
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   286
    return p - (unsigned char*)&str.s;
81d733899e06 Use strchr instead of strstr with specially constructed string255 struct (not tested)
unc0rr
parents: 10131
diff changeset
   287
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   288
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   289
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   290
Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   291
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   292
    unsigned char* p;
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   293
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   294
    if (str.len == 0) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   295
        return 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   296
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   297
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   298
    if (substr.len == 0) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   299
        return 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   300
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   301
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   302
    FIX_STRING(substr);
14164
0aeea29ef890 Introduce FIX_STRINGA, use it fpcrtl_pos
unc0rr
parents: 13925
diff changeset
   303
    FIX_STRINGA(str);
0aeea29ef890 Introduce FIX_STRINGA, use it fpcrtl_pos
unc0rr
parents: 13925
diff changeset
   304
14254
06f2dc4deab2 Remove erroneous code duplicating FIX_STRING functionality, adjust ansistring array size
unc0rr
parents: 14195
diff changeset
   305
    p = strstr(str.str, substr.str);
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   306
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   307
    if (p == NULL) {
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   308
        return 0;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   309
    }
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   310
10131
4b4a043111f4 - pas2c recognizes typecasts in initialization expressions
unc0rr
parents: 10128
diff changeset
   311
    return p - (unsigned char *)&str.s;
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   312
}
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   313
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   314
Integer fpcrtl_length(string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   315
    return s.len;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   316
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   317
10128
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   318
Integer fpcrtl_lengthA(astring s)
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   319
{
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   320
    return s.len;
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   321
}
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   322
0f6878b5395a Implement needed rtl functions
unc0rr
parents: 8850
diff changeset
   323
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   324
string255 fpcrtl_lowerCase(string255 s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   325
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   326
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   327
    for (i = 0; i < s.len; i++) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   328
        if (s.str[i] >= 'A' && s.str[i] <= 'Z') {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   329
            s.str[i] += 'a' - 'A';
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   330
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   331
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   332
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   333
    return s;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   334
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   335
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   336
void fpcrtl_fillChar__vars(void *x, SizeInt count, Byte value) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   337
    memset(x, value, count);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   338
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   339
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   340
void fpcrtl_new__vars(void **p, int size) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   341
    *p = malloc(size);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   342
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   343
14173
5c6f947c342c Pas2C: Fix data types of Trunc and Ceil
Wuzzy <Wuzzy2@mail.ru>
parents: 14164
diff changeset
   344
Int64 fpcrtl_trunc(extended n) {
5c6f947c342c Pas2C: Fix data types of Trunc and Ceil
Wuzzy <Wuzzy2@mail.ru>
parents: 14164
diff changeset
   345
    return (Int64) n;
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   346
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   347
10910
df11dea74701 Add ceil() function to fpcrtl of pas2c
unc0rr
parents: 10840
diff changeset
   348
Integer fpcrtl_ceil(extended n) {
14173
5c6f947c342c Pas2C: Fix data types of Trunc and Ceil
Wuzzy <Wuzzy2@mail.ru>
parents: 14164
diff changeset
   349
    return (Integer) (ceil(n));
10910
df11dea74701 Add ceil() function to fpcrtl of pas2c
unc0rr
parents: 10840
diff changeset
   350
}
df11dea74701 Add ceil() function to fpcrtl of pas2c
unc0rr
parents: 10840
diff changeset
   351
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   352
LongInt str_to_int(char *src)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   353
{
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   354
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   355
    int len = strlen(src);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   356
    char *end;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   357
    for(i = 0; i < len; i++)
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   358
    {
14195
6f76fe84fdee Backed out changeset b15014125f67
Wuzzy <Wuzzy2@mail.ru>
parents: 14194
diff changeset
   359
        if(src[i] == '$'){
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   360
            // hex
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   361
            return strtol(src + i + 1, &end, 16);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   362
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   363
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   364
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   365
    // decimal
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   366
    return atoi(src);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   367
}
8850
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   368
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   369
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
   370
{
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   371
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   372
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   373
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   374
8850
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   375
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
   376
{
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   377
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   378
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   379
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   380
8850
ae8a957c69fd engine to c now compiles with some manual intervention (as of bug 596)
koda
parents: 8053
diff changeset
   381
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
   382
{
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   383
    FIX_STRING(s);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   384
    *a = str_to_int(s.str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   385
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   386
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   387
LongInt fpcrtl_random(LongInt l) {
10625
125e120165aa flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents: 10134
diff changeset
   388
    // random(0) is undefined in docs but effectively returns 0 in free pascal
125e120165aa flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents: 10134
diff changeset
   389
    if (l == 0) {
10840
6d1986411733 add newline after warning
sheepluva
parents: 10838
diff changeset
   390
        printf("WARNING: random(0) called!\n");
10625
125e120165aa flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents: 10134
diff changeset
   391
        return 0;
125e120165aa flake FrameTicks value of 0 now indicades that the frame should not be changed
sheepluva
parents: 10134
diff changeset
   392
    }
7983
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   393
    return (LongInt) (rand() / (double) RAND_MAX * l);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   394
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   395
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   396
void __attribute__((overloadable)) fpcrtl_str__vars(float x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   397
    sprintf(s->str, "%f", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   398
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   399
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   400
void __attribute__((overloadable)) fpcrtl_str__vars(double x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   401
    sprintf(s->str, "%f", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   402
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   403
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   404
void __attribute__((overloadable)) fpcrtl_str__vars(uint8_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   405
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   406
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   407
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   408
void __attribute__((overloadable)) fpcrtl_str__vars(int8_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   409
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   410
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   411
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   412
void __attribute__((overloadable)) fpcrtl_str__vars(uint16_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   413
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   414
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   415
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   416
void __attribute__((overloadable)) fpcrtl_str__vars(int16_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   417
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   418
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   419
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   420
void __attribute__((overloadable)) fpcrtl_str__vars(uint32_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   421
    sprintf(s->str, "%u", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   422
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   423
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   424
void __attribute__((overloadable)) fpcrtl_str__vars(int32_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   425
    sprintf(s->str, "%d", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   426
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   427
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   428
void __attribute__((overloadable)) fpcrtl_str__vars(uint64_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   429
    sprintf(s->str, "%llu", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   430
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   431
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   432
void __attribute__((overloadable)) fpcrtl_str__vars(int64_t x, string255 *s) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   433
    sprintf(s->str, "%lld", x);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   434
    s->len = strlen(s->str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   435
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   436
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   437
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   438
 * XXX No protection currently!
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   439
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   440
void fpcrtl_interlockedIncrement__vars(int *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   441
    (*i)++;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   442
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   443
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   444
void fpcrtl_interlockedDecrement__vars(int *i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   445
    (*i)--;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   446
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   447
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   448
/*
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   449
 * This function should be called when entering main
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   450
 */
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   451
void fpcrtl_init(int argc, char** argv) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   452
    int i;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   453
    paramCount = argc;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   454
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   455
    printf("ARGC = %d\n", paramCount);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   456
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   457
    for (i = 0; i < argc; i++) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   458
        if (strlen(argv[i]) > 255) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   459
            assert(0);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   460
        }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   461
        strcpy(params[i].str, argv[i]);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   462
        params[i].len = strlen(params[i].str);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   463
    }
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   464
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   465
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   466
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   467
int fpcrtl_paramCount() {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   468
    return paramCount - 1; // ignore the first one
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   469
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   470
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   471
string255 fpcrtl_paramStr(int i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   472
    return params[i];
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   473
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   474
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   475
int fpcrtl_UTF8ToUnicode(PWideChar dest, PChar src, SizeInt maxLen) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   476
    //return swprintf(dest, maxLen, L"%hs", "src"); //doesn't work in emscripten
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   477
    return 0;
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   478
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   479
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   480
uint32_t __attribute__((overloadable)) fpcrtl_lo(uint64_t i) {
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   481
    return (i & 0xFFFFFFFF);
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   482
}
02f36c3e7f6c add xymeng's rtl port
koda
parents:
diff changeset
   483