shithub: puzzles

Download patch

ref: 3276376d1be74b66970b88c3e941dcedf8d22474
parent: b3243d75043cf1d70beb88d2a36eaebfe85c2c3f
author: Simon Tatham <anakin@pobox.com>
date: Sun Oct 1 10:04:47 EDT 2017

Assorted char * -> const char * API changes.

I went through all the char * parameters and return values I could see
in puzzles.h by eye and spotted ones that surely ought to have been
const all along.

--- a/combi.c
+++ b/combi.c
@@ -79,7 +79,7 @@
 
 #include <stdio.h>
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     abort();
 }
--- a/devel.but
+++ b/devel.but
@@ -1961,7 +1961,8 @@
 \S{drawing-draw-text} \cw{draw_text()}
 
 \c void draw_text(drawing *dr, int x, int y, int fonttype,
-\c                int fontsize, int align, int colour, char *text);
+\c                int fontsize, int align, int colour,
+\c                const char *text);
 
 Draws text in the puzzle window.
 
@@ -2122,7 +2123,7 @@
 
 \S{drawing-status-bar} \cw{status_bar()}
 
-\c void status_bar(drawing *dr, char *text);
+\c void status_bar(drawing *dr, const char *text);
 
 Sets the text in the game's status bar to \c{text}. The text is copied
 from the supplied buffer, so the caller is free to deallocate or
@@ -2393,7 +2394,8 @@
 \S{drawingapi-draw-text} \cw{draw_text()}
 
 \c void (*draw_text)(void *handle, int x, int y, int fonttype,
-\c                   int fontsize, int align, int colour, char *text);
+\c                   int fontsize, int align, int colour,
+\c                   const char *text);
 
 This function behaves exactly like the back end \cw{draw_text()}
 function; see \k{drawing-draw-text}.
@@ -2496,7 +2498,7 @@
 
 \S{drawingapi-status-bar} \cw{status_bar()}
 
-\c void (*status_bar)(void *handle, char *text);
+\c void (*status_bar)(void *handle, const char *text);
 
 This function behaves exactly like the back end \cw{status_bar()}
 function; see \k{drawing-status-bar}.
@@ -3178,7 +3180,7 @@
 
 \H{midend-game-id} \cw{midend_game_id()}
 
-\c const char *midend_game_id(midend *me, char *id);
+\c const char *midend_game_id(midend *me, const char *id);
 
 Passes the mid-end a string game ID (of any of the valid forms
 \cq{params}, \cq{params:description} or \cq{params#seed}) which the
@@ -3507,7 +3509,7 @@
 
 \H{frontend-fatal} \cw{fatal()}
 
-\c void fatal(char *fmt, ...);
+\c void fatal(const char *fmt, ...);
 
 This is called by some utility functions if they encounter a
 genuinely fatal error such as running out of memory. It is a
--- a/drawing.c
+++ b/drawing.c
@@ -71,7 +71,7 @@
 }
 
 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
-               int align, int colour, char *text)
+               int align, int colour, const char *text)
 {
     dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align,
 		       colour, text);
@@ -190,7 +190,7 @@
     return NULL;		       /* placate optimiser */
 }
 
-void status_bar(drawing *dr, char *text)
+void status_bar(drawing *dr, const char *text)
 {
     char *rewritten;
 
--- a/emcc.c
+++ b/emcc.c
@@ -122,7 +122,7 @@
  * Fatal error, called in cases of complete despair such as when
  * malloc() has returned NULL.
  */
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     char buf[512];
     va_list ap;
@@ -136,7 +136,7 @@
     js_error_box(buf);
 }
 
-void debug_printf(char *fmt, ...)
+void debug_printf(const char *fmt, ...)
 {
     char buf[512];
     va_list ap;
@@ -384,7 +384,8 @@
 }
 
 static void js_draw_text(void *handle, int x, int y, int fonttype,
-                         int fontsize, int align, int colour, char *text)
+                         int fontsize, int align, int colour,
+                         const char *text)
 {
     char fontstyle[80];
     int halign;
@@ -515,7 +516,7 @@
     js_canvas_end_draw();
 }
 
-static void js_status_bar(void *handle, char *text)
+static void js_status_bar(void *handle, const char *text)
 {
     js_canvas_set_statusbar(text);
 }
--- a/gtk.c
+++ b/gtk.c
@@ -71,7 +71,7 @@
 #ifdef DEBUGGING
 static FILE *debug_fp = NULL;
 
-void dputs(char *buf)
+void dputs(const char *buf)
 {
     if (!debug_fp) {
         debug_fp = fopen("debug.log", "w");
@@ -85,7 +85,7 @@
     }
 }
 
-void debug_printf(char *fmt, ...)
+void debug_printf(const char *fmt, ...)
 {
     char buf[4096];
     va_list ap;
@@ -101,7 +101,7 @@
  * Error reporting functions used elsewhere.
  */
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     va_list ap;
 
@@ -264,7 +264,7 @@
 #endif
 }
 
-void gtk_status_bar(void *handle, char *text)
+void gtk_status_bar(void *handle, const char *text)
 {
     frontend *fe = (frontend *)handle;
 
@@ -1000,7 +1000,7 @@
 }
 
 void gtk_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
-		   int align, int colour, char *text)
+		   int align, int colour, const char *text)
 {
     frontend *fe = (frontend *)handle;
     int i;
--- a/midend.c
+++ b/midend.c
@@ -1320,7 +1320,8 @@
     me->game_id_change_notify_ctx = ctx;
 }
 
-void midend_supersede_game_desc(midend *me, char *desc, char *privdesc)
+void midend_supersede_game_desc(midend *me, const char *desc,
+                                const char *privdesc)
 {
     sfree(me->desc);
     sfree(me->privdesc);
@@ -1393,10 +1394,11 @@
     return NULL;
 }
 
-static const char *midend_game_id_int(midend *me, char *id, int defmode)
+static const char *midend_game_id_int(midend *me, const char *id, int defmode)
 {
     const char *error;
-    char *par, *desc, *seed;
+    char *par = NULL;
+    const char *desc, *seed;
     game_params *newcurparams, *newparams, *oldparams1, *oldparams2;
     int free_params;
 
@@ -1409,8 +1411,10 @@
          * description. So `par' now points to the parameters
          * string, and `desc' to the description string.
          */
-        *desc++ = '\0';
-        par = id;
+        par = snewn(desc-id + 1, char);
+        strncpy(par, id, desc-id);
+        par[desc-id] = '\0';
+        desc++;
         seed = NULL;
     } else if (seed && (!desc || seed < desc)) {
         /*
@@ -1418,8 +1422,10 @@
          * So `par' now points to the parameters string, and `seed'
          * to the seed string.
          */
-        *seed++ = '\0';
-        par = id;
+        par = snewn(seed-id + 1, char);
+        strncpy(par, id, seed-id);
+        par[seed-id] = '\0';
+        seed++;
         desc = NULL;
     } else {
         /*
@@ -1428,12 +1434,14 @@
          */
         if (defmode == DEF_SEED) {
             seed = id;
-            par = desc = NULL;
+            par = NULL;
+            desc = NULL;
         } else if (defmode == DEF_DESC) {
             desc = id;
-            par = seed = NULL;
+            par = NULL;
+            seed = NULL;
         } else {
-            par = id;
+            par = dupstr(id);
             seed = desc = NULL;
         }
     }
@@ -1563,10 +1571,12 @@
         me->genmode = GOT_SEED;
     }
 
+    sfree(par);
+
     return NULL;
 }
 
-const char *midend_game_id(midend *me, char *id)
+const char *midend_game_id(midend *me, const char *id)
 {
     return midend_game_id_int(me, id, DEF_PARAMS);
 }
@@ -1721,7 +1731,7 @@
     return me->ourgame->status(me->states[me->statepos-1].state);
 }
 
-char *midend_rewrite_statusbar(midend *me, char *text)
+char *midend_rewrite_statusbar(midend *me, const char *text)
 {
     /*
      * An important special case is that we are occasionally called
--- a/misc.c
+++ b/misc.c
@@ -351,7 +351,7 @@
 
 void draw_text_outline(drawing *dr, int x, int y, int fonttype,
                        int fontsize, int align,
-                       int text_colour, int outline_colour, char *text)
+                       int text_colour, int outline_colour, const char *text)
 {
     if (outline_colour > -1) {
         draw_text(dr, x-1, y, fonttype, fontsize, align, outline_colour, text);
--- a/nestedvm.c
+++ b/nestedvm.c
@@ -17,7 +17,7 @@
 extern void _pause();
 extern int _call_java(int cmd, int arg1, int arg2, int arg3);
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     va_list ap;
     fprintf(stderr, "fatal error: ");
@@ -53,7 +53,7 @@
     output[0] = output[1]= output[2] = 0.8f;
 }
 
-void nestedvm_status_bar(void *handle, char *text)
+void nestedvm_status_bar(void *handle, const char *text)
 {
     _call_java(4,0,(int)text,0);
 }
@@ -79,7 +79,7 @@
 }
 
 void nestedvm_draw_text(void *handle, int x, int y, int fonttype, int fontsize,
-		   int align, int colour, char *text)
+                        int align, int colour, const char *text)
 {
     frontend *fe = (frontend *)handle;
     _call_java(5, x + fe->ox, y + fe->oy, 
--- a/nullfe.c
+++ b/nullfe.c
@@ -10,7 +10,7 @@
 
 void frontend_default_colour(frontend *fe, float *output) {}
 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
-               int align, int colour, char *text) {}
+               int align, int colour, const char *text) {}
 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour) {}
 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour) {}
 void draw_thick_line(drawing *dr, float thickness,
@@ -41,8 +41,9 @@
 { return 0; }
 void print_line_width(drawing *dr, int width) {}
 void print_line_dotted(drawing *dr, int dotted) {}
-void midend_supersede_game_desc(midend *me, char *desc, char *privdesc) {}
-void status_bar(drawing *dr, char *text) {}
+void midend_supersede_game_desc(midend *me, const char *desc,
+                                const char *privdesc) {}
+void status_bar(drawing *dr, const char *text) {}
 struct preset_menu *preset_menu_new(void) {return NULL;}
 struct preset_menu *preset_menu_add_submenu(struct preset_menu *parent,
                                             char *title) {return NULL;}
@@ -49,7 +50,7 @@
 void preset_menu_add_preset(struct preset_menu *parent,
                             char *title, game_params *params) {}
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     va_list ap;
 
@@ -64,7 +65,7 @@
 }
 
 #ifdef DEBUGGING
-void debug_printf(char *fmt, ...)
+void debug_printf(const char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
--- a/osx.m
+++ b/osx.m
@@ -111,7 +111,7 @@
  * clearly defined subsystem.
  */
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     va_list ap;
     char errorbuf[2048];
@@ -275,7 +275,7 @@
     return item;
 }
 
-NSMenuItem *newitem(NSMenu *parent, char *title, char *key,
+NSMenuItem *newitem(NSMenu *parent, const char *title, const char *key,
 		    id target, SEL action)
 {
     return initnewitem([NSMenuItem allocWithZone:[NSMenu menuZone]],
@@ -437,7 +437,7 @@
 - (void)keyDown:(NSEvent *)ev;
 - (void)activateTimer;
 - (void)deactivateTimer;
-- (void)setStatusLine:(char *)text;
+- (void)setStatusLine:(const char *)text;
 - (void)resizeForNewGameParams;
 - (void)updateTypeMenuTick;
 @end
@@ -726,7 +726,7 @@
     last_time = now;
 }
 
-- (void)showError:(char *)message
+- (void)showError:(const char *)message
 {
     NSAlert *alert;
 
@@ -1300,7 +1300,7 @@
     if (update) {
 	int k;
 	config_item *i;
-	char *error;
+	const char *error;
 
 	k = 0;
 	for (i = cfg; i->type != C_END; i++) {
@@ -1348,7 +1348,7 @@
     [self sheetEndWithStatus:NO];
 }
 
-- (void)setStatusLine:(char *)text
+- (void)setStatusLine:(const char *)text
 {
     [[status cell] setTitle:[NSString stringWithUTF8String:text]];
 }
@@ -1461,7 +1461,8 @@
     NSRectFill(r);
 }
 static void osx_draw_text(void *handle, int x, int y, int fonttype,
-			  int fontsize, int align, int colour, char *text)
+			  int fontsize, int align, int colour,
+                          const char *text)
 {
     frontend *fe = (frontend *)handle;
     NSString *string = [NSString stringWithUTF8String:text];
@@ -1612,7 +1613,7 @@
     frontend *fe = (frontend *)handle;
     [fe->image unlockFocus];
 }
-static void osx_status_bar(void *handle, char *text)
+static void osx_status_bar(void *handle, const char *text)
 {
     frontend *fe = (frontend *)handle;
     [fe->window setStatusLine:text];
--- a/ps.c
+++ b/ps.c
@@ -102,7 +102,8 @@
 }
 
 static void ps_draw_text(void *handle, int x, int y, int fonttype,
-			 int fontsize, int align, int colour, char *text)
+			 int fontsize, int align, int colour,
+                         const char *text)
 {
     psdata *ps = (psdata *)handle;
 
--- a/puzzles.h
+++ b/puzzles.h
@@ -224,12 +224,12 @@
 /* We can't use #ifdef DEBUG, because Cygwin defines it by default. */
 #ifdef DEBUGGING
 #define debug(x) (debug_printf x)
-void debug_printf(char *fmt, ...);
+void debug_printf(const char *fmt, ...);
 #else
 #define debug(x)
 #endif
 
-void fatal(char *fmt, ...);
+void fatal(const char *fmt, ...);
 void frontend_default_colour(frontend *fe, float *output);
 void deactivate_timer(frontend *fe);
 void activate_timer(frontend *fe);
@@ -241,7 +241,7 @@
 drawing *drawing_new(const drawing_api *api, midend *me, void *handle);
 void drawing_free(drawing *dr);
 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
-               int align, int colour, char *text);
+               int align, int colour, const char *text);
 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour);
 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour);
 void draw_polygon(drawing *dr, int *coords, int npoints,
@@ -256,7 +256,7 @@
 void draw_update(drawing *dr, int x, int y, int w, int h);
 void end_draw(drawing *dr);
 char *text_fallback(drawing *dr, const char *const *strings, int nstrings);
-void status_bar(drawing *dr, char *text);
+void status_bar(drawing *dr, const char *text);
 blitter *blitter_new(drawing *dr, int w, int h);
 void blitter_free(drawing *dr, blitter *bl);
 /* save puts the portion of the current display with top-left corner
@@ -312,7 +312,7 @@
 enum { CFG_SETTINGS, CFG_SEED, CFG_DESC, CFG_FRONTEND_SPECIFIC };
 config_item *midend_get_config(midend *me, int which, char **wintitle);
 const char *midend_set_config(midend *me, int which, config_item *cfg);
-const char *midend_game_id(midend *me, char *id);
+const char *midend_game_id(midend *me, const char *id);
 char *midend_get_game_id(midend *me);
 char *midend_get_random_seed(midend *me);
 int midend_can_format_as_text_now(midend *me);
@@ -321,8 +321,9 @@
 int midend_status(midend *me);
 int midend_can_undo(midend *me);
 int midend_can_redo(midend *me);
-void midend_supersede_game_desc(midend *me, char *desc, char *privdesc);
-char *midend_rewrite_statusbar(midend *me, char *text);
+void midend_supersede_game_desc(midend *me, const char *desc,
+                                const char *privdesc);
+char *midend_rewrite_statusbar(midend *me, const char *text);
 void midend_serialise(midend *me,
                       void (*write)(void *ctx, void *buf, int len),
                       void *wctx);
@@ -392,7 +393,7 @@
  * by one pixel; useful for highlighting. Outline is omitted if -1. */
 void draw_text_outline(drawing *dr, int x, int y, int fonttype,
                        int fontsize, int align,
-                       int text_colour, int outline_colour, char *text);
+                       int text_colour, int outline_colour, const char *text);
 
 /* Copies text left-justified with spaces. Length of string must be
  * less than buffer size. */
@@ -645,7 +646,7 @@
  */
 struct drawing_api {
     void (*draw_text)(void *handle, int x, int y, int fonttype, int fontsize,
-		      int align, int colour, char *text);
+		      int align, int colour, const char *text);
     void (*draw_rect)(void *handle, int x, int y, int w, int h, int colour);
     void (*draw_line)(void *handle, int x1, int y1, int x2, int y2,
 		      int colour);
@@ -658,7 +659,7 @@
     void (*unclip)(void *handle);
     void (*start_draw)(void *handle);
     void (*end_draw)(void *handle);
-    void (*status_bar)(void *handle, char *text);
+    void (*status_bar)(void *handle, const char *text);
     blitter *(*blitter_new)(void *handle, int w, int h);
     void (*blitter_free)(void *handle, blitter *bl);
     void (*blitter_save)(void *handle, blitter *bl, int x, int y);
--- a/unequal.c
+++ b/unequal.c
@@ -2039,7 +2039,7 @@
 
 #if 0 /* currently unused */
 
-static void debug_printf(char *fmt, ...)
+static void debug_printf(const char *fmt, ...)
 {
     char buf[4096];
     va_list ap;
--- a/unfinished/path.c
+++ b/unfinished/path.c
@@ -770,7 +770,7 @@
 #ifdef TEST_GENERAL
 #include <stdarg.h>
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     va_list ap;
 
--- a/untangle.c
+++ b/untangle.c
@@ -656,7 +656,7 @@
      */
     ret = NULL;
     {
-	char *sep;
+	const char *sep;
 	char buf[80];
 	int retlen;
 	edge *ea;
--- a/windows.c
+++ b/windows.c
@@ -150,7 +150,7 @@
     OutputDebugString(buf);
 }
 
-void debug_printf(char *fmt, ...)
+void debug_printf(const char *fmt, ...)
 {
     char buf[4096];
     va_list ap;
@@ -258,7 +258,7 @@
 static void update_type_menu_tick(frontend *fe);
 static void update_copy_menu_greying(frontend *fe);
 
-void fatal(char *fmt, ...)
+void fatal(const char *fmt, ...)
 {
     char buf[2048];
     va_list ap;
@@ -304,7 +304,7 @@
     *randseedsize = sizeof(SYSTEMTIME);
 }
 
-static void win_status_bar(void *handle, char *text)
+static void win_status_bar(void *handle, const char *text)
 {
 #ifdef _WIN32_WCE
     TCHAR wText[255];
@@ -556,7 +556,8 @@
 }
 
 static void win_draw_text(void *handle, int x, int y, int fonttype,
-			  int fontsize, int align, int colour, char *text)
+			  int fontsize, int align, int colour,
+                          const char *text)
 {
     frontend *fe = (frontend *)handle;
     POINT xy;