Added copyright lines for files I've edited this year.
[synfig.git] / synfig-core / trunk / src / tool / main.cpp
index 424d2ea..b4155f4 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007, 2008 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -34,6 +35,7 @@
 #include <list>
 #include <ETL/clock>
 #include <algorithm>
+#include <cstring>
 
 #include <synfig/loadcanvas.h>
 #include <synfig/savecanvas.h>
@@ -56,18 +58,26 @@ using namespace synfig;
 
 /* === M A C R O S ========================================================= */
 
+#ifdef ENABLE_NLS
+#undef _
+#define _(x) gettext(x)
+#else
+#undef _
+#define _(x) (x)
+#endif
+
 enum exit_code
 {
-       SYNFIGTOOL_OK                           =0,
-       SYNFIGTOOL_FILENOTFOUND         =1,
-       SYNFIGTOOL_BORRED                       =2,
-       SYNFIGTOOL_HELP                         =3,
-       SYNFIGTOOL_UNKNOWNARGUMENT      =4,
-       SYNFIGTOOL_UNKNOWNERROR         =5,
-       SYNFIGTOOL_INVALIDTARGET                =6,
-       SYNFIGTOOL_RENDERFAILURE                =7,
-       SYNFIGTOOL_BLANK                                =8,
-       SYNFIGTOOL_BADVERSION           =9,
+       SYNFIGTOOL_OK                           = 0,
+       SYNFIGTOOL_FILENOTFOUND         = 1,
+       SYNFIGTOOL_BORED                        = 2,
+       SYNFIGTOOL_HELP                         = 3,
+       SYNFIGTOOL_UNKNOWNARGUMENT      = 4,
+       SYNFIGTOOL_UNKNOWNERROR         = 5,
+       SYNFIGTOOL_INVALIDTARGET        6,
+       SYNFIGTOOL_RENDERFAILURE        7,
+       SYNFIGTOOL_BLANK                        8,
+       SYNFIGTOOL_BADVERSION           = 9,
        SYNFIGTOOL_MISSINGARGUMENT      =10
 };
 
@@ -122,7 +132,7 @@ public:
        }
 
        virtual bool
-       amount_complete(int current, int total)
+       amount_complete(int /*current*/, int /*total*/)
        {
                return true;
        }
@@ -295,12 +305,14 @@ void display_help(int amount)
                                cerr<<strprintf(" %s %s %s",flag, arg, spaces+strlen(arg)+strlen(flag)+1)+description<<endl;
                        else
                                cerr<<strprintf(" %s %s",flag,spaces+strlen(flag))+description<<endl;
-
                }
        };
-       cerr<<_("syntax: ")<<progname<<" [DEFAULT OPTIONS] ([SIF FILE] [SPECIFIC OPTIONS])..."<<endl;
-       cerr<<endl;
-       if(amount>=1)
+
+       cerr << endl << _("syntax: ") << progname << " [DEFAULT OPTIONS] ([SIF FILE] [SPECIFIC OPTIONS])..." << endl << endl;
+
+       if(amount == 0)
+               Argument("--help",NULL,_("Print out usage and syntax info"));
+       else
        {
                Argument("-t","<output type>",_("Specify output target (Default:unknown)"));
                Argument("-w","<pixel width>",_("Set the image width (Use zero for file default)"));
@@ -314,16 +326,13 @@ void display_help(int amount)
                Argument("-c","<canvas id>",_("Render the canvas with the given id instead of the root."));
                Argument("-o","<output file>",_("Specify output filename"));
                Argument("-T","<# of threads>",_("Enable multithreaded renderer using specified # of threads"));
-
                Argument("-b",NULL,_("Print Benchmarks"));
-
                Argument("--fps","<framerate>",_("Set the frame rate"));
                Argument("--time","<time>",_("Render a single frame at <seconds>"));
                Argument("--begin-time","<time>",_("Set the starting time"));
                Argument("--end-time","<time>",_("Set the ending time"));
                Argument("--dpi","<res>",_("Set the dots-per-inch"));
                Argument("--append","<filename>",_("Append layers in <filename> to composition"));
-
                Argument("--layer-info","<layer>",_("Print out layer's description, parameter info, etc."));
                Argument("--layers",NULL,_("Print out the list of available layers"));
                Argument("--targets",NULL,_("Print out the list of available targets"));
@@ -339,9 +348,7 @@ void display_help(int amount)
                Argument("--signal-test",NULL,_("Test signal implementation"));
 #endif
        }
-       else
 
-       Argument("--help",NULL,_("Print out usage and syntax info"));
        cerr<<endl;
 }
 
@@ -366,11 +373,9 @@ int process_global_flags(arg_list_t &arg_list)
                        return SYNFIGTOOL_HELP;
                }
 
-
                if(*iter == "--help")
                {
                        display_help(1);
-
                        return SYNFIGTOOL_HELP;
                }
 
@@ -446,7 +451,6 @@ int process_global_flags(arg_list_t &arg_list)
                        synfig::Target::Book::iterator iter=synfig::Target::book().begin();
                        for(;iter!=synfig::Target::book().end();iter++)
                                cout<<iter->first<<endl;
-
                        return SYNFIGTOOL_HELP;
                }
 
@@ -457,7 +461,6 @@ int process_global_flags(arg_list_t &arg_list)
                        synfig::LinkableValueNode::Book::iterator iter=synfig::LinkableValueNode::book().begin();
                        for(;iter!=synfig::LinkableValueNode::book().end();iter++)
                                cout<<iter->first<<endl;
-
                        return SYNFIGTOOL_HELP;
                }
 
@@ -468,16 +471,13 @@ int process_global_flags(arg_list_t &arg_list)
                        synfig::Importer::Book::iterator iter=synfig::Importer::book().begin();
                        for(;iter!=synfig::Importer::book().end();iter++)
                                cout<<iter->first<<endl;
-
                        return SYNFIGTOOL_HELP;
                }
 
                if(*iter == "--version")
                {
                        cerr<<PACKAGE<<" "<<VERSION<<endl;
-
                        arg_list.erase(iter);
-
                        return SYNFIGTOOL_HELP;
                }
 
@@ -493,33 +493,26 @@ int process_global_flags(arg_list_t &arg_list)
 **\n\
 **     " << endl << endl;
                        arg_list.erase(iter);
-
                        return SYNFIGTOOL_HELP;
                }
 
                if(*iter == "-v")
                {
                        verbosity++;
-
                        arg_list.erase(iter);
-
                        continue;
                }
 
                if(*iter == "-q")
                {
                        be_quiet=true;
-
                        arg_list.erase(iter);
-
                        continue;
                }
                if(*iter == "-b")
                {
                        print_benchmarks=true;
-
                        arg_list.erase(iter);
-
                        continue;
                }
        }
@@ -527,6 +520,15 @@ int process_global_flags(arg_list_t &arg_list)
        return SYNFIGTOOL_OK;
 }
 
+/* true if the given flag takes an extra parameter */
+bool flag_requires_value(String flag) {
+       return (flag=="-a"      || flag=="-c"   || flag=="-g"   || flag=="-h"   || flag=="-o" ||
+                       flag=="-Q"      || flag=="-s"   || flag=="-t"   || flag=="-T"   || flag=="-w" ||
+                       flag=="--append"        || flag=="--begin-time"         || flag=="--dpi"                || flag=="--dpi-x"      ||
+                       flag=="--dpi-y"         || flag=="--end-frame"          || flag=="--end-time"   || flag=="--fps"        ||
+                       flag=="--frame"         || flag=="--start-frame"        || flag=="--start-time" || flag=="--time"       );
+}
+
 int extract_arg_cluster(arg_list_t &arg_list,arg_list_t &cluster)
 {
        arg_list_t::iterator iter, next;
@@ -539,29 +541,7 @@ int extract_arg_cluster(arg_list_t &arg_list,arg_list_t &cluster)
                        return SYNFIGTOOL_OK;
                }
 
-               if(
-                       *iter=="-t" ||
-                       *iter=="-w" ||
-                       *iter=="-h" ||
-                       *iter=="-a" ||
-                       *iter=="-g" ||
-                       *iter=="-o" ||
-                       *iter=="-s" ||
-                       *iter=="-Q" ||
-                       *iter=="-c" ||
-                       *iter=="--fps" ||
-                       *iter=="--start-time" ||
-                       *iter=="--begin-time" ||
-                       *iter=="--end-time" ||
-                       *iter=="--start-frame" ||
-                       *iter=="--end-frame" ||
-                       *iter=="--time" ||
-                       *iter=="--frame" ||
-                       *iter=="--dpi" ||
-                       *iter=="--dpi-x" ||
-                       *iter=="--dpi-y" ||
-                       *iter=="--append" ||
-                       *iter=="-T" )
+               if (flag_requires_value(*iter))
                {
                        cluster.push_back(*iter);
                        arg_list.erase(iter);
@@ -687,6 +667,8 @@ int extract_RendDesc(arg_list_t &arg_list,RendDesc &desc)
                        //desc.set_gamma(Gamma(atof(iter->c_str())));
                        arg_list.erase(iter);
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
        if(w&&h)
        {
@@ -724,6 +706,8 @@ int extract_quality(arg_list_t &arg_list,int &quality)
                        VERBOSE_OUT(1)<<strprintf(_("Quality set to %d"),quality)<<endl;
                        arg_list.erase(iter);
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
 
        return SYNFIGTOOL_OK;
@@ -742,6 +726,8 @@ int extract_threads(arg_list_t &arg_list,int &threads)
                        VERBOSE_OUT(1)<<strprintf(_("Threads set to %d"),threads)<<endl;
                        arg_list.erase(iter);
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
 
        return SYNFIGTOOL_OK;
@@ -761,6 +747,8 @@ int extract_target(arg_list_t &arg_list,string &type)
                        type=*iter;
                        arg_list.erase(iter);
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
 
        return SYNFIGTOOL_OK;
@@ -780,6 +768,8 @@ int extract_append(arg_list_t &arg_list,string &filename)
                        filename=*iter;
                        arg_list.erase(iter);
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
 
        return SYNFIGTOOL_OK;
@@ -801,6 +791,8 @@ int extract_outfile(arg_list_t &arg_list,string &outfile)
                        arg_list.erase(iter);
                        ret=SYNFIGTOOL_OK;
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
 
        return ret;
@@ -820,6 +812,8 @@ int extract_canvasid(arg_list_t &arg_list,string &canvasid)
                        canvasid=*iter;
                        arg_list.erase(iter);
                }
+               else if (flag_requires_value(*iter))
+                       iter=next++;
        }
 
        return SYNFIGTOOL_OK;
@@ -835,6 +829,13 @@ int main(int argc, char *argv[])
        arg_list_t arg_list;
        job_list_t job_list;
 
+       setlocale(LC_ALL, "");
+
+#ifdef ENABLE_NLS
+       bindtextdomain("synfig", LOCALEDIR);
+       textdomain("synfig");
+#endif
+
        progname=argv[0];
        Progress p(argv[0]);
 
@@ -880,9 +881,8 @@ int main(int argc, char *argv[])
                          return ret;
 
                        // Open the composition
-                       {
-                               job_list.front().root=open_canvas(job_list.front().filename);
-                       }
+                       job_list.front().root=open_canvas(job_list.front().filename);
+
                        if(!job_list.front().root)
                        {
                                cerr<<_("Unable to open ")<<job_list.front().filename<<"."<<endl;
@@ -948,7 +948,7 @@ int main(int argc, char *argv[])
                                        }
                                        VERBOSE_OUT(2)<<_("Appended contents of ")<<composite_file<<endl;
                                }
-                       }while(false);
+                       } while(false);
 
                        VERBOSE_OUT(4)<<_("Attempting to determine target/outfile...")<<endl;
 
@@ -957,11 +957,28 @@ int main(int argc, char *argv[])
                        if(target_name.empty() && !job_list.front().outfilename.empty())
                        {
                                VERBOSE_OUT(3)<<_("Target name undefined, attempting to figure it out")<<endl;
-                               string ext=job_list.front().outfilename.substr(job_list.front().outfilename.rfind('.')+1);
+                               string ext = filename_extension(job_list.front().outfilename);
+                               if (ext.length()) ext = ext.substr(1);
                                if(Target::ext_book().count(ext))
+                               {
                                        target_name=Target::ext_book()[ext];
+                                       info("target name not specified - using %s", target_name.c_str());
+                               }
                                else
-                                       target_name=ext;
+                               {
+                                       string lower_ext(ext);
+
+                                       for(unsigned int i=0;i<ext.length();i++)
+                                               lower_ext[i] = tolower(ext[i]);
+
+                                       if(Target::ext_book().count(lower_ext))
+                                       {
+                                               target_name=Target::ext_book()[lower_ext];
+                                               info("target name not specified - using %s", target_name.c_str());
+                                       }
+                                       else
+                                               target_name=ext;
+                               }
                        }
 
                        // If the target type is STILL not yet defined, then
@@ -977,9 +994,7 @@ int main(int argc, char *argv[])
                        // given input filename. (ie: change the extension)
                        if(job_list.front().outfilename.empty())
                        {
-                               job_list.front().outfilename=job_list.front().filename;
-                               job_list.front().outfilename.erase(find(job_list.front().outfilename.begin(),job_list.front().outfilename.end(),'.'),job_list.front().outfilename.end());
-                               job_list.front().outfilename+='.';
+                               job_list.front().outfilename = filename_sans_extension(job_list.front().filename) + '.';
                                if(Target::book().count(target_name))
                                        job_list.front().outfilename+=Target::book()[target_name].second;
                                else
@@ -1033,8 +1048,6 @@ int main(int argc, char *argv[])
                                job_list.pop_front();
                                continue;
                        }
-                       //string bleh;
-                       //getline(cin,bleh);
                }
        }
 
@@ -1050,7 +1063,7 @@ int main(int argc, char *argv[])
        if(!job_list.size())
        {
                cerr<<_("Nothing to do!")<<endl;
-               return SYNFIGTOOL_BORRED;
+               return SYNFIGTOOL_BORED;
        }
 
        for(;job_list.size();job_list.pop_front())