[geany/geany-plugins] 1585dd: Merge pull request #364 from b4n/pretty-printer/less-deprecated
Jiří Techet
git-noreply at xxxxx
Thu Jun 9 13:09:06 UTC 2016
Branch: refs/heads/master
Author: Jiří Techet <techet at gmail.com>
Committer: Jiří Techet <techet at gmail.com>
Date: Thu, 09 Jun 2016 13:09:06 UTC
Commit: 1585dde214a25eebed3b39686bc03646e90afd01
https://github.com/geany/geany-plugins/commit/1585dde214a25eebed3b39686bc03646e90afd01
Log Message:
-----------
Merge pull request #364 from b4n/pretty-printer/less-deprecated
Pretty printer: Geany deprecated stuff fixes and other stuff
Modified Paths:
--------------
pretty-printer/src/PluginEntry.c
pretty-printer/src/PrettyPrinter.c
pretty-printer/src/PrettyPrinter.h
Modified: pretty-printer/src/PluginEntry.c
29 lines changed, 15 insertions(+), 14 deletions(-)
===================================================================
@@ -36,7 +36,6 @@ PLUGIN_SET_TRANSLATABLE_INFO(
_("XML PrettyPrinter"),
_("Formats an XML and makes it human-readable."),
PRETTY_PRINTER_VERSION, "Cédric Tabin - http://www.astorm.ch")
-PLUGIN_KEY_GROUP(prettyprinter, 1)
/*========================================== DECLARATIONS ================================================================*/
@@ -51,6 +50,8 @@ static void config_closed(GtkWidget* configWidget, gint response, gpointer data)
void plugin_init(GeanyData *data)
{
+ GeanyKeyGroup *key_group;
+
/* initializes the libxml2 */
LIBXML_TEST_VERSION
@@ -62,7 +63,8 @@ void plugin_init(GeanyData *data)
gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu_item);
/* init keybindings */
- keybindings_set_item(plugin_key_group, 0, kb_run_xml_pretty_print,
+ key_group = plugin_set_key_group(geany_plugin, "prettyprinter", 1, NULL);
+ keybindings_set_item(key_group, 0, kb_run_xml_pretty_print,
0, 0, "run_pretty_printer_xml", _("Run the PrettyPrinter XML"),
main_menu_item);
@@ -107,8 +109,10 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
GeanyDocument* doc = document_get_current();
GeanyEditor* editor;
ScintillaObject* sco;
- int length;
- char* buffer;
+ int input_length;
+ const gchar* input_buffer;
+ int output_length;
+ gchar* output_buffer;
xmlDoc* parsedDocument;
int result;
int xOffset;
@@ -122,17 +126,12 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
/* default printing options */
if (prettyPrintingOptions == NULL) { prettyPrintingOptions = createDefaultPrettyPrintingOptions(); }
- /* prepare the buffer that will contain the text
- * from the scintilla object */
- length = sci_get_length(sco)+1;
- buffer = (char*)malloc(length*sizeof(char));
- if (buffer == NULL) { exit(-1); } /* malloc error */
-
/* retrieves the text */
- sci_get_text(sco, length, buffer);
+ input_length = sci_get_length(sco);
+ input_buffer = (gchar *) scintilla_send_message(sco, SCI_GETCHARACTERPOINTER, 0, 0);
/* checks if the data is an XML format */
- parsedDocument = xmlParseDoc((unsigned char*)buffer);
+ parsedDocument = xmlParseDoc((const unsigned char*)input_buffer);
/* this is not a valid xml => exit with an error message */
if(parsedDocument == NULL)
@@ -145,15 +144,15 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
xmlFreeDoc(parsedDocument);
/* process pretty-printing */
- result = processXMLPrettyPrinting(&buffer, &length, prettyPrintingOptions);
+ result = processXMLPrettyPrinting(input_buffer, input_length, &output_buffer, &output_length, prettyPrintingOptions);
if (result != PRETTY_PRINTING_SUCCESS)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to process PrettyPrinting on the specified XML because some features are not supported.\n\nSee Help > Debug messages for more details..."));
return;
}
/* updates the document */
- sci_set_text(sco, buffer);
+ sci_set_text(sco, output_buffer);
/* set the line */
xOffset = scintilla_send_message(sco, SCI_GETXOFFSET, 0, 0);
@@ -162,4 +161,6 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
/* sets the type */
fileType = filetypes_index(GEANY_FILETYPES_XML);
document_set_filetype(doc, fileType);
+
+ g_free(output_buffer);
}
Modified: pretty-printer/src/PrettyPrinter.c
42 lines changed, 23 insertions(+), 19 deletions(-)
===================================================================
@@ -65,7 +65,7 @@ static int result; /* result of t
static char* xmlPrettyPrinted; /* new buffer for the formatted XML */
static int xmlPrettyPrintedLength; /* buffer size */
static int xmlPrettyPrintedIndex; /* buffer index (position of the next char to insert) */
-static char* inputBuffer; /* input buffer */
+static const char* inputBuffer; /* input buffer */
static int inputBufferLength; /* input buffer size */
static int inputBufferIndex; /* input buffer index (position of the next char to read into the input string) */
static int currentDepth; /* current depth (for indentation) */
@@ -86,14 +86,14 @@ static void PP_ERROR(const char* fmt, ...)
va_end(va);
}
-int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions* ppOptions)
+int processXMLPrettyPrinting(const char *xml, int xml_length, char** output, int* output_length, PrettyPrintingOptions* ppOptions)
{
bool freeOptions;
char* reallocated;
/* empty buffer, nothing to process */
- if (*length == 0) { return PRETTY_PRINTING_EMPTY_XML; }
- if (buffer == NULL || *buffer == NULL) { return PRETTY_PRINTING_EMPTY_XML; }
+ if (xml_length == 0) { return PRETTY_PRINTING_EMPTY_XML; }
+ if (xml == NULL) { return PRETTY_PRINTING_EMPTY_XML; }
/* initialize the variables */
result = PRETTY_PRINTING_SUCCESS;
@@ -112,11 +112,11 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions*
inputBufferIndex = 0;
currentDepth = -1;
- inputBuffer = *buffer;
- inputBufferLength = *length;
+ inputBuffer = xml;
+ inputBufferLength = xml_length;
- xmlPrettyPrintedLength = *length;
- xmlPrettyPrinted = (char*)malloc(sizeof(char)*(*length));
+ xmlPrettyPrintedLength = xml_length;
+ xmlPrettyPrinted = (char*)g_try_malloc(sizeof(char)*(xml_length));
if (xmlPrettyPrinted == NULL) { PP_ERROR("Allocation error (initialisation)"); return PRETTY_PRINTING_SYSTEM_ERROR; }
/* go to the first char */
@@ -129,24 +129,28 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions*
putCharInBuffer('\0');
/* adjust the final size */
- reallocated = (char*)realloc(xmlPrettyPrinted, xmlPrettyPrintedIndex);
- if (reallocated == NULL) { PP_ERROR("Allocation error (reallocation size is %d)", xmlPrettyPrintedIndex); return PRETTY_PRINTING_SYSTEM_ERROR; }
+ reallocated = (char*)g_try_realloc(xmlPrettyPrinted, xmlPrettyPrintedIndex);
+ if (reallocated == NULL) {
+ PP_ERROR("Allocation error (reallocation size is %d)", xmlPrettyPrintedIndex);
+ g_free(xmlPrettyPrinted);
+ xmlPrettyPrinted = NULL;
+ return PRETTY_PRINTING_SYSTEM_ERROR;
+ }
xmlPrettyPrinted = reallocated;
/* freeing the unused values */
- if (freeOptions) { free(options); }
+ if (freeOptions) { g_free(options); }
/* if success, then update the values */
if (result == PRETTY_PRINTING_SUCCESS)
{
- free(*buffer);
- *buffer = xmlPrettyPrinted;
- *length = xmlPrettyPrintedIndex-2; /* the '\0' is not in the length */
+ *output = xmlPrettyPrinted;
+ *output_length = xmlPrettyPrintedIndex-2; /* the '\0' is not in the length */
}
/* else clean the other values */
else
{
- free(xmlPrettyPrinted);
+ g_free(xmlPrettyPrinted);
}
/* updating the pointers for the using into the caller function */
@@ -161,7 +165,7 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions*
PrettyPrintingOptions* createDefaultPrettyPrintingOptions(void)
{
- PrettyPrintingOptions* defaultOptions = (PrettyPrintingOptions*)malloc(sizeof(PrettyPrintingOptions));
+ PrettyPrintingOptions* defaultOptions = (PrettyPrintingOptions*)g_try_malloc(sizeof(PrettyPrintingOptions));
if (defaultOptions == NULL)
{
PP_ERROR("Unable to allocate memory for PrettyPrintingOptions");
@@ -208,7 +212,7 @@ void putCharInBuffer(char charToAdd)
if (charToAdd == '\0') { ++xmlPrettyPrintedLength; }
else { xmlPrettyPrintedLength += inputBufferLength; }
- reallocated = (char*)realloc(xmlPrettyPrinted, xmlPrettyPrintedLength);
+ reallocated = (char*)g_try_realloc(xmlPrettyPrinted, xmlPrettyPrintedLength);
if (reallocated == NULL) { PP_ERROR("Allocation error (char was %c)", charToAdd); return; }
xmlPrettyPrinted = reallocated;
}
@@ -616,7 +620,7 @@ void processNode(void)
}
/* store the name */
- nodeName = (char*)malloc(sizeof(char)*nodeNameLength+1);
+ nodeName = (char*)g_try_malloc(sizeof(char)*nodeNameLength+1);
if (nodeName == NULL) { PP_ERROR("Allocation error (node name length is %d)", nodeNameLength); return ; }
nodeName[nodeNameLength] = '\0';
for (i=0 ; i<nodeNameLength ; ++i)
@@ -725,7 +729,7 @@ void processNode(void)
lastNodeOpen = FALSE;
/* freeeeeeee !!! */
- free(nodeName);
+ g_free(nodeName);
nodeName = NULL;
currentNodeName = NULL;
}
Modified: pretty-printer/src/PrettyPrinter.h
4 lines changed, 2 insertions(+), 2 deletions(-)
===================================================================
@@ -84,7 +84,7 @@ PrettyPrintingOptions;
/*========================================== FUNCTIONS =========================================================*/
-int processXMLPrettyPrinting(char** xml, int* length, PrettyPrintingOptions* ppOptions); /* process the pretty-printing on a valid xml string (no check done !!!). The ppOptions ARE NOT FREE-ED after processing. The method returns 0 if the pretty-printing has been done. */
-PrettyPrintingOptions* createDefaultPrettyPrintingOptions(void); /* creates a default PrettyPrintingOptions object */
+int processXMLPrettyPrinting(const char *xml, int xml_length, char** output, int* output_length, PrettyPrintingOptions* ppOptions); /* process the pretty-printing on a valid xml string (no check done !!!). The ppOptions ARE NOT FREE-ED after processing. The method returns 0 if the pretty-printing has been done. */
+PrettyPrintingOptions* createDefaultPrettyPrintingOptions(void); /* creates a default PrettyPrintingOptions object */
#endif
--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).
More information about the Plugins-Commits
mailing list