fix sdl char, zchar and zword confusion
authorborg323 <4010067-borg323@users.noreply.gitlab.com>
Tue, 2 Jul 2019 13:34:51 +0000 (16:34 +0300)
committerborg323 <4010067-borg323@users.noreply.gitlab.com>
Tue, 2 Jul 2019 13:34:51 +0000 (16:34 +0300)
src/sdl/generic.c
src/sdl/sf_video.c

index 8d0ef1986ba330a65d05fefa0afd17f94f9e5958..0a242b6c42a56ae79b953e0815a462ed411b7982 100644 (file)
@@ -227,6 +227,15 @@ int os_storyfile_tell(FILE * fp)
 
 }
 
+static void print_c_string (const char *s)
+{
+    zchar c;
+
+    while ((c = *s++) != 0) {
+           os_display_char (c);
+    }
+}
+
 /*
  * os_warn
  *
@@ -244,29 +253,72 @@ void os_warn (const char *s, ...)
     len = vsnprintf(buf, sizeof(buf), s, va);
     va_end(va);
     /* Solaris 2.6's cc complains if the below cast is missing */
-    os_display_string((zchar *)"\n\n");
+    print_c_string("\n\n");
     os_beep(BEEP_HIGH);
     os_set_text_style(BOLDFACE_STYLE);
-    os_display_string((zchar *)"Warning: ");
+    print_c_string("Warning: ");
     os_set_text_style(0);
-    os_display_string((zchar *)(len < 0 ? s : buf));
-    os_display_string((zchar *)"\n");
+    print_c_string((len < 0 ? s : buf));
+    print_c_string("\n");
     if (len < 0)
-        os_display_string((zchar *)"(formatting error)\n");
+        print_c_string("(formatting error)\n");
     else if (len >= sizeof(buf))
-        os_display_string((zchar *)"(truncated)\n");
+        print_c_string("(truncated)\n");
     new_line();
 }
 
+size_t zcharstrlen(zchar *str)
+{
+    size_t ret = 0;
+
+    while (str[ret] != 0)
+    {
+       ret++;
+    }
+    return ret;
+}
+
+zchar *zcharstrcpy(zchar *dest, zchar *src)
+{
+    size_t i;
+
+    for (i = 0; src[i] != '\0'; i++)
+       dest[i] = src[i];
+    dest[i] = 0;
+
+    return dest;
+}
+
+int zcharstrncmp(zchar *s1, zchar *s2, size_t n)
+{
+    zchar u1, u2;
+    while (n-- > 0)
+    {
+      u1 = *s1++;
+      u2 = *s2++;
+      if (u1 != u2)
+        return u1 - u2;
+      if (u1 == 0)
+        return 0;
+    }
+    return 0;
+}
+
+zchar *zcharstrdup(zchar *src) {
+    zchar *dest = malloc((zcharstrlen(src) + 1) * sizeof(zchar));
+    if (dest) zcharstrcpy(dest, src);
+    return dest;
+}
+
 /* These are useful for circular buffers.
  */
 #define RING_DEC( ptr, beg, end) (ptr > (beg) ? --ptr : (ptr = (end)))
 #define RING_INC( ptr, beg, end) (ptr < (end) ? ++ptr : (ptr = (beg)))
 
 #define MAX_HISTORY 256
-static char *history_buffer[MAX_HISTORY];
-static char **history_next = history_buffer; /* Next available slot. */
-static char **history_view = history_buffer; /* What the user is looking at. */
+static zchar *history_buffer[MAX_HISTORY];
+static zchar **history_next = history_buffer; /* Next available slot. */
+static zchar **history_view = history_buffer; /* What the user is looking at. */
 #define history_end (history_buffer + MAX_HISTORY - 1)
 
 
@@ -278,7 +330,7 @@ void gen_add_to_history(zchar *str)
 
     if (*history_next != NULL)
         free( *history_next);
-    *history_next = strdup((char *)str);
+    *history_next = zcharstrdup(str);
     RING_INC( history_next, history_buffer, history_end);
     history_view = history_next; /* Reset user frame after each line */
 
@@ -302,7 +354,7 @@ void gen_history_reset()
  */
 int gen_history_back(zchar *str, int searchlen, int maxlen)
 {
-    char **prev = history_view;
+    zchar **prev = history_view;
 
     do {
         RING_DEC(history_view, history_buffer, history_end);
@@ -312,10 +364,10 @@ int gen_history_back(zchar *str, int searchlen, int maxlen)
             history_view = prev;
             return 0;
         }
-    } while (strlen(*history_view) > (size_t)maxlen
+    } while (zcharstrlen(*history_view) > (size_t)maxlen
              || (searchlen != 0
-                 && strncmp((char *)str, *history_view, searchlen)));
-    strcpy((char *)str + searchlen, *history_view + searchlen);
+                 && zcharstrncmp(str, *history_view, searchlen)));
+    zcharstrcpy(str + searchlen, *history_view + searchlen);
     return 1;
 }
 
@@ -325,7 +377,7 @@ int gen_history_back(zchar *str, int searchlen, int maxlen)
  */
 int gen_history_forward(zchar *str, int searchlen, int maxlen)
 {
-    char **prev = history_view;
+    zchar **prev = history_view;
 
     do {
         RING_INC(history_view, history_buffer, history_end);
@@ -336,9 +388,9 @@ int gen_history_forward(zchar *str, int searchlen, int maxlen)
             history_view = prev;
             return 0;
         }
-    } while (strlen(*history_view) > (size_t) maxlen
+    } while (zcharstrlen(*history_view) > (size_t) maxlen
              || (searchlen != 0
-                 && strncmp((char *)str, *history_view, searchlen)));
-    strcpy((char *)str + searchlen, *history_view + searchlen);
+                 && zcharstrncmp(str, *history_view, searchlen)));
+    zcharstrcpy(str + searchlen, *history_view + searchlen);
     return 1;
 }
index d74c5480e3c5ac69b6206cebed9e8d153331ce9e..af0143ebabd69326600b9c97cd18135659dbb6ce 100644 (file)
@@ -853,7 +853,7 @@ zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued)
                     // Delete the character to the left of the cursor
                     if (pos > 0) {
                         memmove(buf + pos - 1, buf + pos,
-                                sizeof(zword) * (mywcslen(buf) - pos + 1));
+                                sizeof(zchar) * (mywcslen(buf) - pos + 1));
                         pos--;
                         sf_DrawInput(buf,pos,ptx,pty,width,true);
                     }
@@ -862,7 +862,7 @@ zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued)
                     // Delete the character to the right of the cursor
                     if (pos < mywcslen(buf)) {
                         memmove(buf + pos, buf + pos + 1,
-                                sizeof(zword) * (mywcslen(buf) - pos));
+                                sizeof(zchar) * (mywcslen(buf) - pos));
                         sf_DrawInput(buf,pos,ptx,pty,width,true);
                     }
                     continue;
@@ -923,7 +923,7 @@ zchar os_read_line(int max, zchar *buf, int timeout, int width, int continued)
                         //printf("l%d w%d p%d\n",len,width,pos);
                         // Only allow if the width limit is not exceeded
                         if (len <= width) {
-                            memmove(buf+pos+1,buf+pos,sizeof(zword)*(mywcslen(buf)-pos+1));
+                            memmove(buf+pos+1,buf+pos,sizeof(zchar)*(mywcslen(buf)-pos+1));
                             *(buf+pos) = c;
                             pos++;
                             sf_DrawInput(buf,pos,ptx,pty,width,true);
@@ -1068,7 +1068,7 @@ void os_more_prompt(void)
        x = ts->cx; y = ts->cy;
        h = ts->font->height(ts->font);
                // Show a [More] prompt
-       while (*p) os_display_char((zword)(*p++));
+       while (*p) os_display_char((zchar)(*p++));
 //             theWnd->WriteText(CResString(IDS_MORE));
 //     sf_drawcursor(true);
 //     sf_flushdisplay();