From 6efece51f99980b824b199bbeb6452f03e6736c6 Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Tue, 27 Sep 2016 00:23:14 +0700 Subject: add x-verbs support (bzr r15136.1.1) --- src/main-cmdlinexact.cpp | 526 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 526 insertions(+) create mode 100644 src/main-cmdlinexact.cpp (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp new file mode 100644 index 000000000..9efbfe8cd --- /dev/null +++ b/src/main-cmdlinexact.cpp @@ -0,0 +1,526 @@ +/* + * Authors: + * Dmitry Zhulanov + * + * Copyright (C) 2016 Authors + * + * Released under GNU GPL v2, read the file 'COPYING' for more information + * + * Format of xverbs.yaml + * + * verbose: yes # only "verbose: yes" enable logging + * run: + * # open document to process + * - xverb-id: XFileOpen, gfx_sources/loading_screen/sandclock_atlas.svg + * - xverb-id: XUndoLabel, fresh_document # set label for UndoToLabel xverb works + * # note: if something wrong with undo labels use verb EditUndo instead of XUndoLabel and UndoToLabel at all + * + * # select element to handle + * - xverb-id: XSelectElement, top_sand + * + * # verbs + * - verb-id: EditInvertInAllLayers + * - verb-id: EditDelete + * - verb-id: FitCanvasToDrawing + * + * # save element to separated svg document + * - xverb-id: XFileSaveAs, output/thegame/linux/data/gfx/loading_screen/top_sand.svg + * + * # also save png preview + * - xverb-id: XFileExportPNG, output/thegame/linux/data/gfx_preview/loading_screen/top_sand.png + * + * # return to the fresh_state of document + * - xverb-id: UndoToLabel, fresh_document + * + * # do any other handling + * + * # inkscape have a lot of useful verbs + * - verb-id: FileQuit + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include "main-cmdlinexact.h" +#include "yaml.h" +#include "extension/system.h" +#include "file.h" +#include +#include "sp-root.h" +#include "document-undo.h" +#include "util/units.h" +#include "sp-namedview.h" +#include "resource-manager.h" +#include "ui/dialog/font-substitution.h" +#include "extension/db.h" +#include "preferences.h" +#include "helper/png-write.h" +#include +#include +#include + +#define DPI_BASE Inkscape::Util::Quantity::convert(1, "in", "px") + +namespace +{ +bool s_verbose = false; + +bool createDirForFilename( const std::string &filename ) +{ + size_t found = filename.find_last_of("/\\"); + std::string output_directory = filename.substr(0,found); + + if( output_directory == filename ) + return true; + + if (g_mkdir_with_parents(output_directory.c_str(), 0755)) + { + printf("Can't create directory %s\n", output_directory.c_str()); + fflush(stdout); + + return false; + } + + return true; +} + +void xFileOpen( const Glib::ustring &uri ) +{ + if (s_verbose) { + printf("open %s\n", uri.c_str()); + fflush(stdout); + } + + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + if (desktop) { + SPDocument *old_document = desktop->getDocument(); + desktop->setWaitingCursor(); + Inkscape::DocumentUndo::clearRedo(old_document); + } + + SPDocument *doc = NULL; + Inkscape::Extension::Extension *key = NULL; + try { + doc = Inkscape::Extension::open(key, uri.c_str()); + } catch (std::exception &e) { + doc = NULL; + std::string exeption_mgs = e.what(); + printf("Error: open %s:%s\n",uri.c_str(), exeption_mgs.c_str() ); + fflush(stdout); + } + + // Set viewBox if it doesn't exist + if (!doc->getRoot()->viewBox_set + && (doc->getRoot()->width.unit != SVGLength::PERCENT) + && (doc->getRoot()->height.unit != SVGLength::PERCENT)) { + doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value(doc->getDisplayUnit()), doc->getHeight().value(doc->getDisplayUnit()))); + } + + desktop->change_document(doc); + doc->emitResizedSignal(doc->getWidth().value("px"), doc->getHeight().value("px")); + if(desktop) + desktop->clearWaitingCursor(); + + doc->virgin = FALSE; + + // everyone who cares now has a reference, get rid of our`s + doc->doUnref(); + + // resize the window to match the document properties + sp_namedview_window_from_document(desktop); + sp_namedview_update_layers_from_document(desktop); + + if ( INKSCAPE.use_gui() ) { + // Perform a fixup pass for hrefs. + if ( Inkscape::ResourceManager::getManager().fixupBrokenLinks(doc) ) { + Glib::ustring msg = _("Broken links have been changed to point to existing files."); + desktop->showInfoDialog(msg); + } + + // Check for font substitutions + Inkscape::UI::Dialog::FontSubstitution::getInstance().checkFontSubstitutions(doc); + } +} + +void xFileSaveAs( Inkscape::ActionContext const & context, const Glib::ustring &uri ) +{ + SPDocument *doc = context.getDocument(); + if (s_verbose) { + printf("save as %s\n", uri.c_str()); + fflush(stdout); + } + + if( createDirForFilename( uri )) { + Inkscape::Extension::save( + Inkscape::Extension::db.get("org.inkscape.output.svg.inkscape"), + doc, uri.c_str(), false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS); + if (s_verbose) { + printf("save done: %s\n", uri.c_str() ); + fflush(stdout); + } + } + else + { + printf("can't create dirs for filename %s\n", uri.c_str() ); + fflush(stdout); + } +} + +void xFileExportPNG( Inkscape::ActionContext const & context, const Glib::ustring &uri ) +{ + if (s_verbose) { + printf("export png %s\n", uri.c_str()); + fflush(stdout); + } + + SPDocument *doc = context.getDocument(); + + gdouble dpi = 200.0; + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + dpi = prefs->getDouble("/dialogs/export/defaultxdpi/value", DPI_BASE); + + gdouble width = doc->getWidth().value(doc->getDisplayUnit()); + gdouble height = doc->getHeight().value(doc->getDisplayUnit()); + + gdouble bmwidth = (width) * dpi / DPI_BASE; + gdouble bmheight = (height) * dpi / DPI_BASE; + + int png_width = (int)(0.5 + bmwidth); + int png_height = (int)(0.5 + bmheight); + + SPNamedView *nv = desktop->getNamedView(); + + ExportResult status = sp_export_png_file(doc, uri.c_str(), + Geom::Rect(Geom::Point(0,0), Geom::Point(width, height)), png_width, png_height, dpi, dpi, + nv->pagecolor, 0, 0, TRUE); +} + +void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustring &uri ) +{ + if (context.getDocument() == NULL || context.getSelection() == NULL) { return; } + + if (s_verbose) { + printf("select element: %s\n", uri.c_str()); + fflush(stdout); + } + + SPDocument * doc = context.getDocument(); + SPObject * obj = doc->getObjectById(uri); + + if (obj == NULL) { + printf(_("Unable to find node ID: '%s'\n"), uri.c_str()); + fflush(stdout); + return; + } + + Inkscape::Selection * selection = context.getSelection(); + selection->add(obj); + + if (s_verbose) { + printf("select done %s\n", uri.c_str()); + fflush(stdout); + } +} + +} // end of unnamed namespace + +namespace Inkscape { + +CmdLineXAction::CmdLineXAction (gchar const * arg, xaction_args_values_map_t &values_map): + CmdLineAction(true, arg), _values_map(values_map) { + this->arg = (char *)arg; + return; +} + +bool +CmdLineXAction::isExtended() { + return true; +} + +void +CmdLineXAction::doItX (ActionContext const & context) { + (void)(context); + + if( arg == "XFileSaveAs") + xFileSaveAs( context, _values_map["filename"] ); + else if (arg == "XFileOpen") + xFileOpen( _values_map["filename"] ); + else if (arg == "XFileExportPNG") + xFileExportPNG( context, _values_map["png_filename"] ); + else if (arg == "XSelectElement") + xSelectElement( context, _values_map["element-id"] ); + else + { + printf("unknown xverb: %s", arg.c_str()); + fflush(stdout); + } + + return; +} + +enum parser_state_t{ HANDLING_ROOT, + HANDLING_VERBOSE, // options + HANDLING_RUN, HANDLING_RUN_LIST, HANDLING_RUN_LIST_ENTRY }; // run entries + +struct verb_info_t +{ + bool xverb; + std::vector args; +}; + +typedef std::list verbs_list_t; + +static void tokenize(const std::string& str, std::vector& tokens, const std::string& delimiters = ",") +{ + // Skip delimiters at beginning. + std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); + + // Find first non-delimiter. + std::string::size_type pos = str.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) { + // Found a token, add it to the vector. + tokens.push_back(str.substr(lastPos, pos - lastPos)); + + // Skip delimiters. + lastPos = str.find_first_not_of(delimiters, pos); + + // Find next non-delimiter. + pos = str.find_first_of(delimiters, lastPos); + } +} + +void +CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { + FILE *fh = fopen(yaml_filename, "r"); + if(fh == NULL) { + printf("Failed to open file!\n"); + fflush(stdout); + return; + } + + yaml_parser_t parser; + if(!yaml_parser_initialize(&parser)) { + printf("Failed to initialize parser!\n"); + fflush(stdout); + return; + } + + /* Set input file */ + yaml_parser_set_input_file(&parser, fh); + + parser_state_t state = HANDLING_ROOT; + + + bool handling_key = false; + bool handling_value = false; + + std::string key; + verbs_list_t verbs_list; + + // parse + yaml_token_t token; + do { + yaml_parser_scan(&parser, &token); + switch(token.type) + { + // avoid "warning: enumeration value", "-Wswitch" + case YAML_NO_TOKEN: break; + case YAML_STREAM_START_TOKEN: break; + case YAML_STREAM_END_TOKEN: break; + case YAML_VERSION_DIRECTIVE_TOKEN: break; + case YAML_TAG_DIRECTIVE_TOKEN: break; + case YAML_DOCUMENT_START_TOKEN: break; + case YAML_DOCUMENT_END_TOKEN: break; + case YAML_FLOW_SEQUENCE_START_TOKEN: break; + case YAML_FLOW_SEQUENCE_END_TOKEN: break; + case YAML_FLOW_MAPPING_START_TOKEN: break; + case YAML_FLOW_MAPPING_END_TOKEN: break; + case YAML_FLOW_ENTRY_TOKEN: break; + case YAML_ALIAS_TOKEN: break; + case YAML_ANCHOR_TOKEN: break; + case YAML_TAG_TOKEN: break; + + /* Token types (read before actual token) */ + case YAML_KEY_TOKEN: + handling_key = true; + handling_value = false; + break; + case YAML_VALUE_TOKEN: + handling_key = false; + handling_value = true; + break; + + /* Block delimeters */ + case YAML_BLOCK_SEQUENCE_START_TOKEN: + if( state == HANDLING_ROOT ) + { + if( key == "run" ) + state = HANDLING_RUN; + } + break; + case YAML_BLOCK_ENTRY_TOKEN: + if( state == HANDLING_RUN ) + state = HANDLING_RUN_LIST; + else if( state == HANDLING_RUN_LIST ) + state = HANDLING_RUN_LIST_ENTRY; + else if( state == HANDLING_VERBOSE ) + state = HANDLING_ROOT; + break; + case YAML_BLOCK_END_TOKEN: + if( state == HANDLING_RUN_LIST_ENTRY ) + state = HANDLING_RUN_LIST; + else if( state == HANDLING_RUN_LIST ) + state = HANDLING_RUN; + else if( state == HANDLING_VERBOSE ) + state = HANDLING_ROOT; + else if( state == HANDLING_RUN ) + state = HANDLING_ROOT; + break; + + /* Data */ + case YAML_BLOCK_MAPPING_START_TOKEN: + break; + case YAML_SCALAR_TOKEN: + if( handling_key ) + key = (char *)token.data.scalar.value; + else if ( handling_value ) + { + if(state == HANDLING_RUN_LIST) + { + if(key == "xverb-id") + { + verb_info_t verb; + verb.xverb = true; + + std::string values = (char *)token.data.scalar.value; + tokenize(values, verb.args); + for( size_t i = 0; i < verb.args.size(); ++i ) + { + verb.args[i].erase(0, verb.args[i].find_first_not_of(' ')); //prefixing spaces + verb.args[i].erase(verb.args[i].find_last_not_of(' ')+1); //surfixing spaces + } + verbs_list.push_back(verb); + } + else if(key == "verb-id") + { + verb_info_t verb; + verb.xverb = false; + verb.args.push_back((char *)token.data.scalar.value); + verbs_list.push_back(verb); + } + else + { + printf("unknown verb type [%s]\n", key.c_str()); + fflush(stdout); + } + } + else if(state == HANDLING_ROOT) + { + std::string value = (char *)token.data.scalar.value; + if( (key == "verbose") && (value == "yes") ) + s_verbose = true; + } + } + break; + } + } while(token.type != YAML_STREAM_END_TOKEN); + + /* Cleanup */ + yaml_token_delete(&token); + yaml_parser_delete(&parser); + fclose(fh); + + typedef std::map undo_labels_map_t; + undo_labels_map_t undo_labels_map; + int undo_counter = 0; + + verbs_list_t::iterator iter = verbs_list.begin(); + for( ; iter != verbs_list.end(); ++iter ) + { + verb_info_t &verb = *iter; + std::string &verb_word = verb.args[0]; + if( s_verbose ) + printf("handle %s and args count is %d\n", verb_word.c_str(), (int)verb.args.size()); + + if( verb.args.size() == 2 ) + { + xaction_args_values_map_t values_map; + if (verb_word == "XFileSaveAs" || verb_word == "XFileOpen") + { + std::string &filename = verb.args[1]; + values_map["filename"] = filename; + new CmdLineXAction(verb_word.c_str(), values_map); + } + else if (verb_word == "XUndoLabel") + undo_labels_map[verb.args[1]] = undo_counter; + else if (verb_word == "UndoToLabel") + { + undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); + if(iter != undo_labels_map.end()) + { + int counter = undo_counter - iter->second; + if( counter > 0 ) + { + for(int i = 0; i < counter; ++i) + new CmdLineAction(true, "EditUndo"); + undo_counter -= counter; + } + } + } + else if (verb_word == "XSelectElement") + { + ++undo_counter; + values_map["element-id"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } + else if (verb_word == "XFileExportPNG") + { + std::string &png_filename = verb.args[1]; + values_map["png_filename"] = png_filename; + if(createDirForFilename( png_filename )) { + new CmdLineXAction(verb_word.c_str(), values_map); + } + } + } + else if(!verb.xverb) + { + ++undo_counter; + new CmdLineAction(true, verb.args[0].c_str()); + } + + else + { + printf("Unhadled verb %s\n", verb.args[0].c_str()); + fflush(stdout); + } + } + + fflush(stdout); + return; +} + + +} // Inkscape + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 : -- cgit v1.2.3 From 38cd1793fb8ac7983c001834193d41b5b5b4e660 Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Tue, 27 Sep 2016 21:17:03 +0700 Subject: update intends and brackets (bzr r15136.1.3) --- src/main-cmdlinexact.cpp | 661 +++++++++++++++++++++++------------------------ 1 file changed, 320 insertions(+), 341 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 9efbfe8cd..940abfca4 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -7,6 +7,7 @@ * Released under GNU GPL v2, read the file 'COPYING' for more information * * Format of xverbs.yaml + * using: $ inkscape -B xverbs.yaml * * verbose: yes # only "verbose: yes" enable logging * run: @@ -80,29 +81,28 @@ bool createDirForFilename( const std::string &filename ) std::string output_directory = filename.substr(0,found); if( output_directory == filename ) - return true; + return true; - if (g_mkdir_with_parents(output_directory.c_str(), 0755)) - { - printf("Can't create directory %s\n", output_directory.c_str()); - fflush(stdout); + if (g_mkdir_with_parents(output_directory.c_str(), 0755)) { + printf("Can't create directory %s\n", output_directory.c_str()); + fflush(stdout); - return false; - } + return false; + } return true; } void xFileOpen( const Glib::ustring &uri ) { - if (s_verbose) { - printf("open %s\n", uri.c_str()); - fflush(stdout); - } + if (s_verbose) { + printf("open %s\n", uri.c_str()); + fflush(stdout); + } SPDesktop *desktop = SP_ACTIVE_DESKTOP; if (desktop) { - SPDocument *old_document = desktop->getDocument(); + SPDocument *old_document = desktop->getDocument(); desktop->setWaitingCursor(); Inkscape::DocumentUndo::clearRedo(old_document); } @@ -114,123 +114,122 @@ void xFileOpen( const Glib::ustring &uri ) } catch (std::exception &e) { doc = NULL; std::string exeption_mgs = e.what(); - printf("Error: open %s:%s\n",uri.c_str(), exeption_mgs.c_str() ); - fflush(stdout); + printf("Error: open %s:%s\n",uri.c_str(), exeption_mgs.c_str() ); + fflush(stdout); } - // Set viewBox if it doesn't exist - if (!doc->getRoot()->viewBox_set - && (doc->getRoot()->width.unit != SVGLength::PERCENT) - && (doc->getRoot()->height.unit != SVGLength::PERCENT)) { - doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value(doc->getDisplayUnit()), doc->getHeight().value(doc->getDisplayUnit()))); - } - - desktop->change_document(doc); - doc->emitResizedSignal(doc->getWidth().value("px"), doc->getHeight().value("px")); - if(desktop) - desktop->clearWaitingCursor(); - - doc->virgin = FALSE; - - // everyone who cares now has a reference, get rid of our`s - doc->doUnref(); - - // resize the window to match the document properties - sp_namedview_window_from_document(desktop); - sp_namedview_update_layers_from_document(desktop); - - if ( INKSCAPE.use_gui() ) { - // Perform a fixup pass for hrefs. - if ( Inkscape::ResourceManager::getManager().fixupBrokenLinks(doc) ) { - Glib::ustring msg = _("Broken links have been changed to point to existing files."); - desktop->showInfoDialog(msg); - } - - // Check for font substitutions - Inkscape::UI::Dialog::FontSubstitution::getInstance().checkFontSubstitutions(doc); - } + // Set viewBox if it doesn't exist + if (!doc->getRoot()->viewBox_set + && (doc->getRoot()->width.unit != SVGLength::PERCENT) + && (doc->getRoot()->height.unit != SVGLength::PERCENT)) { + doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value(doc->getDisplayUnit()), doc->getHeight().value(doc->getDisplayUnit()))); + } + + desktop->change_document(doc); + doc->emitResizedSignal(doc->getWidth().value("px"), doc->getHeight().value("px")); + if(desktop) + desktop->clearWaitingCursor(); + + doc->virgin = FALSE; + + // everyone who cares now has a reference, get rid of our`s + doc->doUnref(); + + // resize the window to match the document properties + sp_namedview_window_from_document(desktop); + sp_namedview_update_layers_from_document(desktop); + + if ( INKSCAPE.use_gui() ) { + // Perform a fixup pass for hrefs. + if ( Inkscape::ResourceManager::getManager().fixupBrokenLinks(doc) ) { + Glib::ustring msg = _("Broken links have been changed to point to existing files."); + desktop->showInfoDialog(msg); + } + + // Check for font substitutions + Inkscape::UI::Dialog::FontSubstitution::getInstance().checkFontSubstitutions(doc); + } } void xFileSaveAs( Inkscape::ActionContext const & context, const Glib::ustring &uri ) { - SPDocument *doc = context.getDocument(); - if (s_verbose) { - printf("save as %s\n", uri.c_str()); - fflush(stdout); - } - - if( createDirForFilename( uri )) { - Inkscape::Extension::save( - Inkscape::Extension::db.get("org.inkscape.output.svg.inkscape"), - doc, uri.c_str(), false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS); - if (s_verbose) { - printf("save done: %s\n", uri.c_str() ); - fflush(stdout); - } - } - else - { - printf("can't create dirs for filename %s\n", uri.c_str() ); - fflush(stdout); - } + SPDocument *doc = context.getDocument(); + if (s_verbose) { + printf("save as %s\n", uri.c_str()); + fflush(stdout); + } + + if( createDirForFilename( uri )) { + Inkscape::Extension::save( + Inkscape::Extension::db.get("org.inkscape.output.svg.inkscape"), + doc, uri.c_str(), false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS); + if (s_verbose) { + printf("save done: %s\n", uri.c_str() ); + fflush(stdout); + } + } + else { + printf("can't create dirs for filename %s\n", uri.c_str() ); + fflush(stdout); + } } void xFileExportPNG( Inkscape::ActionContext const & context, const Glib::ustring &uri ) { - if (s_verbose) { - printf("export png %s\n", uri.c_str()); - fflush(stdout); - } - - SPDocument *doc = context.getDocument(); - - gdouble dpi = 200.0; - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - dpi = prefs->getDouble("/dialogs/export/defaultxdpi/value", DPI_BASE); - - gdouble width = doc->getWidth().value(doc->getDisplayUnit()); - gdouble height = doc->getHeight().value(doc->getDisplayUnit()); - - gdouble bmwidth = (width) * dpi / DPI_BASE; - gdouble bmheight = (height) * dpi / DPI_BASE; - - int png_width = (int)(0.5 + bmwidth); - int png_height = (int)(0.5 + bmheight); - - SPNamedView *nv = desktop->getNamedView(); - - ExportResult status = sp_export_png_file(doc, uri.c_str(), - Geom::Rect(Geom::Point(0,0), Geom::Point(width, height)), png_width, png_height, dpi, dpi, - nv->pagecolor, 0, 0, TRUE); + if (s_verbose) { + printf("export png %s\n", uri.c_str()); + fflush(stdout); + } + + SPDocument *doc = context.getDocument(); + + gdouble dpi = 200.0; + SPDesktop *desktop = SP_ACTIVE_DESKTOP; + + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + dpi = prefs->getDouble("/dialogs/export/defaultxdpi/value", DPI_BASE); + + gdouble width = doc->getWidth().value(doc->getDisplayUnit()); + gdouble height = doc->getHeight().value(doc->getDisplayUnit()); + + gdouble bmwidth = (width) * dpi / DPI_BASE; + gdouble bmheight = (height) * dpi / DPI_BASE; + + int png_width = (int)(0.5 + bmwidth); + int png_height = (int)(0.5 + bmheight); + + SPNamedView *nv = desktop->getNamedView(); + + ExportResult status = sp_export_png_file(doc, uri.c_str(), + Geom::Rect(Geom::Point(0,0), Geom::Point(width, height)), png_width, png_height, dpi, dpi, + nv->pagecolor, 0, 0, TRUE); } void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustring &uri ) { - if (context.getDocument() == NULL || context.getSelection() == NULL) { return; } + if (context.getDocument() == NULL || context.getSelection() == NULL) { return; } - if (s_verbose) { - printf("select element: %s\n", uri.c_str()); - fflush(stdout); - } + if (s_verbose) { + printf("select element: %s\n", uri.c_str()); + fflush(stdout); + } - SPDocument * doc = context.getDocument(); - SPObject * obj = doc->getObjectById(uri); + SPDocument * doc = context.getDocument(); + SPObject * obj = doc->getObjectById(uri); - if (obj == NULL) { - printf(_("Unable to find node ID: '%s'\n"), uri.c_str()); - fflush(stdout); - return; - } + if (obj == NULL) { + printf(_("Unable to find node ID: '%s'\n"), uri.c_str()); + fflush(stdout); + return; + } - Inkscape::Selection * selection = context.getSelection(); - selection->add(obj); + Inkscape::Selection * selection = context.getSelection(); + selection->add(obj); - if (s_verbose) { - printf("select done %s\n", uri.c_str()); - fflush(stdout); - } + if (s_verbose) { + printf("select done %s\n", uri.c_str()); + fflush(stdout); + } } } // end of unnamed namespace @@ -238,45 +237,44 @@ void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustrin namespace Inkscape { CmdLineXAction::CmdLineXAction (gchar const * arg, xaction_args_values_map_t &values_map): - CmdLineAction(true, arg), _values_map(values_map) { - this->arg = (char *)arg; - return; + CmdLineAction(true, arg), _values_map(values_map) { + this->arg = (char *)arg; + return; } bool CmdLineXAction::isExtended() { - return true; + return true; } void CmdLineXAction::doItX (ActionContext const & context) { - (void)(context); - - if( arg == "XFileSaveAs") - xFileSaveAs( context, _values_map["filename"] ); - else if (arg == "XFileOpen") - xFileOpen( _values_map["filename"] ); - else if (arg == "XFileExportPNG") - xFileExportPNG( context, _values_map["png_filename"] ); - else if (arg == "XSelectElement") - xSelectElement( context, _values_map["element-id"] ); - else - { - printf("unknown xverb: %s", arg.c_str()); - fflush(stdout); - } - - return; + (void)(context); + + if( arg == "XFileSaveAs") + xFileSaveAs( context, _values_map["filename"] ); + else if (arg == "XFileOpen") + xFileOpen( _values_map["filename"] ); + else if (arg == "XFileExportPNG") + xFileExportPNG( context, _values_map["png_filename"] ); + else if (arg == "XSelectElement") + xSelectElement( context, _values_map["element-id"] ); + else { + printf("unknown xverb: %s", arg.c_str()); + fflush(stdout); + } + + return; } enum parser_state_t{ HANDLING_ROOT, - HANDLING_VERBOSE, // options - HANDLING_RUN, HANDLING_RUN_LIST, HANDLING_RUN_LIST_ENTRY }; // run entries + HANDLING_VERBOSE, // options + HANDLING_RUN, HANDLING_RUN_LIST, HANDLING_RUN_LIST_ENTRY }; // run entries struct verb_info_t { - bool xverb; - std::vector args; + bool xverb; + std::vector args; }; typedef std::list verbs_list_t; @@ -303,212 +301,193 @@ static void tokenize(const std::string& str, std::vector& tokens, c void CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { - FILE *fh = fopen(yaml_filename, "r"); - if(fh == NULL) { - printf("Failed to open file!\n"); - fflush(stdout); - return; - } - - yaml_parser_t parser; - if(!yaml_parser_initialize(&parser)) { - printf("Failed to initialize parser!\n"); - fflush(stdout); - return; - } - - /* Set input file */ - yaml_parser_set_input_file(&parser, fh); - - parser_state_t state = HANDLING_ROOT; - - - bool handling_key = false; - bool handling_value = false; - - std::string key; - verbs_list_t verbs_list; - - // parse - yaml_token_t token; - do { - yaml_parser_scan(&parser, &token); - switch(token.type) - { - // avoid "warning: enumeration value", "-Wswitch" - case YAML_NO_TOKEN: break; - case YAML_STREAM_START_TOKEN: break; - case YAML_STREAM_END_TOKEN: break; - case YAML_VERSION_DIRECTIVE_TOKEN: break; - case YAML_TAG_DIRECTIVE_TOKEN: break; - case YAML_DOCUMENT_START_TOKEN: break; - case YAML_DOCUMENT_END_TOKEN: break; - case YAML_FLOW_SEQUENCE_START_TOKEN: break; - case YAML_FLOW_SEQUENCE_END_TOKEN: break; - case YAML_FLOW_MAPPING_START_TOKEN: break; - case YAML_FLOW_MAPPING_END_TOKEN: break; - case YAML_FLOW_ENTRY_TOKEN: break; - case YAML_ALIAS_TOKEN: break; - case YAML_ANCHOR_TOKEN: break; - case YAML_TAG_TOKEN: break; - - /* Token types (read before actual token) */ - case YAML_KEY_TOKEN: - handling_key = true; - handling_value = false; - break; - case YAML_VALUE_TOKEN: - handling_key = false; - handling_value = true; - break; - - /* Block delimeters */ - case YAML_BLOCK_SEQUENCE_START_TOKEN: - if( state == HANDLING_ROOT ) - { - if( key == "run" ) - state = HANDLING_RUN; - } - break; - case YAML_BLOCK_ENTRY_TOKEN: - if( state == HANDLING_RUN ) - state = HANDLING_RUN_LIST; - else if( state == HANDLING_RUN_LIST ) - state = HANDLING_RUN_LIST_ENTRY; - else if( state == HANDLING_VERBOSE ) - state = HANDLING_ROOT; - break; - case YAML_BLOCK_END_TOKEN: - if( state == HANDLING_RUN_LIST_ENTRY ) - state = HANDLING_RUN_LIST; - else if( state == HANDLING_RUN_LIST ) - state = HANDLING_RUN; - else if( state == HANDLING_VERBOSE ) - state = HANDLING_ROOT; - else if( state == HANDLING_RUN ) - state = HANDLING_ROOT; - break; - - /* Data */ - case YAML_BLOCK_MAPPING_START_TOKEN: - break; - case YAML_SCALAR_TOKEN: - if( handling_key ) - key = (char *)token.data.scalar.value; - else if ( handling_value ) - { - if(state == HANDLING_RUN_LIST) - { - if(key == "xverb-id") - { - verb_info_t verb; - verb.xverb = true; - - std::string values = (char *)token.data.scalar.value; - tokenize(values, verb.args); - for( size_t i = 0; i < verb.args.size(); ++i ) - { - verb.args[i].erase(0, verb.args[i].find_first_not_of(' ')); //prefixing spaces - verb.args[i].erase(verb.args[i].find_last_not_of(' ')+1); //surfixing spaces - } - verbs_list.push_back(verb); - } - else if(key == "verb-id") - { - verb_info_t verb; - verb.xverb = false; - verb.args.push_back((char *)token.data.scalar.value); - verbs_list.push_back(verb); - } - else - { - printf("unknown verb type [%s]\n", key.c_str()); - fflush(stdout); - } - } - else if(state == HANDLING_ROOT) - { - std::string value = (char *)token.data.scalar.value; - if( (key == "verbose") && (value == "yes") ) - s_verbose = true; - } - } - break; - } - } while(token.type != YAML_STREAM_END_TOKEN); - - /* Cleanup */ - yaml_token_delete(&token); - yaml_parser_delete(&parser); - fclose(fh); - - typedef std::map undo_labels_map_t; - undo_labels_map_t undo_labels_map; - int undo_counter = 0; - - verbs_list_t::iterator iter = verbs_list.begin(); - for( ; iter != verbs_list.end(); ++iter ) - { - verb_info_t &verb = *iter; - std::string &verb_word = verb.args[0]; - if( s_verbose ) - printf("handle %s and args count is %d\n", verb_word.c_str(), (int)verb.args.size()); - - if( verb.args.size() == 2 ) - { - xaction_args_values_map_t values_map; - if (verb_word == "XFileSaveAs" || verb_word == "XFileOpen") - { - std::string &filename = verb.args[1]; - values_map["filename"] = filename; - new CmdLineXAction(verb_word.c_str(), values_map); - } - else if (verb_word == "XUndoLabel") - undo_labels_map[verb.args[1]] = undo_counter; - else if (verb_word == "UndoToLabel") - { - undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); - if(iter != undo_labels_map.end()) - { - int counter = undo_counter - iter->second; - if( counter > 0 ) - { - for(int i = 0; i < counter; ++i) - new CmdLineAction(true, "EditUndo"); - undo_counter -= counter; - } - } - } - else if (verb_word == "XSelectElement") - { - ++undo_counter; - values_map["element-id"] = verb.args[1]; - new CmdLineXAction(verb_word.c_str(), values_map); - } - else if (verb_word == "XFileExportPNG") - { - std::string &png_filename = verb.args[1]; - values_map["png_filename"] = png_filename; - if(createDirForFilename( png_filename )) { - new CmdLineXAction(verb_word.c_str(), values_map); - } - } - } - else if(!verb.xverb) - { - ++undo_counter; - new CmdLineAction(true, verb.args[0].c_str()); - } - - else - { - printf("Unhadled verb %s\n", verb.args[0].c_str()); - fflush(stdout); - } - } - - fflush(stdout); - return; + FILE *fh = fopen(yaml_filename, "r"); + if(fh == NULL) { + printf("Failed to open file!\n"); + fflush(stdout); + return; + } + + yaml_parser_t parser; + if(!yaml_parser_initialize(&parser)) { + printf("Failed to initialize parser!\n"); + fflush(stdout); + return; + } + + /* Set input file */ + yaml_parser_set_input_file(&parser, fh); + + parser_state_t state = HANDLING_ROOT; + + + bool handling_key = false; + bool handling_value = false; + + std::string key; + verbs_list_t verbs_list; + + // parse + yaml_token_t token; + do { + yaml_parser_scan(&parser, &token); + switch(token.type) + { + // avoid "warning: enumeration value", "-Wswitch" + case YAML_NO_TOKEN: break; + case YAML_STREAM_START_TOKEN: break; + case YAML_STREAM_END_TOKEN: break; + case YAML_VERSION_DIRECTIVE_TOKEN: break; + case YAML_TAG_DIRECTIVE_TOKEN: break; + case YAML_DOCUMENT_START_TOKEN: break; + case YAML_DOCUMENT_END_TOKEN: break; + case YAML_FLOW_SEQUENCE_START_TOKEN: break; + case YAML_FLOW_SEQUENCE_END_TOKEN: break; + case YAML_FLOW_MAPPING_START_TOKEN: break; + case YAML_FLOW_MAPPING_END_TOKEN: break; + case YAML_FLOW_ENTRY_TOKEN: break; + case YAML_ALIAS_TOKEN: break; + case YAML_ANCHOR_TOKEN: break; + case YAML_TAG_TOKEN: break; + + /* Token types (read before actual token) */ + case YAML_KEY_TOKEN: + handling_key = true; + handling_value = false; + break; + case YAML_VALUE_TOKEN: + handling_key = false; + handling_value = true; + break; + + /* Block delimeters */ + case YAML_BLOCK_SEQUENCE_START_TOKEN: + if( state == HANDLING_ROOT ) { + if( key == "run" ) + state = HANDLING_RUN; + } + break; + case YAML_BLOCK_ENTRY_TOKEN: + if( state == HANDLING_RUN ) + state = HANDLING_RUN_LIST; + else if( state == HANDLING_RUN_LIST ) + state = HANDLING_RUN_LIST_ENTRY; + else if( state == HANDLING_VERBOSE ) + state = HANDLING_ROOT; + break; + case YAML_BLOCK_END_TOKEN: + if( state == HANDLING_RUN_LIST_ENTRY ) + state = HANDLING_RUN_LIST; + else if( state == HANDLING_RUN_LIST ) + state = HANDLING_RUN; + else if( state == HANDLING_VERBOSE ) + state = HANDLING_ROOT; + else if( state == HANDLING_RUN ) + state = HANDLING_ROOT; + break; + + /* Data */ + case YAML_BLOCK_MAPPING_START_TOKEN: + break; + case YAML_SCALAR_TOKEN: + if( handling_key ) + key = (char *)token.data.scalar.value; + else if ( handling_value ) { + if(state == HANDLING_RUN_LIST) { + if(key == "xverb-id") { + verb_info_t verb; + verb.xverb = true; + + std::string values = (char *)token.data.scalar.value; + tokenize(values, verb.args); + for( size_t i = 0; i < verb.args.size(); ++i ) { + verb.args[i].erase(0, verb.args[i].find_first_not_of(' ')); //prefixing spaces + verb.args[i].erase(verb.args[i].find_last_not_of(' ')+1); //surfixing spaces + } + verbs_list.push_back(verb); + } + else if(key == "verb-id") { + verb_info_t verb; + verb.xverb = false; + verb.args.push_back((char *)token.data.scalar.value); + verbs_list.push_back(verb); + } + else { + printf("unknown verb type [%s]\n", key.c_str()); + fflush(stdout); + } + } + else if(state == HANDLING_ROOT) { + std::string value = (char *)token.data.scalar.value; + if( (key == "verbose") && (value == "yes") ) + s_verbose = true; + } + } + break; + } + } while(token.type != YAML_STREAM_END_TOKEN); + + /* Cleanup */ + yaml_token_delete(&token); + yaml_parser_delete(&parser); + fclose(fh); + + typedef std::map undo_labels_map_t; + undo_labels_map_t undo_labels_map; + int undo_counter = 0; + + verbs_list_t::iterator iter = verbs_list.begin(); + for( ; iter != verbs_list.end(); ++iter ) { + verb_info_t &verb = *iter; + std::string &verb_word = verb.args[0]; + if( s_verbose ) + printf("handle %s and args count is %d\n", verb_word.c_str(), (int)verb.args.size()); + + if( verb.args.size() == 2 ) { + xaction_args_values_map_t values_map; + if (verb_word == "XFileSaveAs" || verb_word == "XFileOpen") { + std::string &filename = verb.args[1]; + values_map["filename"] = filename; + new CmdLineXAction(verb_word.c_str(), values_map); + } + else if (verb_word == "XUndoLabel") + undo_labels_map[verb.args[1]] = undo_counter; + else if (verb_word == "UndoToLabel") { + undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); + if(iter != undo_labels_map.end()) { + int counter = undo_counter - iter->second; + if( counter > 0 ) { + for(int i = 0; i < counter; ++i) + new CmdLineAction(true, "EditUndo"); + undo_counter -= counter; + } + } + } + else if (verb_word == "XSelectElement") { + ++undo_counter; + values_map["element-id"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } + else if (verb_word == "XFileExportPNG") { + std::string &png_filename = verb.args[1]; + values_map["png_filename"] = png_filename; + if(createDirForFilename( png_filename )) { + new CmdLineXAction(verb_word.c_str(), values_map); + } + } + } + else if(!verb.xverb) { + ++undo_counter; + new CmdLineAction(true, verb.args[0].c_str()); + } + else { + printf("Unhadled verb %s\n", verb.args[0].c_str()); + fflush(stdout); + } + } + + fflush(stdout); + return; } -- cgit v1.2.3 From 2de5e2452a8cd319e195eb77099e5a3a8898c94f Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Tue, 27 Sep 2016 23:25:28 +0700 Subject: ignore additional arguments of x-verbs (bzr r15136.1.6) --- src/main-cmdlinexact.cpp | 98 +++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 34 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 940abfca4..a498f8425 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -437,44 +437,74 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { int undo_counter = 0; verbs_list_t::iterator iter = verbs_list.begin(); - for( ; iter != verbs_list.end(); ++iter ) { + for (;iter != verbs_list.end(); ++iter) { verb_info_t &verb = *iter; std::string &verb_word = verb.args[0]; - if( s_verbose ) + if (s_verbose) printf("handle %s and args count is %d\n", verb_word.c_str(), (int)verb.args.size()); - if( verb.args.size() == 2 ) { - xaction_args_values_map_t values_map; - if (verb_word == "XFileSaveAs" || verb_word == "XFileOpen") { - std::string &filename = verb.args[1]; - values_map["filename"] = filename; - new CmdLineXAction(verb_word.c_str(), values_map); - } - else if (verb_word == "XUndoLabel") - undo_labels_map[verb.args[1]] = undo_counter; - else if (verb_word == "UndoToLabel") { - undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); - if(iter != undo_labels_map.end()) { - int counter = undo_counter - iter->second; - if( counter > 0 ) { - for(int i = 0; i < counter; ++i) - new CmdLineAction(true, "EditUndo"); - undo_counter -= counter; - } - } - } - else if (verb_word == "XSelectElement") { - ++undo_counter; - values_map["element-id"] = verb.args[1]; - new CmdLineXAction(verb_word.c_str(), values_map); - } - else if (verb_word == "XFileExportPNG") { - std::string &png_filename = verb.args[1]; - values_map["png_filename"] = png_filename; - if(createDirForFilename( png_filename )) { - new CmdLineXAction(verb_word.c_str(), values_map); - } - } + if (verb_word == "XFileOpen") { + if( verb.args.size() < 2 ) + { + printf("bad arguments for XFileOpen\n"); + continue; + } + + xaction_args_values_map_t values_map; + values_map["filename"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } else if (verb_word == "XFileSaveAs") + { + if (verb.args.size() < 2) { + printf("bad arguments for XFileSaveAs\n"); + continue; + } + + xaction_args_values_map_t values_map; + values_map["filename"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } else if (verb_word == "XUndoLabel") { + if (verb.args.size() < 2) { + printf("bad arguments for XUndoLabel\n"); + continue; + } + undo_labels_map[verb.args[1]] = undo_counter; + } else if (verb_word == "UndoToLabel") { + if (verb.args.size() < 2) { + printf("bad arguments for UndoToLabel\n"); + continue; + } + + undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); + if(iter != undo_labels_map.end()) { + int counter = undo_counter - iter->second; + if( counter > 0 ) { + for(int i = 0; i < counter; ++i) + new CmdLineAction(true, "EditUndo"); + undo_counter -= counter; + } + } + } else if (verb_word == "XSelectElement") { + if (verb.args.size() < 2) { + printf("bad arguments for XSelectElement\n"); + continue; + } + ++undo_counter; + + xaction_args_values_map_t values_map; + values_map["element-id"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } else if (verb_word == "XFileExportPNG") { + if (verb.args.size() < 2) { + printf("bad arguments for XFileExportPNG\n"); + continue; + } + + xaction_args_values_map_t values_map; + std::string &png_filename = verb.args[1]; + values_map["png_filename"] = png_filename; + if(createDirForFilename( png_filename )) + new CmdLineXAction(verb_word.c_str(), values_map); } else if(!verb.xverb) { ++undo_counter; -- cgit v1.2.3 From d9c220b750654b229096a9f62f24480e100c8aa8 Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Thu, 29 Sep 2016 21:47:23 +0700 Subject: allow optional args for xverbs (bzr r15136.1.7) --- src/main-cmdlinexact.cpp | 185 ++++++++++++++++++++++++----------------------- 1 file changed, 96 insertions(+), 89 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index a498f8425..ddf8a73eb 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -68,6 +68,7 @@ #include #include #include +#include #define DPI_BASE Inkscape::Util::Quantity::convert(1, "in", "px") @@ -93,6 +94,35 @@ bool createDirForFilename( const std::string &filename ) return true; } +std::vector vectorFromString(const std::string &csv) +{ + std::vector result; + + std::string delimiters = ","; + + // Skip delimiters at beginning. + std::string::size_type lastPos = csv.find_first_not_of(delimiters, 0); + + // Find first non-delimiter. + std::string::size_type pos = csv.find_first_of(delimiters, lastPos); + + while (std::string::npos != pos || std::string::npos != lastPos) { + // Found a token, add it to the vector. + std::string token = csv.substr(lastPos, pos - lastPos); + token.erase(0, token.find_first_not_of(' ')); //prefixing spaces + token.erase(token.find_last_not_of(' ')+1); //surfixing spaces + result.push_back(token); + + // Skip delimiters. + lastPos = csv.find_first_not_of(delimiters, pos); + + // Find next non-delimiter. + pos = csv.find_first_of(delimiters, lastPos); + } + + return result; +} + void xFileOpen( const Glib::ustring &uri ) { if (s_verbose) { @@ -279,25 +309,6 @@ struct verb_info_t typedef std::list verbs_list_t; -static void tokenize(const std::string& str, std::vector& tokens, const std::string& delimiters = ",") -{ - // Skip delimiters at beginning. - std::string::size_type lastPos = str.find_first_not_of(delimiters, 0); - - // Find first non-delimiter. - std::string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (std::string::npos != pos || std::string::npos != lastPos) { - // Found a token, add it to the vector. - tokens.push_back(str.substr(lastPos, pos - lastPos)); - - // Skip delimiters. - lastPos = str.find_first_not_of(delimiters, pos); - - // Find next non-delimiter. - pos = str.find_first_of(delimiters, lastPos); - } -} void CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { @@ -397,19 +408,15 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { if(key == "xverb-id") { verb_info_t verb; verb.xverb = true; - - std::string values = (char *)token.data.scalar.value; - tokenize(values, verb.args); - for( size_t i = 0; i < verb.args.size(); ++i ) { - verb.args[i].erase(0, verb.args[i].find_first_not_of(' ')); //prefixing spaces - verb.args[i].erase(verb.args[i].find_last_not_of(' ')+1); //surfixing spaces - } + verb.args = vectorFromString((char *)token.data.scalar.value); + if ((verb.args.size() > 1) && Verb::getbyid((char *)token.data.scalar.value)) + verb.xverb = false; verbs_list.push_back(verb); } else if(key == "verb-id") { verb_info_t verb; verb.xverb = false; - verb.args.push_back((char *)token.data.scalar.value); + verb.args = vectorFromString((char *)token.data.scalar.value); verbs_list.push_back(verb); } else { @@ -443,68 +450,68 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { if (s_verbose) printf("handle %s and args count is %d\n", verb_word.c_str(), (int)verb.args.size()); - if (verb_word == "XFileOpen") { - if( verb.args.size() < 2 ) - { - printf("bad arguments for XFileOpen\n"); - continue; - } - - xaction_args_values_map_t values_map; - values_map["filename"] = verb.args[1]; - new CmdLineXAction(verb_word.c_str(), values_map); - } else if (verb_word == "XFileSaveAs") - { - if (verb.args.size() < 2) { - printf("bad arguments for XFileSaveAs\n"); - continue; - } - - xaction_args_values_map_t values_map; - values_map["filename"] = verb.args[1]; - new CmdLineXAction(verb_word.c_str(), values_map); - } else if (verb_word == "XUndoLabel") { - if (verb.args.size() < 2) { - printf("bad arguments for XUndoLabel\n"); - continue; - } - undo_labels_map[verb.args[1]] = undo_counter; - } else if (verb_word == "UndoToLabel") { - if (verb.args.size() < 2) { - printf("bad arguments for UndoToLabel\n"); - continue; - } - - undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); - if(iter != undo_labels_map.end()) { - int counter = undo_counter - iter->second; - if( counter > 0 ) { - for(int i = 0; i < counter; ++i) - new CmdLineAction(true, "EditUndo"); - undo_counter -= counter; - } - } - } else if (verb_word == "XSelectElement") { - if (verb.args.size() < 2) { - printf("bad arguments for XSelectElement\n"); - continue; - } - ++undo_counter; - - xaction_args_values_map_t values_map; - values_map["element-id"] = verb.args[1]; - new CmdLineXAction(verb_word.c_str(), values_map); - } else if (verb_word == "XFileExportPNG") { - if (verb.args.size() < 2) { - printf("bad arguments for XFileExportPNG\n"); - continue; - } - - xaction_args_values_map_t values_map; - std::string &png_filename = verb.args[1]; - values_map["png_filename"] = png_filename; - if(createDirForFilename( png_filename )) - new CmdLineXAction(verb_word.c_str(), values_map); + if (verb_word == "XFileOpen") { + if( verb.args.size() < 2 ) + { + printf("bad arguments for XFileOpen\n"); + continue; + } + + xaction_args_values_map_t values_map; + values_map["filename"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } else if (verb_word == "XFileSaveAs") + { + if (verb.args.size() < 2) { + printf("bad arguments for XFileSaveAs\n"); + continue; + } + + xaction_args_values_map_t values_map; + values_map["filename"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } else if (verb_word == "XUndoLabel") { + if (verb.args.size() < 2) { + printf("bad arguments for XUndoLabel\n"); + continue; + } + undo_labels_map[verb.args[1]] = undo_counter; + } else if (verb_word == "UndoToLabel") { + if (verb.args.size() < 2) { + printf("bad arguments for UndoToLabel\n"); + continue; + } + + undo_labels_map_t::iterator iter = undo_labels_map.find(verb.args[1]); + if(iter != undo_labels_map.end()) { + int counter = undo_counter - iter->second; + if( counter > 0 ) { + for(int i = 0; i < counter; ++i) + new CmdLineAction(true, "EditUndo"); + undo_counter -= counter; + } + } + } else if (verb_word == "XSelectElement") { + if (verb.args.size() < 2) { + printf("bad arguments for XSelectElement\n"); + continue; + } + ++undo_counter; + + xaction_args_values_map_t values_map; + values_map["element-id"] = verb.args[1]; + new CmdLineXAction(verb_word.c_str(), values_map); + } else if (verb_word == "XFileExportPNG") { + if (verb.args.size() < 2) { + printf("bad arguments for XFileExportPNG\n"); + continue; + } + + xaction_args_values_map_t values_map; + std::string &png_filename = verb.args[1]; + values_map["png_filename"] = png_filename; + if(createDirForFilename( png_filename )) + new CmdLineXAction(verb_word.c_str(), values_map); } else if(!verb.xverb) { ++undo_counter; -- cgit v1.2.3 From 06f90c27b6e17397b03f20fe92fd0fb31d55332c Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Thu, 29 Sep 2016 23:08:29 +0700 Subject: allow to use verbs as xverbs (bzr r15136.1.8) --- src/main-cmdlinexact.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index ddf8a73eb..a3c9436bd 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -517,8 +517,13 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { ++undo_counter; new CmdLineAction(true, verb.args[0].c_str()); } + else if( Verb::getbyid(verb.args[0].c_str()) != NULL ) + { + ++undo_counter; + new CmdLineAction(true, verb.args[0].c_str()); + } else { - printf("Unhadled verb %s\n", verb.args[0].c_str()); + printf("Unhadled xverb %s\n", verb.args[0].c_str()); fflush(stdout); } } -- cgit v1.2.3 From 81364db0bc98b01319dc5ecb835a4e37b906bad2 Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Sat, 1 Oct 2016 13:05:10 +0700 Subject: disable xverb feature if WITH_YAML defined (bzr r15136.1.9) --- src/main-cmdlinexact.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index a3c9436bd..3dc83bc8f 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -52,7 +52,10 @@ #include #include "main-cmdlinexact.h" +#ifdef WITH_YAML #include "yaml.h" +#endif // WITH_YAML + #include "extension/system.h" #include "file.h" #include @@ -312,6 +315,9 @@ typedef std::list verbs_list_t; void CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { +#ifndef WITH_YAML + return; +#else // WITH_YAML FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { printf("Failed to open file!\n"); @@ -409,7 +415,7 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { verb_info_t verb; verb.xverb = true; verb.args = vectorFromString((char *)token.data.scalar.value); - if ((verb.args.size() > 1) && Verb::getbyid((char *)token.data.scalar.value)) + if ((verb.args.size() > 1) && Verb::getbyid((char *)token.data.scalar.value, false)) verb.xverb = false; verbs_list.push_back(verb); } @@ -529,7 +535,7 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { } fflush(stdout); - return; +#endif // WITH_YAML } -- cgit v1.2.3 From ea38e86ebfbcc53d86433fddbffffbfb5403c9fc Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Sat, 1 Oct 2016 16:01:18 +0700 Subject: fix indent (bzr r15136.1.10) --- src/main-cmdlinexact.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 3dc83bc8f..f29d22377 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -316,8 +316,8 @@ typedef std::list verbs_list_t; void CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { #ifndef WITH_YAML - return; -#else // WITH_YAML + return; +#else // WITH_YAML FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { printf("Failed to open file!\n"); @@ -527,7 +527,7 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { { ++undo_counter; new CmdLineAction(true, verb.args[0].c_str()); - } + } else { printf("Unhadled xverb %s\n", verb.args[0].c_str()); fflush(stdout); -- cgit v1.2.3 From 017616c4fc99883323c2a0d6e44b88f7cd16ce2d Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Sat, 1 Oct 2016 17:08:49 +0700 Subject: fix cmake warning and remove whole xverbs code from compilation without yaml (bzr r15136.1.11) --- src/main-cmdlinexact.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index f29d22377..f5db522ae 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -38,7 +38,7 @@ * # inkscape have a lot of useful verbs * - verb-id: FileQuit */ - +#ifdef WITH_YAML #include #include #include @@ -52,9 +52,8 @@ #include #include "main-cmdlinexact.h" -#ifdef WITH_YAML + #include "yaml.h" -#endif // WITH_YAML #include "extension/system.h" #include "file.h" @@ -315,9 +314,6 @@ typedef std::list verbs_list_t; void CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { -#ifndef WITH_YAML - return; -#else // WITH_YAML FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { printf("Failed to open file!\n"); @@ -535,12 +531,13 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { } fflush(stdout); -#endif // WITH_YAML } } // Inkscape +#endif // WITH_YAML + /* Local Variables: mode:c++ -- cgit v1.2.3 From 745dd3ffab16bdadc91beb6d3b0da81594f539fa Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Sun, 2 Oct 2016 11:02:44 +0700 Subject: split big function (bzr r15136.1.12) --- src/main-cmdlinexact.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index f5db522ae..2f7afe9fa 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -311,21 +311,23 @@ struct verb_info_t typedef std::list verbs_list_t; +static verbs_list_t +parseVerbsYAMLFile(gchar const *yaml_filename) +{ + verbs_list_t verbs_list; -void -CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { printf("Failed to open file!\n"); fflush(stdout); - return; + return verbs_list; } yaml_parser_t parser; if(!yaml_parser_initialize(&parser)) { printf("Failed to initialize parser!\n"); fflush(stdout); - return; + return verbs_list; } /* Set input file */ @@ -333,12 +335,10 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { parser_state_t state = HANDLING_ROOT; - bool handling_key = false; bool handling_value = false; std::string key; - verbs_list_t verbs_list; // parse yaml_token_t token; @@ -441,6 +441,14 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { yaml_parser_delete(&parser); fclose(fh); + return verbs_list; +} + +void +CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) +{ + verbs_list_t verbs_list = parseVerbsYAMLFile(yaml_filename); + typedef std::map undo_labels_map_t; undo_labels_map_t undo_labels_map; int undo_counter = 0; -- cgit v1.2.3 From 62fc42f0bceab46b06f0d535a235d8bfe4c851a2 Mon Sep 17 00:00:00 2001 From: Dmitry Zhulanov Date: Mon, 3 Oct 2016 21:38:19 +0700 Subject: butify idents (bzr r15136.1.13) --- src/main-cmdlinexact.cpp | 108 +++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 45 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 2f7afe9fa..99f0c699b 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -8,11 +8,11 @@ * * Format of xverbs.yaml * using: $ inkscape -B xverbs.yaml - * + * * verbose: yes # only "verbose: yes" enable logging * run: * # open document to process - * - xverb-id: XFileOpen, gfx_sources/loading_screen/sandclock_atlas.svg + * - xverb-id: XFileOpen, gfx_sources/loading_screen/sandclock_atlas.svg * - xverb-id: XUndoLabel, fresh_document # set label for UndoToLabel xverb works * # note: if something wrong with undo labels use verb EditUndo instead of XUndoLabel and UndoToLabel at all * @@ -31,7 +31,7 @@ * - xverb-id: XFileExportPNG, output/thegame/linux/data/gfx_preview/loading_screen/top_sand.png * * # return to the fresh_state of document - * - xverb-id: UndoToLabel, fresh_document + * - xverb-id: UndoToLabel, fresh_document * * # do any other handling * @@ -82,10 +82,10 @@ bool createDirForFilename( const std::string &filename ) { size_t found = filename.find_last_of("/\\"); std::string output_directory = filename.substr(0,found); - + if( output_directory == filename ) return true; - + if (g_mkdir_with_parents(output_directory.c_str(), 0755)) { printf("Can't create directory %s\n", output_directory.c_str()); fflush(stdout); @@ -138,7 +138,7 @@ void xFileOpen( const Glib::ustring &uri ) desktop->setWaitingCursor(); Inkscape::DocumentUndo::clearRedo(old_document); } - + SPDocument *doc = NULL; Inkscape::Extension::Extension *key = NULL; try { @@ -152,8 +152,8 @@ void xFileOpen( const Glib::ustring &uri ) // Set viewBox if it doesn't exist if (!doc->getRoot()->viewBox_set - && (doc->getRoot()->width.unit != SVGLength::PERCENT) - && (doc->getRoot()->height.unit != SVGLength::PERCENT)) { + && (doc->getRoot()->width.unit != SVGLength::PERCENT) + && (doc->getRoot()->height.unit != SVGLength::PERCENT)) { doc->setViewBox(Geom::Rect::from_xywh(0, 0, doc->getWidth().value(doc->getDisplayUnit()), doc->getHeight().value(doc->getDisplayUnit()))); } @@ -193,7 +193,7 @@ void xFileSaveAs( Inkscape::ActionContext const & context, const Glib::ustring & if( createDirForFilename( uri )) { Inkscape::Extension::save( - Inkscape::Extension::db.get("org.inkscape.output.svg.inkscape"), + Inkscape::Extension::db.get("org.inkscape.output.svg.inkscape"), doc, uri.c_str(), false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS); if (s_verbose) { printf("save done: %s\n", uri.c_str() ); @@ -220,26 +220,28 @@ void xFileExportPNG( Inkscape::ActionContext const & context, const Glib::ustrin Inkscape::Preferences *prefs = Inkscape::Preferences::get(); dpi = prefs->getDouble("/dialogs/export/defaultxdpi/value", DPI_BASE); - + gdouble width = doc->getWidth().value(doc->getDisplayUnit()); gdouble height = doc->getHeight().value(doc->getDisplayUnit()); - + gdouble bmwidth = (width) * dpi / DPI_BASE; gdouble bmheight = (height) * dpi / DPI_BASE; - + int png_width = (int)(0.5 + bmwidth); int png_height = (int)(0.5 + bmheight); - + SPNamedView *nv = desktop->getNamedView(); ExportResult status = sp_export_png_file(doc, uri.c_str(), - Geom::Rect(Geom::Point(0,0), Geom::Point(width, height)), png_width, png_height, dpi, dpi, - nv->pagecolor, 0, 0, TRUE); + Geom::Rect(Geom::Point(0,0), Geom::Point(width, height)), png_width, png_height, dpi, dpi, + nv->pagecolor, 0, 0, TRUE); } void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustring &uri ) { - if (context.getDocument() == NULL || context.getSelection() == NULL) { return; } + if (context.getDocument() == NULL || context.getSelection() == NULL) { + return; + } if (s_verbose) { printf("select element: %s\n", uri.c_str()); @@ -281,7 +283,7 @@ CmdLineXAction::isExtended() { void CmdLineXAction::doItX (ActionContext const & context) { - (void)(context); + (void)(context); if( arg == "XFileSaveAs") xFileSaveAs( context, _values_map["filename"] ); @@ -299,9 +301,10 @@ CmdLineXAction::doItX (ActionContext const & context) { return; } -enum parser_state_t{ HANDLING_ROOT, - HANDLING_VERBOSE, // options - HANDLING_RUN, HANDLING_RUN_LIST, HANDLING_RUN_LIST_ENTRY }; // run entries +enum parser_state_t { HANDLING_ROOT, + HANDLING_VERBOSE, // options + HANDLING_RUN, HANDLING_RUN_LIST, HANDLING_RUN_LIST_ENTRY + }; // run entries struct verb_info_t { @@ -314,7 +317,7 @@ typedef std::list verbs_list_t; static verbs_list_t parseVerbsYAMLFile(gchar const *yaml_filename) { - verbs_list_t verbs_list; + verbs_list_t verbs_list; FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { @@ -346,24 +349,39 @@ parseVerbsYAMLFile(gchar const *yaml_filename) yaml_parser_scan(&parser, &token); switch(token.type) { - // avoid "warning: enumeration value", "-Wswitch" - case YAML_NO_TOKEN: break; - case YAML_STREAM_START_TOKEN: break; - case YAML_STREAM_END_TOKEN: break; - case YAML_VERSION_DIRECTIVE_TOKEN: break; - case YAML_TAG_DIRECTIVE_TOKEN: break; - case YAML_DOCUMENT_START_TOKEN: break; - case YAML_DOCUMENT_END_TOKEN: break; - case YAML_FLOW_SEQUENCE_START_TOKEN: break; - case YAML_FLOW_SEQUENCE_END_TOKEN: break; - case YAML_FLOW_MAPPING_START_TOKEN: break; - case YAML_FLOW_MAPPING_END_TOKEN: break; - case YAML_FLOW_ENTRY_TOKEN: break; - case YAML_ALIAS_TOKEN: break; - case YAML_ANCHOR_TOKEN: break; - case YAML_TAG_TOKEN: break; - - /* Token types (read before actual token) */ + // avoid "warning: enumeration value", "-Wswitch" + case YAML_NO_TOKEN: + break; + case YAML_STREAM_START_TOKEN: + break; + case YAML_STREAM_END_TOKEN: + break; + case YAML_VERSION_DIRECTIVE_TOKEN: + break; + case YAML_TAG_DIRECTIVE_TOKEN: + break; + case YAML_DOCUMENT_START_TOKEN: + break; + case YAML_DOCUMENT_END_TOKEN: + break; + case YAML_FLOW_SEQUENCE_START_TOKEN: + break; + case YAML_FLOW_SEQUENCE_END_TOKEN: + break; + case YAML_FLOW_MAPPING_START_TOKEN: + break; + case YAML_FLOW_MAPPING_END_TOKEN: + break; + case YAML_FLOW_ENTRY_TOKEN: + break; + case YAML_ALIAS_TOKEN: + break; + case YAML_ANCHOR_TOKEN: + break; + case YAML_TAG_TOKEN: + break; + + /* Token types (read before actual token) */ case YAML_KEY_TOKEN: handling_key = true; handling_value = false; @@ -373,12 +391,12 @@ parseVerbsYAMLFile(gchar const *yaml_filename) handling_value = true; break; - /* Block delimeters */ + /* Block delimeters */ case YAML_BLOCK_SEQUENCE_START_TOKEN: if( state == HANDLING_ROOT ) { if( key == "run" ) state = HANDLING_RUN; - } + } break; case YAML_BLOCK_ENTRY_TOKEN: if( state == HANDLING_RUN ) @@ -399,7 +417,7 @@ parseVerbsYAMLFile(gchar const *yaml_filename) state = HANDLING_ROOT; break; - /* Data */ + /* Data */ case YAML_BLOCK_MAPPING_START_TOKEN: break; case YAML_SCALAR_TOKEN: @@ -441,7 +459,7 @@ parseVerbsYAMLFile(gchar const *yaml_filename) yaml_parser_delete(&parser); fclose(fh); - return verbs_list; + return verbs_list; } void @@ -454,7 +472,7 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) int undo_counter = 0; verbs_list_t::iterator iter = verbs_list.begin(); - for (;iter != verbs_list.end(); ++iter) { + for (; iter != verbs_list.end(); ++iter) { verb_info_t &verb = *iter; std::string &verb_word = verb.args[0]; if (s_verbose) @@ -516,7 +534,7 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) printf("bad arguments for XFileExportPNG\n"); continue; } - + xaction_args_values_map_t values_map; std::string &png_filename = verb.args[1]; values_map["png_filename"] = png_filename; -- cgit v1.2.3 From 5bfd9a2fc8244c78125dc417975b6feff6b884f1 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Thu, 18 May 2017 03:57:46 +0700 Subject: fix naming (bzr r15697.1.1) --- src/main-cmdlinexact.cpp | 37 ++++--------------------------------- 1 file changed, 4 insertions(+), 33 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 99f0c699b..fd9520104 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -6,38 +6,9 @@ * * Released under GNU GPL v2, read the file 'COPYING' for more information * - * Format of xverbs.yaml - * using: $ inkscape -B xverbs.yaml - * - * verbose: yes # only "verbose: yes" enable logging - * run: - * # open document to process - * - xverb-id: XFileOpen, gfx_sources/loading_screen/sandclock_atlas.svg - * - xverb-id: XUndoLabel, fresh_document # set label for UndoToLabel xverb works - * # note: if something wrong with undo labels use verb EditUndo instead of XUndoLabel and UndoToLabel at all - * - * # select element to handle - * - xverb-id: XSelectElement, top_sand - * - * # verbs - * - verb-id: EditInvertInAllLayers - * - verb-id: EditDelete - * - verb-id: FitCanvasToDrawing - * - * # save element to separated svg document - * - xverb-id: XFileSaveAs, output/thegame/linux/data/gfx/loading_screen/top_sand.svg - * - * # also save png preview - * - xverb-id: XFileExportPNG, output/thegame/linux/data/gfx_preview/loading_screen/top_sand.png - * - * # return to the fresh_state of document - * - xverb-id: UndoToLabel, fresh_document - * - * # do any other handling - * - * # inkscape have a lot of useful verbs - * - verb-id: FileQuit + * more details: http://wiki.inkscape.org/wiki/index.php/Using_xverbs */ + #ifdef WITH_YAML #include #include @@ -504,9 +475,9 @@ CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) continue; } undo_labels_map[verb.args[1]] = undo_counter; - } else if (verb_word == "UndoToLabel") { + } else if (verb_word == "XUndoToLabel") { if (verb.args.size() < 2) { - printf("bad arguments for UndoToLabel\n"); + printf("bad arguments for XUndoToLabel\n"); continue; } -- cgit v1.2.3 From d865c3a94b4761f476877ad11239c46830d68cec Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Thu, 18 May 2017 04:00:31 +0700 Subject: fix crash (bzr r15697.1.2) --- src/main-cmdlinexact.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index fd9520104..fc9e84dc4 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -108,6 +108,7 @@ void xFileOpen( const Glib::ustring &uri ) SPDocument *old_document = desktop->getDocument(); desktop->setWaitingCursor(); Inkscape::DocumentUndo::clearRedo(old_document); + Inkscape::DocumentUndo::clearUndo(old_document); } SPDocument *doc = NULL; -- cgit v1.2.3 From 814731e7a894b2ce17695c60a0b15b2a3202511a Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Thu, 1 Jun 2017 09:28:12 +0700 Subject: improve open .yaml error message and exit with error Fixed bugs: - https://launchpad.net/bugs/1692707 (bzr r15700.1.1) --- src/main-cmdlinexact.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index fc9e84dc4..2806e5b5f 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -293,9 +293,11 @@ parseVerbsYAMLFile(gchar const *yaml_filename) FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { - printf("Failed to open file!\n"); + printf("Failed to open file %s\n", yaml_filename); fflush(stdout); - return verbs_list; + + // exit with error + exit(1); } yaml_parser_t parser; -- cgit v1.2.3 From 6b20733af3ca815d12168948ff6d7f3737d17541 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Sat, 3 Jun 2017 13:41:49 +0700 Subject: fix crash in XFileOpen Fixed bugs: - https://launchpad.net/bugs/1692699 (bzr r15700.1.2) --- src/main-cmdlinexact.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 2806e5b5f..676bf7b44 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -120,6 +120,7 @@ void xFileOpen( const Glib::ustring &uri ) std::string exeption_mgs = e.what(); printf("Error: open %s:%s\n",uri.c_str(), exeption_mgs.c_str() ); fflush(stdout); + return; } // Set viewBox if it doesn't exist -- cgit v1.2.3 From 454c66cd88665d3e9e7374e134e2215fbc8c2d38 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Sat, 3 Jun 2017 14:05:27 +0700 Subject: check if file exist Fixed bugs: - https://launchpad.net/bugs/1692699 (bzr r15700.1.3) --- src/main-cmdlinexact.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 676bf7b44..000ad6fec 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -41,7 +41,7 @@ #include #include #include -#include +#include #define DPI_BASE Inkscape::Util::Quantity::convert(1, "in", "px") @@ -98,6 +98,11 @@ std::vector vectorFromString(const std::string &csv) void xFileOpen( const Glib::ustring &uri ) { + if (!Inkscape::IO::file_test(uri.c_str(), (GFileTest)(G_FILE_TEST_EXISTS))) { + printf("document is not exists: %s\n", uri.c_str()); + return; + } + if (s_verbose) { printf("open %s\n", uri.c_str()); fflush(stdout); @@ -120,7 +125,6 @@ void xFileOpen( const Glib::ustring &uri ) std::string exeption_mgs = e.what(); printf("Error: open %s:%s\n",uri.c_str(), exeption_mgs.c_str() ); fflush(stdout); - return; } // Set viewBox if it doesn't exist -- cgit v1.2.3 From 387d4eccb3a10dbda4a15e152fdea20525d934f7 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Sat, 3 Jun 2017 14:59:36 +0700 Subject: handle svg documents relative to .yaml path Fixed bugs: - https://launchpad.net/bugs/1692701 (bzr r15700.1.4) --- src/main-cmdlinexact.cpp | 57 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 17 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 000ad6fec..9e7231194 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -48,6 +48,7 @@ namespace { bool s_verbose = false; +Glib::ustring s_yaml_filename(""); bool createDirForFilename( const std::string &filename ) { @@ -96,15 +97,33 @@ std::vector vectorFromString(const std::string &csv) return result; } +Glib::ustring getDocumentPathRelatedYaml( const Glib::ustring &uri ) +{ + if (g_path_is_absolute (uri.c_str())) + return uri; + + gchar *yaml_dirname = g_path_get_dirname(s_yaml_filename.c_str()); + gchar *document_path = g_build_filename(yaml_dirname, uri.c_str(), NULL); + + Glib::ustring result(document_path); + + g_free(document_path); + g_free(yaml_dirname); + + return result; +} + void xFileOpen( const Glib::ustring &uri ) { - if (!Inkscape::IO::file_test(uri.c_str(), (GFileTest)(G_FILE_TEST_EXISTS))) { - printf("document is not exists: %s\n", uri.c_str()); + Glib::ustring document_filename = getDocumentPathRelatedYaml(uri); + + if (!Inkscape::IO::file_test(document_filename.c_str(), (GFileTest)(G_FILE_TEST_EXISTS))) { + printf("document is not exists: %s\n", document_filename.c_str()); return; } if (s_verbose) { - printf("open %s\n", uri.c_str()); + printf("open %s\n", document_filename.c_str()); fflush(stdout); } @@ -119,11 +138,11 @@ void xFileOpen( const Glib::ustring &uri ) SPDocument *doc = NULL; Inkscape::Extension::Extension *key = NULL; try { - doc = Inkscape::Extension::open(key, uri.c_str()); + doc = Inkscape::Extension::open(key, document_filename.c_str()); } catch (std::exception &e) { doc = NULL; std::string exeption_mgs = e.what(); - printf("Error: open %s:%s\n",uri.c_str(), exeption_mgs.c_str() ); + printf("Error: open %s:%s\n",document_filename.c_str(), exeption_mgs.c_str() ); fflush(stdout); } @@ -162,31 +181,33 @@ void xFileOpen( const Glib::ustring &uri ) void xFileSaveAs( Inkscape::ActionContext const & context, const Glib::ustring &uri ) { + Glib::ustring document_filename = getDocumentPathRelatedYaml(uri); SPDocument *doc = context.getDocument(); if (s_verbose) { - printf("save as %s\n", uri.c_str()); + printf("save as %s\n", document_filename.c_str()); fflush(stdout); } - if( createDirForFilename( uri )) { + if( createDirForFilename( document_filename )) { Inkscape::Extension::save( Inkscape::Extension::db.get("org.inkscape.output.svg.inkscape"), - doc, uri.c_str(), false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS); + doc, document_filename.c_str(), false, false, true, Inkscape::Extension::FILE_SAVE_METHOD_SAVE_AS); if (s_verbose) { - printf("save done: %s\n", uri.c_str() ); + printf("save done: %s\n", document_filename.c_str() ); fflush(stdout); } } else { - printf("can't create dirs for filename %s\n", uri.c_str() ); + printf("can't create dirs for filename %s\n", document_filename.c_str() ); fflush(stdout); } } void xFileExportPNG( Inkscape::ActionContext const & context, const Glib::ustring &uri ) { + Glib::ustring document_filename = getDocumentPathRelatedYaml(uri); if (s_verbose) { - printf("export png %s\n", uri.c_str()); + printf("export png %s\n", document_filename.c_str()); fflush(stdout); } @@ -209,27 +230,27 @@ void xFileExportPNG( Inkscape::ActionContext const & context, const Glib::ustrin SPNamedView *nv = desktop->getNamedView(); - ExportResult status = sp_export_png_file(doc, uri.c_str(), + ExportResult status = sp_export_png_file(doc, document_filename.c_str(), Geom::Rect(Geom::Point(0,0), Geom::Point(width, height)), png_width, png_height, dpi, dpi, nv->pagecolor, 0, 0, TRUE); } -void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustring &uri ) +void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustring &element_name ) { if (context.getDocument() == NULL || context.getSelection() == NULL) { return; } if (s_verbose) { - printf("select element: %s\n", uri.c_str()); + printf("select element: %s\n", element_name.c_str()); fflush(stdout); } SPDocument * doc = context.getDocument(); - SPObject * obj = doc->getObjectById(uri); + SPObject * obj = doc->getObjectById(element_name); if (obj == NULL) { - printf(_("Unable to find node ID: '%s'\n"), uri.c_str()); + printf(_("Unable to find node ID: '%s'\n"), element_name.c_str()); fflush(stdout); return; } @@ -238,7 +259,7 @@ void xSelectElement( Inkscape::ActionContext const & context, const Glib::ustrin selection->add(obj); if (s_verbose) { - printf("select done %s\n", uri.c_str()); + printf("select done %s\n", element_name.c_str()); fflush(stdout); } } @@ -444,6 +465,8 @@ parseVerbsYAMLFile(gchar const *yaml_filename) void CmdLineXAction::createActionsFromYAML( gchar const *yaml_filename ) { + s_yaml_filename = Glib::ustring(yaml_filename); + verbs_list_t verbs_list = parseVerbsYAMLFile(yaml_filename); typedef std::map undo_labels_map_t; -- cgit v1.2.3 From de3945431b7222ed127ed59cdae8990640b79170 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Sat, 3 Jun 2017 20:40:10 +0700 Subject: abort Inkscape if XFileOpen command fails Fixed bugs: - https://launchpad.net/bugs/1695606 (bzr r15700.1.5) --- src/main-cmdlinexact.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 9e7231194..9827378b2 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -119,7 +119,7 @@ void xFileOpen( const Glib::ustring &uri ) if (!Inkscape::IO::file_test(document_filename.c_str(), (GFileTest)(G_FILE_TEST_EXISTS))) { printf("document is not exists: %s\n", document_filename.c_str()); - return; + exit(1); } if (s_verbose) { -- cgit v1.2.3 From 745324f2e65dec6eeec16585f5d5ca639179ded3 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Sat, 3 Jun 2017 20:50:01 +0700 Subject: minor fixes Fixed bugs: - https://launchpad.net/bugs/1692699 (bzr r15700.1.6) --- src/main-cmdlinexact.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 9827378b2..4da138f0c 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -118,7 +118,7 @@ void xFileOpen( const Glib::ustring &uri ) Glib::ustring document_filename = getDocumentPathRelatedYaml(uri); if (!Inkscape::IO::file_test(document_filename.c_str(), (GFileTest)(G_FILE_TEST_EXISTS))) { - printf("document is not exists: %s\n", document_filename.c_str()); + printf("SVG document does not exist: %s\n", document_filename.c_str()); exit(1); } @@ -319,7 +319,7 @@ parseVerbsYAMLFile(gchar const *yaml_filename) FILE *fh = fopen(yaml_filename, "r"); if(fh == NULL) { - printf("Failed to open file %s\n", yaml_filename); + printf("Failed to read from file %s\n", yaml_filename); fflush(stdout); // exit with error -- cgit v1.2.3 From 16e2b8d276eec85a7fc138e66f438caf6acf0929 Mon Sep 17 00:00:00 2001 From: "dmitry.zhulanov@gmail.com" <> Date: Sun, 4 Jun 2017 21:21:43 +0700 Subject: replace fopen with g_fopen Fixed bugs: - https://launchpad.net/bugs/1695629 (bzr r15700.1.7) --- src/main-cmdlinexact.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main-cmdlinexact.cpp') diff --git a/src/main-cmdlinexact.cpp b/src/main-cmdlinexact.cpp index 4da138f0c..eca402ab3 100644 --- a/src/main-cmdlinexact.cpp +++ b/src/main-cmdlinexact.cpp @@ -29,6 +29,7 @@ #include "extension/system.h" #include "file.h" #include +#include #include "sp-root.h" #include "document-undo.h" #include "util/units.h" @@ -317,7 +318,7 @@ parseVerbsYAMLFile(gchar const *yaml_filename) { verbs_list_t verbs_list; - FILE *fh = fopen(yaml_filename, "r"); + FILE *fh = g_fopen(yaml_filename, "r"); if(fh == NULL) { printf("Failed to read from file %s\n", yaml_filename); fflush(stdout); -- cgit v1.2.3