[Freeciv-Dev] (PR#9936) merge helptext_improvement and helptext_wonder
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: |
undisclosed-recipients: ; |
Subject: |
[Freeciv-Dev] (PR#9936) merge helptext_improvement and helptext_wonder |
From: |
"Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx> |
Date: |
Sat, 4 Sep 2004 11:19:11 -0700 |
Reply-to: |
rt@xxxxxxxxxxx |
<URL: http://rt.freeciv.org/Ticket/Display.html?id=9936 >
This patch:
1. Merges helptext_improvement and helptext_wonder into helptext_building.
2. Adds a bufsz parameter to avoid buffer overflows.
3. Changes the callers appropriately.
4. Also changes the callers to not use long_buffer (some kind of change
was needed here, and I think it's best in the long run to cut out
long_buffer).
5. Has the functions return the buffer (though nobody uses it yet, it
could save a line here and there).
The other functions in helpdata should be changed similarly (by adding a
bufsz parameter). Another option is to use static astrings. If Per
moves these functions into common/ to be used by the
civmanual-generator, then they need to be safe from overrunning the buffer.
Another note is that the callers still generate some strings for
themselves, including the cost, tech requirement, and obsoletion tech.
These strings should probably also be provided by helpdata.
jason
? client/h
Index: client/helpdata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/helpdata.c,v
retrieving revision 1.73
diff -u -r1.73 helpdata.c
--- client/helpdata.c 31 Aug 2004 04:40:48 -0000 1.73
+++ client/helpdata.c 4 Sep 2004 18:13:54 -0000
@@ -494,62 +494,55 @@
*****************************************************************/
/**************************************************************************
- Write dynamic text for improvements (not wonders). This includes
+ Write dynamic text for buildings (including wonders). This includes
the ruleset helptext as well as any automatically generated text.
user_text, if non-NULL, will be appended to the text.
**************************************************************************/
-void helptext_improvement(char *buf, Impr_Type_id which,
- const char *user_text)
+char *helptext_building(char *buf, size_t bufsz, Impr_Type_id which,
+ const char *user_text)
{
struct impr_type *imp = &improvement_types[which];
assert(buf);
buf[0] = '\0';
- if (imp->helptext[0] != '\0') {
- sprintf(buf + strlen(buf), "%s ", _(imp->helptext));
- }
- if (tech_exists(improvement_types[which].obsolete_by)) {
- sprintf(buf + strlen(buf), "\n\n");
- sprintf(buf + strlen(buf),
- _("The discovery of %s will make %s obsolete."),
- advances[improvement_types[which].obsolete_by].name,
- improvement_types[which].name);
- sprintf(buf + strlen(buf), " ");
- }
- if (user_text && user_text[0] != '\0') {
- sprintf(buf+strlen(buf), "\n\n%s", user_text);
- }
- wordwrap_string(buf, 68);
-}
-/****************************************************************
- Append misc dynamic text for wonders.
-*****************************************************************/
-void helptext_wonder(char *buf, int which,
- const char *user_text)
-{
- struct impr_type *imp = &improvement_types[which];
-
- assert(buf&&user_text);
- buf[0] = '\0';
- if(which==B_MANHATTEN && num_role_units(F_NUCLEAR)>0) {
- int u, t;
+ if (which == B_MANHATTEN && num_role_units(F_NUCLEAR) > 0) {
+ Unit_Type_id u;
+ Tech_Type_id t;
+
u = get_role_unit(F_NUCLEAR, 0);
- assert(u<game.num_unit_types);
+ assert(u < game.num_unit_types);
t = get_unit_type(u)->tech_requirement;
- assert(t<game.num_tech_types);
- sprintf(buf+strlen(buf),
- _("Allows all players with knowledge of %s to build %s units. "),
- advances[t].name, get_unit_type(u)->name);
+ assert(t < game.num_tech_types);
+
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf),
+ _("Allows all players with knowledge of %s "
+ "to build %s units."),
+ advances[t].name, get_unit_type(u)->name);
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf), " ");
}
+
if (imp->helptext[0] != '\0') {
- sprintf(buf + strlen(buf), "%s ", _(imp->helptext));
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf),
+ "%s ", _(imp->helptext));
}
- if (strcmp(user_text, "")!=0) {
- sprintf(buf+strlen(buf), "\n\n%s", user_text);
+
+ if (tech_exists(improvement_types[which].obsolete_by)) {
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf), "\n\n");
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf),
+ _("The discovery of %s will make %s obsolete."),
+ advances[improvement_types[which].obsolete_by].name,
+ improvement_types[which].name);
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf), " ");
+ }
+
+ if (user_text && user_text[0] != '\0') {
+ my_snprintf(buf + strlen(buf), bufsz - strlen(buf), "\n\n%s", user_text);
}
+
wordwrap_string(buf, 68);
+ return buf;
}
/****************************************************************
Index: client/helpdata.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/helpdata.h,v
retrieving revision 1.9
diff -u -r1.9 helpdata.h
--- client/helpdata.h 27 Aug 2004 17:14:41 -0000 1.9
+++ client/helpdata.h 4 Sep 2004 18:13:54 -0000
@@ -31,8 +31,8 @@
void help_iter_start(void);
const struct help_item *help_iter_next(void);
-void helptext_improvement(char *buf, Impr_Type_id id, const char *user_text);
-void helptext_wonder(char *buf, int which, const char *user_text);
+char *helptext_building(char *buf, size_t bufsz, Impr_Type_id which,
+ const char *user_text);
void helptext_unit(char *buf, int i, const char *user_text);
void helptext_tech(char *buf, int i, const char *user_text);
void helptext_terrain(char *buf, int i, const char *user_text);
Index: client/gui-gtk/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk/helpdlg.c,v
retrieving revision 1.67
diff -u -r1.67 helpdlg.c
--- client/gui-gtk/helpdlg.c 31 Aug 2004 04:40:49 -0000 1.67
+++ client/gui-gtk/helpdlg.c 4 Sep 2004 18:13:54 -0000
@@ -645,7 +645,7 @@
static void help_update_improvement(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_IMPROVEMENT);
@@ -670,7 +670,7 @@
}
gtk_widget_show_all(help_itable);
- helptext_improvement(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
gtk_text_freeze(GTK_TEXT(help_text));
gtk_text_insert(GTK_TEXT(help_text), NULL, NULL, NULL, buf, -1);
@@ -685,7 +685,7 @@
static void help_update_wonder(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_WONDER);
@@ -715,7 +715,7 @@
}
gtk_widget_show_all(help_wtable);
- helptext_wonder(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
gtk_text_freeze(GTK_TEXT(help_text));
gtk_text_insert(GTK_TEXT(help_text), NULL, NULL, NULL, buf, -1);
gtk_text_thaw(GTK_TEXT(help_text));
Index: client/gui-gtk-2.0/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-gtk-2.0/helpdlg.c,v
retrieving revision 1.34
diff -u -r1.34 helpdlg.c
--- client/gui-gtk-2.0/helpdlg.c 31 Aug 2004 04:40:49 -0000 1.34
+++ client/gui-gtk-2.0/helpdlg.c 4 Sep 2004 18:13:55 -0000
@@ -684,7 +684,7 @@
static void help_update_improvement(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_IMPROVEMENT);
@@ -709,7 +709,7 @@
}
gtk_widget_show(help_itable);
- helptext_improvement(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
gtk_text_buffer_set_text(help_text, buf, -1);
gtk_widget_show(help_text_sw);
}
@@ -720,7 +720,7 @@
static void help_update_wonder(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_WONDER);
@@ -749,7 +749,7 @@
}
gtk_widget_show(help_wtable);
- helptext_wonder(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
gtk_text_buffer_set_text(help_text, buf, -1);
gtk_widget_show(help_text_sw);
}
Index: client/gui-mui/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-mui/helpdlg.c,v
retrieving revision 1.34
diff -u -r1.34 helpdlg.c
--- client/gui-mui/helpdlg.c 25 Feb 2004 20:09:51 -0000 1.34
+++ client/gui-mui/helpdlg.c 4 Sep 2004 18:13:55 -0000
@@ -605,7 +605,7 @@
static void help_update_improvement(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_IMPROVEMENT);
@@ -620,7 +620,7 @@
UpdateTechButton(help_imprv_needs_button, imp->tech_req);
}
- helptext_improvement(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
DoMethod(help_text_listview, MUIM_NList_Insert, buf, -2,
MUIV_List_Insert_Bottom);
}
@@ -630,7 +630,7 @@
static void help_update_wonder(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_WONDER);
@@ -651,7 +651,7 @@
set(help_wonder_obsolete_button, MUIA_Text_Contents, TECHTYPE_NONE);
}
- helptext_wonder(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
DoMethod(help_text_listview, MUIM_NList_Insert, buf, -2,
MUIV_List_Insert_Bottom);
}
Index: client/gui-sdl/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-sdl/helpdlg.c,v
retrieving revision 1.6
diff -u -r1.6 helpdlg.c
--- client/gui-sdl/helpdlg.c 18 Aug 2004 04:23:20 -0000 1.6
+++ client/gui-sdl/helpdlg.c 4 Sep 2004 18:13:55 -0000
@@ -191,7 +191,7 @@
bool created, text = FALSE;
int width = 0;
struct impr_type *pImpr_type;
- char *buffer = &long_buffer[0];
+ char buffer[64000];
if(current_help_dlg != IMPR)
{
@@ -403,12 +403,7 @@
start_x = (FRAME_WH + 1 + width + pHelpDlg->pEndActiveWidgetList->size.w +
20);
buffer[0] = '\0';
- if (is_wonder(impr))
- {
- helptext_wonder(buffer, impr, "");
- } else {
- helptext_improvement(buffer, impr, "");
- }
+ helptext_building(buffer, sizeof(buffer), impr, NULL);
if (buffer[0] != '\0')
{
SDL_String16 *pStr = create_str16_from_char(buffer, 12);
Index: client/gui-win32/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-win32/helpdlg.c,v
retrieving revision 1.20
diff -u -r1.20 helpdlg.c
--- client/gui-win32/helpdlg.c 31 Aug 2004 04:40:49 -0000 1.20
+++ client/gui-win32/helpdlg.c 4 Sep 2004 18:13:55 -0000
@@ -470,7 +470,7 @@
static void help_update_improvement(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_IMPROVEMENT);
@@ -493,7 +493,7 @@
SetWindowText(help_ilabel[5], _("(Never)"));
/* create_tech_tree(help_improvement_tree, 0, game.num_tech_types, 3);*/
}
- helptext_improvement(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
set_help_text(buf);
}
@@ -503,7 +503,7 @@
static void help_update_wonder(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_WONDER);
@@ -532,7 +532,7 @@
/* create_tech_tree(help_improvement_tree, 0, game.num_tech_types, 3); */
}
- helptext_wonder(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
set_help_text(buf);
}
Index: client/gui-xaw/helpdlg.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/gui-xaw/helpdlg.c,v
retrieving revision 1.51
diff -u -r1.51 helpdlg.c
--- client/gui-xaw/helpdlg.c 31 Aug 2004 04:40:50 -0000 1.51
+++ client/gui-xaw/helpdlg.c 4 Sep 2004 18:13:55 -0000
@@ -766,7 +766,7 @@
static void help_update_improvement(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_IMPROVEMENT);
@@ -794,7 +794,7 @@
create_tech_tree(help_tech_tree, 0, A_LAST, 3);
}
set_title_topic(pitem);
- helptext_improvement(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
XtVaSetValues(help_text, XtNstring, buf, NULL);
}
@@ -804,7 +804,7 @@
static void help_update_wonder(const struct help_item *pitem,
char *title, int which)
{
- char *buf = &long_buffer[0];
+ char buf[64000];
create_help_page(HELP_WONDER);
@@ -837,7 +837,7 @@
create_tech_tree(help_tech_tree, 0, game.num_tech_types, 3);
}
set_title_topic(pitem);
- helptext_wonder(buf, which, pitem->text);
+ helptext_building(buf, sizeof(buf), which, pitem->text);
XtVaSetValues(help_text, XtNstring, buf, NULL);
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#9936) merge helptext_improvement and helptext_wonder,
Jason Short <=
|
|