Branch: refs/heads/master Author: Colomban Wendling ban@herbesfolles.org Committer: Colomban Wendling ban@herbesfolles.org Date: Sat, 20 Feb 2016 19:03:22 UTC Commit: e1b778f9ef4b02ee8fffd46eff11ae83153a1eeb https://github.com/geany/geany-plugins/commit/e1b778f9ef4b02ee8fffd46eff11ae...
Log Message: ----------- pretty-printer: Separate the input and output arguments
Modified Paths: -------------- pretty-printer/src/PluginEntry.c pretty-printer/src/PrettyPrinter.c pretty-printer/src/PrettyPrinter.h
Modified: pretty-printer/src/PluginEntry.c 23 lines changed, 13 insertions(+), 10 deletions(-) =================================================================== @@ -111,8 +111,10 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata) GeanyDocument* doc = document_get_current(); GeanyEditor* editor; ScintillaObject* sco; - int length; - gchar* buffer; + int input_length; + gchar* input_buffer; + int output_length; + gchar* output_buffer; xmlDoc* parsedDocument; int result; int xOffset; @@ -128,19 +130,19 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata)
/* prepare the buffer that will contain the text * from the scintilla object */ - length = sci_get_length(sco)+1; - buffer = (char*)g_malloc(length*sizeof(char)); + input_length = sci_get_length(sco)+1; + input_buffer = (char*)g_malloc(input_length*sizeof(char));
/* retrieves the text */ - sci_get_text(sco, length, buffer); + sci_get_text(sco, input_length, input_buffer);
/* checks if the data is an XML format */ parsedDocument = xmlParseDoc((unsigned char*)buffer);
/* this is not a valid xml => exit with an error message */ if(parsedDocument == NULL) { - g_free(buffer); + g_free(input_buffer); dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to parse the content as XML.")); return; } @@ -149,16 +151,16 @@ 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) { - g_free(buffer); + g_free(input_buffer); 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); @@ -168,5 +170,6 @@ void xml_format(GtkMenuItem* menuitem, gpointer gdata) fileType = filetypes_index(GEANY_FILETYPES_XML); document_set_filetype(doc, fileType);
- g_free(buffer); + g_free(input_buffer); + g_free(output_buffer); }
Modified: pretty-printer/src/PrettyPrinter.c 21 lines changed, 10 insertions(+), 11 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*)g_try_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 */ @@ -144,9 +144,8 @@ int processXMLPrettyPrinting(char** buffer, int* length, PrettyPrintingOptions* /* if success, then update the values */ if (result == PRETTY_PRINTING_SUCCESS) { - g_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
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).
plugins-commits@lists.geany.org