Implement needed rtl functions
authorunc0rr
Mon, 10 Feb 2014 23:02:49 +0400
changeset 10128 0f6878b5395a
parent 10127 7f29a65aa1e4
child 10129 cd2a64a1f4aa
Implement needed rtl functions
hedgewars/uGame.pas
hedgewars/uStore.pas
project_files/hwc/rtl/fpcrtl.h
project_files/hwc/rtl/misc.c
project_files/hwc/rtl/misc.h
project_files/hwc/rtl/pas2c.h
project_files/hwc/rtl/system.c
--- a/hedgewars/uGame.pas	Mon Feb 10 00:43:03 2014 +0400
+++ b/hedgewars/uGame.pas	Mon Feb 10 23:02:49 2014 +0400
@@ -27,7 +27,7 @@
     implementation
 ////////////////////
 uses uInputHandler, uTeams, uIO, uAI, uGears, uSound, uLocale, uCaptions,
-     uTypes, uVariables, uCommands, uConsts, uVisualGearsList
+     uTypes, uVariables, uCommands, uConsts, uVisualGearsList, uUtils
      {$IFDEF USE_TOUCH_INTERFACE}, uTouch{$ENDIF};
 
 procedure DoGameTick(Lag: LongInt);
--- a/hedgewars/uStore.pas	Mon Feb 10 00:43:03 2014 +0400
+++ b/hedgewars/uStore.pas	Mon Feb 10 23:02:49 2014 +0400
@@ -1328,7 +1328,10 @@
         tmpline2:= _S'';
         SplitByChar(tmpline, tmpline2, ':');
         if length(tmpline2) > 0 then
-            WriteInRect(tmpsurf, cFontBorder + 2, r2.y + r2.h, $ffc7c7c7, font, PChar(tmpline + ':'));
+            begin
+            tmpline:= tmpline + ':';
+            WriteInRect(tmpsurf, cFontBorder + 2, r2.y + r2.h, $ffc7c7c7, font, PChar(tmpline));
+            end;
         end
     end;
 
--- a/project_files/hwc/rtl/fpcrtl.h	Mon Feb 10 00:43:03 2014 +0400
+++ b/project_files/hwc/rtl/fpcrtl.h	Mon Feb 10 23:02:49 2014 +0400
@@ -191,6 +191,7 @@
 #define _pchar                              fpcrtl_pchar
 #define _strconcatA                         fpcrtl_strconcatA
 #define _strncompareA                       fpcrtl_strncompareA
+#define _strappendA                         fpcrtl_strappendA
 
 // hooks are implemented in javascript
 void start_hook(void);
--- a/project_files/hwc/rtl/misc.c	Mon Feb 10 00:43:03 2014 +0400
+++ b/project_files/hwc/rtl/misc.c	Mon Feb 10 23:02:49 2014 +0400
@@ -43,48 +43,54 @@
 
 string255 fpcrtl_strconcat(string255 str1, string255 str2)
 {
-    //printf("str1 = %d, %d\n", str1.len, strlen(str1.str));
-    //printf("str2 = %d, %d\n", str2.len, strlen(str2.str));
+    int newlen = str1.len + str2.len;
+    if(newlen > 255) newlen = 255;
 
-#ifdef FPCRTL_DEBUG
-    if(str1.len + (int)(str2.len) > 255){
-        printf("String overflow\n");
-        printf("str1(%d): %s\nstr2(%d): %s\n", str1.len, str1.str, str2.len, str2.str);
-        printf("String will be truncated.\n");
+    memcpy(&(str1.str[str1.len]), str2.str, newlen - str1.len);
+    str1.len = newlen;
 
-        strbuf[0] = 0;
-        strcpy(strbuf, str1.str);
-        strcat(strbuf, str2.str);
-        memcpy(str1.str, strbuf, 255);
-        str1.str[254] = 0;
+    return str1;
+}
 
-        return str1;
-    }
-#endif
+astring fpcrtl_strconcatA(astring str1, astring str2)
+{
+    int newlen = str1.len + str2.len;
+    if(newlen > MAX_ANSISTRING_LENGTH) newlen = MAX_ANSISTRING_LENGTH;
 
-    memcpy(&(str1.str[str1.len]), str2.str, str2.len);
-    str1.str[str1.len + str2.len] = 0;
-    str1.len += str2.len;
+    memcpy(&(str1.s[str1.len + 1]), str2.s[1], newlen - str1.len);
+    str1.len = newlen;
 
     return str1;
 }
 
 string255 fpcrtl_strappend(string255 s, char c)
 {
-    s.str[s.len] = c;
-    s.str[s.len + 1] = 0;
-    s.len ++;
+    if(s.len < 255)
+    {
+        s.s[s.len] = c;
+        ++s.len;
+    }
+
+    return s;
+}
+
+astring fpcrtl_strappendA(astring s, char c)
+{
+    if(s.len < MAX_ANSISTRING_LENGTH)
+    {
+        s.s[s.len] = c;
+        ++s.len;
+    }
 
     return s;
 }
 
 string255 fpcrtl_strprepend(char c, string255 s)
 {
-    FIX_STRING(s);
-
-    memmove(s.str + 1, s.str, s.len + 1); // also move '/0'
+    uint8_t newlen = s.len < 255 ? s.len + 1 : 255;
+    memmove(s.str + 1, s.str, newlen); // also move '/0'
     s.str[0] = c;
-    s.len++;
+    s.len = newlen;
 
     return s;
 }
@@ -96,19 +102,13 @@
     result.len = 2;
     result.str[0] = a;
     result.str[1] = b;
-    result.str[2] = 0;
 
     return result;
 }
 
 bool fpcrtl_strcompare(string255 str1, string255 str2)
 {
-    //printf("str1 = %d, %d\n", str1.len, strlen(str1.str));
-    //printf("str2 = %d, %d\n", str2.len, strlen(str2.str));
-    FIX_STRING(str1);
-    FIX_STRING(str2);
-
-    if(strcmp(str1.str, str2.str) == 0){
+    if(strncmp(str1.str, str2.str, 256) == 0){
         return true;
     }
 
@@ -117,8 +117,6 @@
 
 bool fpcrtl_strcomparec(string255 a, char b)
 {
-    FIX_STRING(a);
-
     if(a.len == 1 && a.str[0] == b){
         return true;
     }
@@ -131,35 +129,93 @@
     return !fpcrtl_strcompare(a, b);
 }
 
-//char* fpcrtl_pchar(string255 s)
-//{
-//    return s.str;
-//}
+bool fpcrtl_strncompareA(astring a, astring b)
+{
+    return (a.len == b.len) && (strncmp(a.s, b.s, MAX_ANSISTRING_LENGTH) == 0);
+}
 
-string255 fpcrtl_pchar2str(char *s)
+
+string255 fpcrtl_pchar2str(const char *s)
 {
     string255 result;
-    int t = strlen(s);
+    int rlen = strlen(s);
 
-    if(t > 255){
-        printf("pchar2str, length > 255\n");
-        assert(0);
+    if(rlen > 255){
+        rlen = 255;
     }
 
-    result.len = t;
-    memcpy(result.str, s, t);
-    result.str[t] = 0;
+    result.len = rlen;
+    memcpy(result.str, s, rlen);
+
+    return result;
+}
+
+
+string255 fpcrtl_make_string(const char* s) {
+    return fpcrtl_pchar2str(s);
+}
+
+
+astring fpcrtl_pchar2astr(const char *s)
+{
+    astring result;
+    int rlen = strlen(s);
+
+    if(rlen > MAX_ANSISTRING_LENGTH){
+        rlen = MAX_ANSISTRING_LENGTH;
+    }
+
+    result.len = rlen;
+    memcpy(result.s + 1, s, rlen);
 
     return result;
 }
 
-string255 fpcrtl_make_string(const char* s) {
+astring fpcrtl_str2astr(string255 s)
+{
+    astring result;
+
+    result.str255 = s;
+    result.len = s.len;
+
+    return result;
+}
+
+string255 fpcrtl_astr2str(astring s)
+{
     string255 result;
-    strcpy(result.str, s);
-    result.len = strlen(s);
+
+    result = s.str255;
+    result.len = s.len > 255 ? 255 : s.len;
+
     return result;
 }
 
+char __pcharBuf[256];
+
+char* fpcrtl__pchar__vars(string255 * s)
+{
+    if(s->len < 255)
+    {
+        s->s[s->len] = 0;
+        return &s->s[1];
+    } else
+    {
+        memcpy(__pcharBuf, s->s[1], 255);
+        __pcharBuf[255] = 0;
+        return &__pcharBuf;
+    }
+}
+
+char* fpcrtl__pcharA__vars(astring * s)
+{
+    if(s->len == MAX_ANSISTRING_LENGTH)
+        --s->len;
+
+    s->s[s->len] = 0;
+    return &s->s[1];
+}
+
 #ifdef EMSCRIPTEN
 GLenum glewInit()
 {
--- a/project_files/hwc/rtl/misc.h	Mon Feb 10 00:43:03 2014 +0400
+++ b/project_files/hwc/rtl/misc.h	Mon Feb 10 23:02:49 2014 +0400
@@ -18,10 +18,9 @@
 #define     macro_dispatcher_(func, nargs)          macro_dispatcher__(func, nargs)
 #define     macro_dispatcher__(func, nargs)         func ## nargs
 
-#define     FPCRTL_DEBUG
+//#define     FPCRTL_DEBUG
 
-#define     FIX_STRING(s)                           (s.str[s.len] = 0)
-
+#define     FIX_STRING(s)                           (s.str[s.len == 255 ? 254 : s.len] = 0)
 //#define fpcrtl_check_string(s)     do{ if(strlen((s).str) != (s).len){ \
 //                                        printf("String %s internal inconsistency error. Length should be %d but actually is %d.\n", (s).str, strlen((s).str), (s).len); \
 //                                        assert(0);\
@@ -41,6 +40,7 @@
 string255   fpcrtl_chrconcat(char a, char b);
 
 astring     fpcrtl_strconcatA(astring str1, astring str2);
+astring     fpcrtl_strappendA(astring s, char c);
 
 // return true if str1 == str2
 bool        fpcrtl_strcompare(string255 str1, string255 str2);
@@ -48,9 +48,11 @@
 bool        fpcrtl_strncompare(string255 a, string255 b);
 bool        fpcrtl_strncompareA(astring a, astring b);
 
-char*       fpcrtl__pchar(string255 s);
-string255   fpcrtl_pchar2str(char *s);
-astring     fpcrtl_pchar2astr(char *s);
+#define     fpcrtl__pchar(s)                    fpcrtl__pchar__vars(&(s))
+#define     fpcrtl__pcharA(s)                   fpcrtl__pcharA__vars(&(s))
+char*       fpcrtl__pchar__vars(string255 * s);
+string255   fpcrtl_pchar2str(const char *s);
+astring     fpcrtl_pchar2astr(const char *s);
 astring     fpcrtl_str2astr(string255 s);
 string255   fpcrtl_astr2str(astring s);
 #define     fpcrtl_TypeInfo                         sizeof // dummy
--- a/project_files/hwc/rtl/pas2c.h	Mon Feb 10 00:43:03 2014 +0400
+++ b/project_files/hwc/rtl/pas2c.h	Mon Feb 10 23:02:49 2014 +0400
@@ -7,7 +7,7 @@
 #include <math.h>
 
 #define MAX_PARAMS 64
-#define MAX_ANSISTRING_LENGTH 16384
+#define MAX_ANSISTRING_LENGTH 16383
 
 typedef union string255_
     {
@@ -28,7 +28,7 @@
         };
         struct {
             unsigned char _dummy2;
-            unsigned char s[MAX_ANSISTRING_LENGTH];
+            unsigned char s[MAX_ANSISTRING_LENGTH + 1];
         };
         struct {
             uint16_t len;
--- a/project_files/hwc/rtl/system.c	Mon Feb 10 00:43:03 2014 +0400
+++ b/project_files/hwc/rtl/system.c	Mon Feb 10 23:02:49 2014 +0400
@@ -40,7 +40,34 @@
 
     memcpy(result.str, s.str + index - 1, count);
 
-    result.str[count] = 0;
+    result.len = count;
+
+    return result;
+}
+
+astring fpcrtl_copyA(astring s, Integer index, Integer count) {
+    astring result;
+
+    result.len = 0;
+
+    if (count < 1) {
+        return result;
+    }
+
+    if (index < 1) {
+        index = 1;
+    }
+
+    if (index > s.len) {
+        return result;
+    }
+
+    if (index + count > s.len + 1) {
+        count = s.len + 1 - index;
+    }
+
+    memcpy(result.s + 1, s.s + index - 1, count);
+
     result.len = count;
 
     return result;
@@ -102,9 +129,6 @@
 
     char* p;
 
-    FIX_STRING(substr);
-    FIX_STRING(str);
-
     if (str.len == 0) {
         return 0;
     }
@@ -113,8 +137,8 @@
         return 0;
     }
 
-    str.str[str.len] = 0;
-    substr.str[substr.len] = 0;
+    FIX_STRING(substr);
+    FIX_STRING(str);
 
     p = strstr(str.str, substr.str);
 
@@ -125,10 +149,40 @@
     return strlen(str.str) - strlen(p) + 1;
 }
 
+Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) {
+
+    char* p;
+
+    if (str.len == 0) {
+        return 0;
+    }
+
+    if (substr.len == 0) {
+        return 0;
+    }
+
+    FIX_STRING(substr);
+    str.s[str.len] = 0;
+
+    p = strstr(str.s + 1, substr.str);
+
+    if (p == NULL) {
+        return 0;
+    }
+
+    return str.len - strlen(p) + 1;
+}
+
 Integer fpcrtl_length(string255 s) {
     return s.len;
 }
 
+Integer fpcrtl_lengthA(astring s)
+{
+    return s.len;
+}
+
+
 string255 fpcrtl_lowerCase(string255 s) {
     int i;