diff options
Diffstat (limited to 'fluid/proj')
| -rw-r--r-- | fluid/proj/mergeback.cxx | 127 | ||||
| -rw-r--r-- | fluid/proj/mergeback.h | 25 | ||||
| -rw-r--r-- | fluid/proj/undo.cxx | 2 |
3 files changed, 88 insertions, 66 deletions
diff --git a/fluid/proj/mergeback.cxx b/fluid/proj/mergeback.cxx index d19893042..bb7ef5504 100644 --- a/fluid/proj/mergeback.cxx +++ b/fluid/proj/mergeback.cxx @@ -17,6 +17,7 @@ #include "proj/mergeback.h" #include "Fluid.h" +#include "Project.h" #include "proj/undo.h" #include "io/Code_Writer.h" #include "nodes/Function_Node.h" @@ -28,6 +29,7 @@ #include <stdarg.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <ctype.h> #include <zlib.h> @@ -35,9 +37,6 @@ extern void propagate_load(Fl_Group*, void*); extern void load_panel(); extern void redraw_browser(); -using namespace fld; -using namespace fld::proj; - // TODO: add application user setting to control mergeback // [] new projects default to mergeback // [] check mergeback when loading project @@ -101,7 +100,7 @@ using namespace fld::proj; \return -2 if no code file was found \return see above */ -int merge_back(Project &proj, const std::string &s, const std::string &p, Mergeback::Task task) { +static int merge_back(fld::Project &proj, const char *s, const char *p, Mergeback::Task task) { if (proj.write_mergeback_data) { Mergeback mergeback(proj); return mergeback.merge_back(s, p, task); @@ -112,7 +111,7 @@ int merge_back(Project &proj, const std::string &s, const std::string &p, Mergeb } /** Allocate and initialize MergeBack class. */ -Mergeback::Mergeback(Project &proj) +Mergeback::Mergeback(fld::Project &proj) : proj_(proj), code(0), line_no(0), @@ -153,9 +152,9 @@ void Mergeback::unindent(char *s) { Read a block of text from the source file and remove the leading two spaces in every line. \param[in] start start of the block within the file \param[in] end end of text within the file - \return a string holding the text that was found in the file + \return a malloc'd string holding the text that was found in the file (caller must free) */ -std::string Mergeback::read_and_unindent_block(long start, long end) { +char *Mergeback::read_and_unindent_block(long start, long end) { long bsize = end-start; long here = ::ftell(code); ::fseek(code, start, SEEK_SET); @@ -166,10 +165,8 @@ std::string Mergeback::read_and_unindent_block(long start, long end) { else block[bsize] = 0; unindent(block); - std::string str = block; - ::free(block); ::fseek(code, here, SEEK_SET); - return str; + return block; } /** Tell user the results of our MergeBack analysis and pop up a dialog to give @@ -179,12 +176,12 @@ std::string Mergeback::read_and_unindent_block(long start, long end) { \return -1 if the user wants to cancel or an error occurred or an issue was presented (message or choice dialog was shown) */ -int Mergeback::ask_user_to_merge(const std::string &code_filename, const std::string &proj_filename) { +int Mergeback::ask_user_to_merge(const char *code_filename, const char *proj_filename) { if (tag_error) { fl_message("Comparing\n \"%s\"\nto\n \"%s\"\n\n" "MergeBack found an error in line %d while reading tags\n" "from the source code. Merging code back is not possible.", - code_filename.c_str(), proj_filename.c_str(), line_no); + code_filename, proj_filename, line_no); return -1; } if (!num_changed_code && !num_changed_structure) { @@ -196,39 +193,47 @@ int Mergeback::ask_user_to_merge(const std::string &code_filename, const std::st "of the source code. These kind of changes can not be\n" "merged back and will be lost when the source code is\n" "generated again from the open project.", - code_filename.c_str(), proj_filename.c_str(), num_changed_structure); + code_filename, proj_filename, num_changed_structure); return -1; } - std::string msg = "Comparing\n \"%1$s\"\nto\n \"%2$s\"\n\n" - "MergeBack found %3$d modifications in the source code."; + char msg[2048]; + int pos = 0; + pos += snprintf(msg + pos, sizeof(msg) - pos, + "Comparing\n \"%%1$s\"\nto\n \"%%2$s\"\n\n" + "MergeBack found %%3$d modifications in the source code."); if (num_possible_override) - msg += "\n\nWARNING: %6$d of these modified blocks appear to also have\n" - "changed in the project. Merging will override changes in\n" - "the project with changes from the source code file."; + pos += snprintf(msg + pos, sizeof(msg) - pos, + "\n\nWARNING: %%6$d of these modified blocks appear to also have\n" + "changed in the project. Merging will override changes in\n" + "the project with changes from the source code file."); if (num_uid_not_found) - msg += "\n\nWARNING: no Node can be found for %4$d of these\n" - "modifications and they can not be merged back."; + pos += snprintf(msg + pos, sizeof(msg) - pos, + "\n\nWARNING: no Node can be found for %%4$d of these\n" + "modifications and they can not be merged back."); if (!num_possible_override && !num_uid_not_found) - msg += "\nMerging these changes back appears to be safe."; + pos += snprintf(msg + pos, sizeof(msg) - pos, + "\nMerging these changes back appears to be safe."); if (num_changed_structure) - msg += "\n\nWARNING: %5$d modifications were found in the project\n" - "structure. These kind of changes can not be merged back\n" - "and will be lost when the source code is generated again\n" - "from the open project."; + pos += snprintf(msg + pos, sizeof(msg) - pos, + "\n\nWARNING: %%5$d modifications were found in the project\n" + "structure. These kind of changes can not be merged back\n" + "and will be lost when the source code is generated again\n" + "from the open project."); if (num_changed_code==num_uid_not_found) { - fl_message(msg.c_str(), - code_filename.c_str(), proj_filename.c_str(), + fl_message(msg, + code_filename, proj_filename, num_changed_code, num_uid_not_found, num_changed_structure, num_possible_override); return -1; } else { - msg += "\n\nClick Cancel to abort the MergeBack operation.\n" - "Click Merge to merge all code changes back into\n" - "the open project."; - int c = fl_choice(msg.c_str(), "Cancel", "Merge", 0, - code_filename.c_str(), proj_filename.c_str(), + pos += snprintf(msg + pos, sizeof(msg) - pos, + "\n\nClick Cancel to abort the MergeBack operation.\n" + "Click Merge to merge all code changes back into\n" + "the open project."); + int c = fl_choice(msg, "Cancel", "Merge", 0, + code_filename, proj_filename, num_changed_code, num_uid_not_found, num_changed_structure, num_possible_override); if (c==0) return -1; @@ -242,8 +247,14 @@ int Mergeback::ask_user_to_merge(const std::string &code_filename, const std::st void Mergeback::analyse_callback(unsigned long code_crc, unsigned long tag_crc, int uid) { Node *tp = proj_.tree.find_by_uid(uid); if (tp && tp->is_true_widget()) { - std::string cb = tp->callback(); cb += "\n"; - unsigned long project_crc = fld::io::Code_Writer::block_crc(cb.c_str()); + const char *cb = tp->callback(); + size_t len = cb ? strlen(cb) : 0; + char *cb_nl = (char*)malloc(len + 2); + if (cb && len) memcpy(cb_nl, cb, len); + cb_nl[len] = '\n'; + cb_nl[len + 1] = '\0'; + unsigned long project_crc = fld::io::Code_Writer::block_crc(cb_nl); + free(cb_nl); // check if the code and project crc are the same, so this modification was already applied if (project_crc!=code_crc) { num_changed_code++; @@ -264,8 +275,14 @@ void Mergeback::analyse_callback(unsigned long code_crc, unsigned long tag_crc, void Mergeback::analyse_code(unsigned long code_crc, unsigned long tag_crc, int uid) { Node *tp = proj_.tree.find_by_uid(uid); if (tp && tp->is_a(FLD_NODE_TYPE_Code)) { - std::string code = tp->name(); code += "\n"; - unsigned long project_crc = fld::io::Code_Writer::block_crc(code.c_str()); + const char *code = tp->name(); + size_t len = code ? strlen(code) : 0; + char *code_nl = (char*)malloc(len + 2); + if (code && len) memcpy(code_nl, code, len); + code_nl[len] = '\n'; + code_nl[len + 1] = '\0'; + unsigned long project_crc = fld::io::Code_Writer::block_crc(code_nl); + free(code_nl); // check if the code and project crc are the same, so this modification was already applied if (project_crc!=code_crc) { num_changed_code++; @@ -468,10 +485,18 @@ int Mergeback::analyse() { int Mergeback::apply_callback(long block_end, long block_start, unsigned long code_crc, int uid) { Node *tp = proj_.tree.find_by_uid(uid); if (tp && tp->is_true_widget()) { - std::string cb = tp->callback(); cb += "\n"; - unsigned long project_crc = fld::io::Code_Writer::block_crc(cb.c_str()); + const char *cb = tp->callback(); + size_t len = cb ? strlen(cb) : 0; + char *cb_nl = (char*)malloc(len + 2); + if (cb && len) memcpy(cb_nl, cb, len); + cb_nl[len] = '\n'; + cb_nl[len + 1] = '\0'; + unsigned long project_crc = fld::io::Code_Writer::block_crc(cb_nl); + free(cb_nl); if (project_crc!=code_crc) { - tp->callback(read_and_unindent_block(block_start, block_end).c_str()); + char *block = read_and_unindent_block(block_start, block_end); + tp->callback(block); + free(block); return 1; } } @@ -484,10 +509,18 @@ int Mergeback::apply_callback(long block_end, long block_start, unsigned long co int Mergeback::apply_code(long block_end, long block_start, unsigned long code_crc, int uid) { Node *tp = proj_.tree.find_by_uid(uid); if (tp && tp->is_a(FLD_NODE_TYPE_Code)) { - std::string cb = tp->name(); cb += "\n"; - unsigned long project_crc = fld::io::Code_Writer::block_crc(cb.c_str()); + const char *code = tp->name(); + size_t len = code ? strlen(code) : 0; + char *code_nl = (char*)malloc(len + 2); + if (code && len) memcpy(code_nl, code, len); + code_nl[len] = '\n'; + code_nl[len + 1] = '\0'; + unsigned long project_crc = fld::io::Code_Writer::block_crc(code_nl); + free(code_nl); if (project_crc!=code_crc) { - tp->name(read_and_unindent_block(block_start, block_end).c_str()); + char *block = read_and_unindent_block(block_start, block_end); + tp->name(block); + free(block); return 1; } } @@ -555,11 +588,11 @@ int Mergeback::apply() { FD_MERGEBACK_APPLY_IF_SAFE, or FD_MERGEBACK_APPLY \return -1 if an error was found in a tag \return -2 if no code file was found - \return See more at ::merge_back(const std::string &s, int task). + \return See more at ::merge_back(const char *s, int task). */ -int Mergeback::merge_back(const std::string &s, const std::string &p, Task task) { +int Mergeback::merge_back(const char *s, const char *p, Task task) { int ret = 0; - code = fl_fopen(s.c_str(), "rb"); + code = fl_fopen(s, "rb"); if (!code) return -2; do { // no actual loop, just make sure we close the code file if (task == FLD_MERGEBACK_TASK_ANALYSE) { @@ -616,9 +649,9 @@ int Mergeback::merge_back(const std::string &s, const std::string &p, Task task) \return 2 if mergeback is called recursively \return 1 if the project filename is not available \return 0 if MergeBack is not enabled, or the result of the merge_back function. - \see Mergeback::merge_back(const std::string &s, const std::string &p, Task task) + \see Mergeback::merge_back(const char *s, const char *p, Task task) */ -int mergeback_code_files(Project &proj, Mergeback::Feedback feedback) +int mergeback_code_files(fld::Project &proj, Mergeback::Feedback feedback) { static bool recursion_lock = false; if (recursion_lock) return 2; diff --git a/fluid/proj/mergeback.h b/fluid/proj/mergeback.h index 48cc4e400..04a9dc902 100644 --- a/fluid/proj/mergeback.h +++ b/fluid/proj/mergeback.h @@ -21,17 +21,11 @@ #include <stdint.h> #include <stdio.h> -#include <string> - -namespace fld { - -class Project; - -namespace proj { +namespace fld { class Project; } /** Class that implements the MergeBack functionality. - \see merge_back(const std::string &s, int task) + \see merge_back(const char *s, int task) */ enum { FLD_MERGEBACK_TAG_GENERIC = 0, @@ -56,7 +50,7 @@ public: enum Feedback { QUIET = 0, CHATTY = 1 }; protected: /// Apply mergeback for this project. - Project &proj_; + fld::Project &proj_; /// Pointer to the C++ code file. FILE *code; /// Current line number in the C++ code file. @@ -73,7 +67,7 @@ protected: int num_possible_override; void unindent(char *s); - std::string read_and_unindent_block(long start, long end); + char *read_and_unindent_block(long start, long end); void analyse_callback(unsigned long code_crc, unsigned long tag_crc, int uid); void analyse_code(unsigned long code_crc, unsigned long tag_crc, int uid); int apply_callback(long block_end, long block_start, unsigned long code_crc, int uid); @@ -86,20 +80,15 @@ protected: static bool read_tag(const char *tag, Tag *prev_type, uint16_t *uid, uint32_t *crc); public: - Mergeback(Project &proj); + Mergeback(fld::Project &proj); ~Mergeback(); - int merge_back(const std::string &s, const std::string &p, Task task); - int ask_user_to_merge(const std::string &s, const std::string &p); + int merge_back(const char *s, const char *p, Task task); + int ask_user_to_merge(const char *s, const char *p); int analyse(); int apply(); static void print_tag(FILE *out, Tag prev_type, Tag next_type, uint16_t uid, uint32_t crc); }; -extern int merge_back(const std::string &s, const std::string &p, int task); - -} // namespace proj -} // namespace fld - extern void start_auto_mergeback(); extern void mergeback_on_load(); diff --git a/fluid/proj/undo.cxx b/fluid/proj/undo.cxx index 01d1b68c2..e4ee89bf6 100644 --- a/fluid/proj/undo.cxx +++ b/fluid/proj/undo.cxx @@ -103,7 +103,7 @@ void Undo::redo() { return; } if (reload_panel) { - for (auto w: Fluid.proj.tree.all_selected_widgets()) { + for (Widget_Node *w: Fluid.proj.tree.all_selected_widgets()) { w->open(); } } |
