Revision: 1868 http://geany.svn.sourceforge.net/geany/?rev=1868&view=rev Author: ntrel Date: 2007-09-11 08:21:11 -0700 (Tue, 11 Sep 2007)
Log Message: ----------- Neaten up the plugin API: Make document_open_file() now wrap document_open_file_full(), without the idx for reloading or pos arguments. Replace str_replace() with string_replace_all() in the plugin API. Add utils_string_replace_all(), taking a GString argument.
Modified Paths: -------------- trunk/ChangeLog trunk/plugins/classbuilder.c trunk/plugins/export.c trunk/src/callbacks.c trunk/src/document.c trunk/src/document.h trunk/src/keyfile.c trunk/src/main.c trunk/src/msgwindow.c trunk/src/plugindata.h trunk/src/plugins.c trunk/src/socket.c trunk/src/ui_utils.c trunk/src/utils.c trunk/src/utils.h trunk/src/win32.c
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/ChangeLog 2007-09-11 15:21:11 UTC (rev 1868) @@ -1,3 +1,16 @@ +2007-09-11 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com> + + * plugins/export.c, plugins/classbuilder.c, src/utils.c, src/win32.c, + src/utils.h, src/plugindata.h, src/msgwindow.c, src/callbacks.c, + src/keyfile.c, src/document.c, src/plugins.c, src/document.h, + src/main.c, src/socket.c, src/ui_utils.c: + Neaten up the plugin API: + Make document_open_file() now wrap document_open_file_full(), + without the idx for reloading or pos arguments. + Replace str_replace() with string_replace_all() in the plugin API. + Add utils_string_replace_all(), taking a GString argument. + + 2007-09-10 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* THANKS, pixmaps/geany.ico, pixmaps/geany.png:
Modified: trunk/plugins/classbuilder.c =================================================================== --- trunk/plugins/classbuilder.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/plugins/classbuilder.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -251,102 +251,97 @@ static gchar* get_template_class_header(ClassInfo *class_info) { + gchar *fileheader = NULL; + GString *template = NULL; + switch (class_info->type) { case GEANY_CLASS_TYPE_CPP: - { - gchar *fileheader = NULL; - gchar *template; - - fileheader = templates->get_template_fileheader(GEANY_FILETYPES_C, class_info->header); - template = g_strdup(templates_cpp_class_header); - template = utils->str_replace(template, "{fileheader}", fileheader); - template = utils->str_replace(template, "{header_guard}", class_info->header_guard); - template = utils->str_replace(template, "{base_include}", class_info->base_include); - template = utils->str_replace(template, "{class_name}", class_info->class_name); - template = utils->str_replace(template, "{base_decl}", class_info->base_decl); - template = utils->str_replace(template, "{constructor_decl}", + fileheader = templates->get_template_fileheader(GEANY_FILETYPES_CPP, class_info->header); + template = g_string_new(templates_cpp_class_header); + utils->string_replace_all(template, "{fileheader}", fileheader); + utils->string_replace_all(template, "{header_guard}", class_info->header_guard); + utils->string_replace_all(template, "{base_include}", class_info->base_include); + utils->string_replace_all(template, "{class_name}", class_info->class_name); + utils->string_replace_all(template, "{base_decl}", class_info->base_decl); + utils->string_replace_all(template, "{constructor_decl}", class_info->constructor_decl); - template = utils->str_replace(template, "{destructor_decl}", + utils->string_replace_all(template, "{destructor_decl}", class_info->destructor_decl); + break;
- return template; - } case GEANY_CLASS_TYPE_GTK: - { - gchar *fileheader = NULL; - gchar *template; - fileheader = templates->get_template_fileheader(GEANY_FILETYPES_C, class_info->header); - template = g_strdup(templates_gtk_class_header); - template = utils->str_replace(template, "{fileheader}", fileheader); - template = utils->str_replace(template, "{header_guard}", class_info->header_guard); - template = utils->str_replace(template, "{base_include}", class_info->base_include); - template = utils->str_replace(template, "{class_name}", class_info->class_name); - template = utils->str_replace(template, "{class_name_up}", class_info->class_name_up); - template = utils->str_replace(template, "{class_name_low}", class_info->class_name_low); - template = utils->str_replace(template, "{base_name}", class_info->base_name); - template = utils->str_replace(template, "{constructor_decl}", + template = g_string_new(templates_gtk_class_header); + utils->string_replace_all(template, "{fileheader}", fileheader); + utils->string_replace_all(template, "{header_guard}", class_info->header_guard); + utils->string_replace_all(template, "{base_include}", class_info->base_include); + utils->string_replace_all(template, "{class_name}", class_info->class_name); + utils->string_replace_all(template, "{class_name_up}", class_info->class_name_up); + utils->string_replace_all(template, "{class_name_low}", class_info->class_name_low); + utils->string_replace_all(template, "{base_name}", class_info->base_name); + utils->string_replace_all(template, "{constructor_decl}", class_info->constructor_decl); - - return template; - } + break; }
- return NULL; + g_free(fileheader); + + if (template) + return g_string_free(template, FALSE); + else + return NULL; }
+ static gchar* get_template_class_source(ClassInfo *class_info) { + gchar *fileheader = NULL; + GString *template = NULL; + switch (class_info->type) { case GEANY_CLASS_TYPE_CPP: - { - gchar *fileheader = NULL; - gchar *template; - - fileheader = templates->get_template_fileheader(GEANY_FILETYPES_C, class_info->source); - template = g_strdup(templates_cpp_class_source); - template = utils->str_replace(template, "{fileheader}", fileheader); - template = utils->str_replace(template, "{header}", class_info->header); - template = utils->str_replace(template, "{class_name}", class_info->class_name); - template = utils->str_replace(template, "{base_include}", class_info->base_include); - template = utils->str_replace(template, "{base_name}", class_info->base_name); - template = utils->str_replace(template, "{constructor_impl}", + fileheader = templates->get_template_fileheader(GEANY_FILETYPES_CPP, class_info->source); + template = g_string_new(templates_cpp_class_source); + utils->string_replace_all(template, "{fileheader}", fileheader); + utils->string_replace_all(template, "{header}", class_info->header); + utils->string_replace_all(template, "{class_name}", class_info->class_name); + utils->string_replace_all(template, "{base_include}", class_info->base_include); + utils->string_replace_all(template, "{base_name}", class_info->base_name); + utils->string_replace_all(template, "{constructor_impl}", class_info->constructor_impl); - template = utils->str_replace(template, "{destructor_impl}", + utils->string_replace_all(template, "{destructor_impl}", class_info->destructor_impl); + break;
- return template; - } case GEANY_CLASS_TYPE_GTK: - { - gchar *fileheader = NULL; - gchar *template; - fileheader = templates->get_template_fileheader(GEANY_FILETYPES_C, class_info->source); - template = g_strdup(templates_gtk_class_source); - template = utils->str_replace(template, "{fileheader}", fileheader); - template = utils->str_replace(template, "{header}", class_info->header); - template = utils->str_replace(template, "{class_name}", class_info->class_name); - template = utils->str_replace(template, "{class_name_up}", class_info->class_name_up); - template = utils->str_replace(template, "{class_name_low}", class_info->class_name_low); - template = utils->str_replace(template, "{base_name}", class_info->base_name); - template = utils->str_replace(template, "{base_gtype}", class_info->base_gtype); - template = utils->str_replace(template, "{destructor_decl}", class_info->destructor_decl); - template = utils->str_replace(template, "{constructor_impl}", + template = g_string_new(templates_gtk_class_source); + utils->string_replace_all(template, "{fileheader}", fileheader); + utils->string_replace_all(template, "{header}", class_info->header); + utils->string_replace_all(template, "{class_name}", class_info->class_name); + utils->string_replace_all(template, "{class_name_up}", class_info->class_name_up); + utils->string_replace_all(template, "{class_name_low}", class_info->class_name_low); + utils->string_replace_all(template, "{base_name}", class_info->base_name); + utils->string_replace_all(template, "{base_gtype}", class_info->base_gtype); + utils->string_replace_all(template, "{destructor_decl}", class_info->destructor_decl); + utils->string_replace_all(template, "{constructor_impl}", class_info->constructor_impl); - template = utils->str_replace(template, "{destructor_impl}", + utils->string_replace_all(template, "{destructor_impl}", class_info->destructor_impl); - template = utils->str_replace(template, "{gtk_destructor_registration}", + utils->string_replace_all(template, "{gtk_destructor_registration}", class_info->gtk_destructor_registration); - - return template; - } + break; }
- return NULL; + g_free(fileheader); + + if (template) + return g_string_free(template, FALSE); + else + return NULL; }
Modified: trunk/plugins/export.c =================================================================== --- trunk/plugins/export.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/plugins/export.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -275,7 +275,7 @@ }
-static gchar *get_date(gint type) +static const gchar *get_date(gint type) { static gchar str[128]; gchar *format; @@ -346,10 +346,10 @@ gchar c, c_next, *tmp; // 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) gint styles[STYLE_MAX + 1][MAX_TYPES]; - gchar *latex; gboolean block_open = FALSE; GString *body; GString *cmds; + GString *latex;
// first read all styles from Scintilla for (i = 0; i <= STYLE_MAX; i++) @@ -526,20 +526,20 @@ }
// write all - latex = g_strdup(TEMPLATE_LATEX); - latex = utils->str_replace(latex, "{export_content}", body->str); - latex = utils->str_replace(latex, "{export_styles}", cmds->str); - latex = utils->str_replace(latex, "{export_date}", get_date(DATE_TYPE_DEFAULT)); + latex = g_string_new(TEMPLATE_LATEX); + utils->string_replace_all(latex, "{export_content}", body->str); + utils->string_replace_all(latex, "{export_styles}", cmds->str); + utils->string_replace_all(latex, "{export_date}", get_date(DATE_TYPE_DEFAULT)); if (doc_list[idx].file_name == NULL) - latex = utils->str_replace(latex, "{export_filename}", GEANY_STRING_UNTITLED); + utils->string_replace_all(latex, "{export_filename}", GEANY_STRING_UNTITLED); else - latex = utils->str_replace(latex, "{export_filename}", doc_list[idx].file_name); + utils->string_replace_all(latex, "{export_filename}", doc_list[idx].file_name);
- write_data(filename, latex); + write_data(filename, latex->str);
g_string_free(body, TRUE); g_string_free(cmds, TRUE); - g_free(latex); + g_string_free(latex, TRUE); }
@@ -550,12 +550,12 @@ // 0 - fore, 1 - back, 2 - bold, 3 - italic, 4 - font size, 5 - used(0/1) gint styles[STYLE_MAX + 1][MAX_TYPES]; gboolean span_open = FALSE; - gchar *html; const gchar *font_name; gint font_size; PangoFontDescription *font_desc; GString *body; GString *css; + GString *html;
// first read all styles from Scintilla for (i = 0; i <= STYLE_MAX; i++) @@ -677,21 +677,21 @@ }
// write all - html = g_strdup(TEMPLATE_HTML); - html = utils->str_replace(html, "{export_date}", get_date(DATE_TYPE_HTML)); - html = utils->str_replace(html, "{export_content}", body->str); - html = utils->str_replace(html, "{export_styles}", css->str); + html = g_string_new(TEMPLATE_HTML); + utils->string_replace_all(html, "{export_date}", get_date(DATE_TYPE_HTML)); + utils->string_replace_all(html, "{export_content}", body->str); + utils->string_replace_all(html, "{export_styles}", css->str); if (doc_list[idx].file_name == NULL) - html = utils->str_replace(html, "{export_filename}", GEANY_STRING_UNTITLED); + utils->string_replace_all(html, "{export_filename}", GEANY_STRING_UNTITLED); else - html = utils->str_replace(html, "{export_filename}", doc_list[idx].file_name); + utils->string_replace_all(html, "{export_filename}", doc_list[idx].file_name);
- write_data(filename, html); + write_data(filename, html->str);
pango_font_description_free(font_desc); g_string_free(body, TRUE); g_string_free(css, TRUE); - g_free(html); + g_string_free(html, TRUE); }
Modified: trunk/src/callbacks.c =================================================================== --- trunk/src/callbacks.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/callbacks.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -1947,7 +1947,7 @@ }
locale_filename = utils_get_locale_from_utf8(filename); - document_open_file(-1, locale_filename, 0, FALSE, NULL, NULL); + document_open_file(locale_filename, FALSE, NULL, NULL);
g_free(filename); g_free(locale_filename);
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/document.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -563,6 +563,15 @@ }
+/* This is a wrapper for document_open_file_full(), see that function for details. + * Do not use this when opening multiple files (unless using document_delay_colourise()). */ +gint document_open_file(const gchar *locale_filename, gboolean readonly, + filetype *ft, const gchar *forced_enc) +{ + return document_open_file_full(-1, locale_filename, 0, readonly, ft, forced_enc); +} + + typedef struct { gchar *data; // null-terminated file data @@ -825,7 +834,7 @@ * * This avoids unnecessary recolourising, saving significant processing when a lot of files * are open of a filetype that supports user typenames, e.g. C. */ -gint document_open_file(gint idx, const gchar *filename, gint pos, gboolean readonly, +gint document_open_file_full(gint idx, const gchar *filename, gint pos, gboolean readonly, filetype *ft, const gchar *forced_enc) { gint editor_mode; @@ -987,7 +996,7 @@ if (list[i] == NULL) break; filename = g_filename_from_uri(list[i], NULL, NULL); if (filename == NULL) continue; - document_open_file(-1, filename, 0, FALSE, NULL, NULL); + document_open_file(filename, FALSE, NULL, NULL); g_free(filename); } document_colourise_new(); @@ -1007,7 +1016,7 @@
for (item = filenames; item != NULL; item = g_slist_next(item)) { - document_open_file(-1, item->data, 0, readonly, ft, forced_enc); + document_open_file(item->data, readonly, ft, forced_enc); } document_colourise_new(); } @@ -1022,7 +1031,7 @@
// try to set the cursor to the position before reloading pos = sci_get_current_position(doc_list[idx].sci); - return document_open_file(idx, NULL, pos, doc_list[idx].readonly, + return document_open_file_full(idx, NULL, pos, doc_list[idx].readonly, doc_list[idx].file_type, forced_enc); }
Modified: trunk/src/document.h =================================================================== --- trunk/src/document.h 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/document.h 2007-09-11 15:21:11 UTC (rev 1868) @@ -127,9 +127,12 @@
/* See document.c. */ -gint document_open_file(gint idx, const gchar *filename, gint pos, gboolean readonly, +gint document_open_file(const gchar *locale_filename, gboolean readonly, filetype *ft, const gchar *forced_enc);
+gint document_open_file_full(gint idx, const gchar *filename, gint pos, gboolean readonly, + filetype *ft, const gchar *forced_enc); + /* Takes a new line separated list of filename URIs and opens each file. * length is the length of the string or -1 if it should be detected */ void document_open_file_list(const gchar *data, gssize length);
Modified: trunk/src/keyfile.c =================================================================== --- trunk/src/keyfile.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/keyfile.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -693,7 +693,7 @@ if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK)) { filetype *ft = filetypes_get_from_uid(uid); - document_open_file(-1, locale_filename, pos, FALSE, ft, NULL); + document_open_file_full(-1, locale_filename, pos, FALSE, ft, NULL); ret = TRUE; } else
Modified: trunk/src/main.c =================================================================== --- trunk/src/main.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/main.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -542,7 +542,7 @@ { gint idx;
- idx = document_open_file(-1, filename, 0, FALSE, NULL, NULL); + idx = document_open_file(filename, FALSE, NULL, NULL); // add recent file manually because opening_session_files is set if (DOC_IDX_VALID(idx)) ui_add_recent_file(doc_list[idx].file_name);
Modified: trunk/src/msgwindow.c =================================================================== --- trunk/src/msgwindow.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/msgwindow.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -514,7 +514,7 @@ g_free(utf8_filename);
if (idx < 0) // file not already open - idx = document_open_file(-1, filename, 0, FALSE, NULL, NULL); + idx = document_open_file(filename, FALSE, NULL, NULL);
if (idx >= 0 && doc_list[idx].is_valid) { @@ -788,7 +788,7 @@ if (filename != NULL && line > -1) { // use document_open_file to find an already open file, or open it in place - idx = document_open_file(-1, filename, 0, FALSE, NULL, NULL); + idx = document_open_file(filename, FALSE, NULL, NULL); // utils_goto_file_line will check valid filename. ret = utils_goto_file_line(filename, FALSE, line); }
Modified: trunk/src/plugindata.h =================================================================== --- trunk/src/plugindata.h 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/plugindata.h 2007-09-11 15:21:11 UTC (rev 1868) @@ -71,12 +71,12 @@
/* The API version should be incremented whenever any plugin data types below are * modified. */ -static const gint api_version = 16; +static const gint api_version = 17;
/* The ABI version should be incremented whenever existing fields in the plugin * data types below have to be changed or reordered. It should stay the same if fields * are only appended, as this doesn't affect existing fields. */ -static const gint abi_version = 8; +static const gint abi_version = 9;
/* This performs runtime checks that try to ensure: * 1. Geany ABI data types are compatible with this plugin. @@ -174,7 +174,7 @@ gint (*get_cur_idx) (); struct document* (*get_current) (); gboolean (*save_file)(gint idx, gboolean force); - gboolean (*open_file)(gint idx, const gchar *filename, gint pos, gboolean readonly, + gboolean (*open_file)(const gchar *locale_filename, gboolean readonly, struct filetype *ft, const gchar *forced_enc); void (*open_files)(const GSList *filenames, gboolean readonly, struct filetype *ft, const gchar *forced_enc); @@ -232,7 +232,8 @@ typedef struct UtilsFuncs { gboolean (*str_equal) (const gchar *a, const gchar *b); - gchar* (*str_replace) (gchar *haystack, const gchar *needle, const gchar *replacement); + gboolean (*string_replace_all) (GString *haystack, const gchar *needle, + const gchar *replacement); GSList* (*get_file_list) (const gchar *path, guint *length, GError **error); gint (*write_file) (const gchar *filename, const gchar *text); gchar* (*get_locale_from_utf8) (const gchar *utf8_text);
Modified: trunk/src/plugins.c =================================================================== --- trunk/src/plugins.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/plugins.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -123,7 +123,7 @@
static UtilsFuncs utils_funcs = { &utils_str_equal, - &utils_str_replace, + &utils_string_replace_all, &utils_get_file_list, &utils_write_file, &utils_get_locale_from_utf8,
Modified: trunk/src/socket.c =================================================================== --- trunk/src/socket.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/socket.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -420,7 +420,7 @@ g_strstrip(buf); // remove \n char
if (g_file_test(buf, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK)) - document_open_file(-1, buf, 0, FALSE, NULL, NULL); + document_open_file(buf, FALSE, NULL, NULL); else { // create new file if it doesn't exist gint idx;
Modified: trunk/src/ui_utils.c =================================================================== --- trunk/src/ui_utils.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/ui_utils.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -838,7 +838,7 @@ gchar *utf8_filename = menu_item_get_text(menuitem); gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
- if (document_open_file(-1, locale_filename, 0, FALSE, NULL, NULL) > -1) + if (document_open_file(locale_filename, FALSE, NULL, NULL) > -1) recent_file_loaded(utf8_filename);
g_free(locale_filename);
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/utils.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -805,8 +805,9 @@ }
-/* replaces all occurrences of needle in haystack with replacement - * all strings have to NULL-terminated and needle and replacement have to be different, +/* Replaces all occurrences of needle in haystack with replacement. + * New code should use utils_string_replace_all() instead. + * All strings have to be NULL-terminated and needle and replacement have to be different, * e.g. needle "%" and replacement "%%" causes an endless loop */ gchar *utils_str_replace(gchar *haystack, const gchar *needle, const gchar *replacement) { @@ -1618,3 +1619,33 @@ } return FALSE; } + + +/* Replaces all occurrences of needle in str with replace. + * Currently this is not safe if replace matches needle. + * Returns: TRUE if needle was found. */ +gboolean utils_string_replace_all(GString *str, const gchar *needle, const gchar *replace) +{ + const gchar *c; + gssize pos = -1; + + g_return_val_if_fail(NZV(needle), FALSE); + g_return_val_if_fail(! NZV(replace) || strstr(replace, needle) == NULL, FALSE); + + while (1) + { + c = strstr(str->str, needle); + if (c == NULL) + break; + else + { + pos = c - str->str; + g_string_erase(str, pos, strlen(needle)); + if (replace) + g_string_insert(str, pos, replace); + } + } + return (pos != -1); +} + +
Modified: trunk/src/utils.h =================================================================== --- trunk/src/utils.h 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/utils.h 2007-09-11 15:21:11 UTC (rev 1868) @@ -99,10 +99,14 @@
gint utils_make_settings_dir(const gchar *dir, const gchar *data_dir, const gchar *doc_dir);
+ +gboolean utils_string_replace_all(GString *str, const gchar *needle, const gchar *replace); + gchar *utils_str_replace(gchar *haystack, const gchar *needle, const gchar *replacement);
gint utils_strpos(const gchar* haystack, const gchar * needle);
+ gchar *utils_get_date_time();
gchar *utils_get_date();
Modified: trunk/src/win32.c =================================================================== --- trunk/src/win32.c 2007-09-10 18:25:03 UTC (rev 1867) +++ trunk/src/win32.c 2007-09-11 15:21:11 UTC (rev 1868) @@ -311,7 +311,7 @@ x = of.nFileOffset - 1; if (x != strlen(fname)) { // open a single file - document_open_file(-1, fname, 0, of.Flags & OFN_READONLY, NULL, NULL); + document_open_file(fname, of.Flags & OFN_READONLY, NULL, NULL); } else { // open multiple files @@ -324,7 +324,7 @@ if (! fname[x+1]) break;
g_snprintf(file_name, 254, "%s\%s", fname, fname + x + 1); - document_open_file(-1, file_name, 0, of.Flags & OFN_READONLY, NULL, NULL); + document_open_file(file_name, of.Flags & OFN_READONLY, NULL, NULL); } x++; }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.