From 3276376d1be74b66970b88c3e941dcedf8d22474 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 1 Oct 2017 14:04:47 +0100 Subject: [PATCH] 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. --- combi.c | 2 +- devel.but | 14 ++++++++------ drawing.c | 4 ++-- emcc.c | 9 +++++---- gtk.c | 10 +++++----- midend.c | 34 ++++++++++++++++++++++------------ misc.c | 2 +- nestedvm.c | 6 +++--- nullfe.c | 11 ++++++----- osx.m | 17 +++++++++-------- ps.c | 3 ++- puzzles.h | 21 +++++++++++---------- unequal.c | 2 +- unfinished/path.c | 2 +- untangle.c | 2 +- windows.c | 9 +++++---- 16 files changed, 83 insertions(+), 65 deletions(-) diff --git a/combi.c b/combi.c index 4c5d107..3460183 100644 --- a/combi.c +++ b/combi.c @@ -79,7 +79,7 @@ void free_combi(combi_ctx *combi) #include -void fatal(char *fmt, ...) +void fatal(const char *fmt, ...) { abort(); } diff --git a/devel.but b/devel.but index c7112bb..af69202 100644 --- a/devel.but +++ b/devel.but @@ -1961,7 +1961,8 @@ This ensures that thin lines are visible even at small scales. \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 @@ printing routines, that code may safely call \cw{draw_update()}.) \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 @@ function \cw{drawing_new()} (see \k{drawing-new}). \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 @@ called unless drawing is attempted. \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 @@ using \cw{midend_size()} and eventually perform a refresh using \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 @@ calling \cw{midend_timer()}. \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 diff --git a/drawing.c b/drawing.c index a10a7f0..caf0b4b 100644 --- a/drawing.c +++ b/drawing.c @@ -71,7 +71,7 @@ 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) { dr->api->draw_text(dr->handle, x, y, fonttype, fontsize, align, colour, text); @@ -190,7 +190,7 @@ char *text_fallback(drawing *dr, const char *const *strings, int nstrings) return NULL; /* placate optimiser */ } -void status_bar(drawing *dr, char *text) +void status_bar(drawing *dr, const char *text) { char *rewritten; diff --git a/emcc.c b/emcc.c index 7a74833..dbb4ab9 100644 --- a/emcc.c +++ b/emcc.c @@ -122,7 +122,7 @@ void get_random_seed(void **randseed, int *randseedsize) * 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 @@ void fatal(char *fmt, ...) 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_unclip(void *handle) } 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 @@ static void js_end_draw(void *handle) 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); } diff --git a/gtk.c b/gtk.c index 8c342f5..089143c 100644 --- 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 dputs(char *buf) } } -void debug_printf(char *fmt, ...) +void debug_printf(const char *fmt, ...) { char buf[4096]; va_list ap; @@ -101,7 +101,7 @@ void debug_printf(char *fmt, ...) * Error reporting functions used elsewhere. */ -void fatal(char *fmt, ...) +void fatal(const char *fmt, ...) { va_list ap; @@ -264,7 +264,7 @@ void frontend_default_colour(frontend *fe, float *output) #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_unclip(void *handle) } 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; diff --git a/midend.c b/midend.c index db038bd..cf1a5ae 100644 --- a/midend.c +++ b/midend.c @@ -1320,7 +1320,8 @@ void midend_request_id_changes(midend *me, void (*notify)(void *), void *ctx) 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 @@ config_item *midend_get_config(midend *me, int which, char **wintitle) 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 @@ static const char *midend_game_id_int(midend *me, char *id, int defmode) * 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 @@ static const char *midend_game_id_int(midend *me, char *id, int defmode) * 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 @@ static const char *midend_game_id_int(midend *me, char *id, int defmode) */ 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 @@ static const char *midend_game_id_int(midend *me, char *id, int defmode) 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 @@ int midend_status(midend *me) 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 diff --git a/misc.c b/misc.c index d555ad2..b6a190b 100644 --- a/misc.c +++ b/misc.c @@ -351,7 +351,7 @@ void pos2c(int w, int h, int pos, int *cx, int *cy) 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); diff --git a/nestedvm.c b/nestedvm.c index b778280..0ad0f3f 100644 --- 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 @@ void frontend_default_colour(frontend *fe, float *output) 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_unclip(void *handle) } 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, diff --git a/nullfe.c b/nullfe.c index ad381a1..14b6e09 100644 --- 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,15 +41,16 @@ int print_rgb_hatched_colour(drawing *dr, float r, float g, float b, int hatch) { 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;} 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 @@ void fatal(char *fmt, ...) } #ifdef DEBUGGING -void debug_printf(char *fmt, ...) +void debug_printf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); diff --git a/osx.m b/osx.m index 64cbb16..9403fae 100644 --- a/osx.m +++ b/osx.m @@ -111,7 +111,7 @@ NSApplication *app; * clearly defined subsystem. */ -void fatal(char *fmt, ...) +void fatal(const char *fmt, ...) { va_list ap; char errorbuf[2048]; @@ -275,7 +275,7 @@ id initnewitem(NSMenuItem *item, NSMenu *parent, const char *title, 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 @@ struct frontend { - (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 @@ struct frontend { last_time = now; } -- (void)showError:(char *)message +- (void)showError:(const char *)message { NSAlert *alert; @@ -1300,7 +1300,7 @@ struct frontend { 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 @@ struct frontend { [self sheetEndWithStatus:NO]; } -- (void)setStatusLine:(char *)text +- (void)setStatusLine:(const char *)text { [[status cell] setTitle:[NSString stringWithUTF8String:text]]; } @@ -1461,7 +1461,8 @@ static void osx_draw_rect(void *handle, int x, int y, int w, int h, int colour) 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 @@ static void osx_end_draw(void *handle) 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]; diff --git a/ps.c b/ps.c index 2394cc5..6ec8aac 100644 --- a/ps.c +++ b/ps.c @@ -102,7 +102,8 @@ static void ps_stroke(psdata *ps, int colour) } 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; diff --git a/puzzles.h b/puzzles.h index 662bcfe..11542a5 100644 --- a/puzzles.h +++ b/puzzles.h @@ -224,12 +224,12 @@ game_params *preset_menu_lookup_by_id(struct preset_menu *menu, int id); /* 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 @@ void get_random_seed(void **randseed, int *randseedsize); 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 start_draw(drawing *dr); 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 @@ int midend_wants_statusbar(midend *me); 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 @@ const char *midend_solve(midend *me); 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 @@ void pos2c(int w, int h, int pos, int *cx, int *cy); * 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 game { */ 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 @@ struct drawing_api { 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); diff --git a/unequal.c b/unequal.c index cdea0a0..ccb00dd 100644 --- a/unequal.c +++ b/unequal.c @@ -2039,7 +2039,7 @@ const char *quis = NULL; #if 0 /* currently unused */ -static void debug_printf(char *fmt, ...) +static void debug_printf(const char *fmt, ...) { char buf[4096]; va_list ap; diff --git a/unfinished/path.c b/unfinished/path.c index 61d6c61..d4ec5d8 100644 --- a/unfinished/path.c +++ b/unfinished/path.c @@ -770,7 +770,7 @@ int main(void) #ifdef TEST_GENERAL #include -void fatal(char *fmt, ...) +void fatal(const char *fmt, ...) { va_list ap; diff --git a/untangle.c b/untangle.c index 10114b5..a693378 100644 --- a/untangle.c +++ b/untangle.c @@ -656,7 +656,7 @@ static char *new_game_desc(const game_params *params, random_state *rs, */ ret = NULL; { - char *sep; + const char *sep; char buf[80]; int retlen; edge *ea; diff --git a/windows.c b/windows.c index 84cf5ec..0a43c5d 100644 --- a/windows.c +++ b/windows.c @@ -150,7 +150,7 @@ void dputs(char *buf) OutputDebugString(buf); } -void debug_printf(char *fmt, ...) +void debug_printf(const char *fmt, ...) { char buf[4096]; va_list ap; @@ -258,7 +258,7 @@ void frontend_free(frontend *fe) 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 @@ void get_random_seed(void **randseed, int *randseedsize) *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_unclip(void *handle) } 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; -- 2.30.2