Author: wyoung
Date: Sat Mar 21 04:28:55 2009
New Revision: 2488

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2488&view=rev
Log:
- Created ssqlsxlat tool.  Does nothing more than just parse the planned
  command line scheme.
- Created CommandLineBase subclass for above, including modifying said
  base class to support a few things the new subclass needs.
- Minor mods to mysqlpp::examples::CommandLine to track previous

Added:
    trunk/ssx/
    trunk/ssx/main.cpp
Modified:
    trunk/   (props changed)
    trunk/Wishlist
    trunk/lib/cmdline.cpp
    trunk/lib/cmdline.h
    trunk/mysql++.bkl

Propchange: trunk/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sat Mar 21 04:28:55 2009
@@ -44,6 +44,7 @@
 resetdb
 simple[0-9]
 ssqls[0-9]
+ssqlsxlat
 store_if
 test_[a-z]*
 tquery[0-9]

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2488&r1=2487&r2=2488&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Sat Mar 21 04:28:55 2009
@@ -19,24 +19,6 @@
 v3.1 Tentative Plan
 -------------------
     o SSQLS v2 (http://lists.mysql.com/plusplus/6929):
-
-      - Command line parser, implementing this usage:
-
-        ssqlsxlat [-i input.ssqls] [ -1 input-ssqlsv1.cpp ]
-                [ -u user ] [ -p password ] [ -s server ] [ -t table ]
-                [ -o parsedump.ssqls]
-
-        -i: parse SSQLSv2 DSL, generating C++ output at minimum
-        -1: find SSQLSv1 declarations in C++ code, and try to interpret
-            as equivalent SSQLSv2
-        -u, -p, -s and -t: log into server, get schema details for a
-            table, and generate output as if parsed from SSQLSv2 DSL
-        -o: write SSQLSv2 parse info out to .ssqls file
-
-        The latter should work with -i, -1, or -t inputs, implying that
-        all of these construct the same data structure, which -o walks,
-        much as regular C++ output code does, differing only in the
-        output format.
 
       - SSQLSv2 DSL parser:
 

Modified: trunk/lib/cmdline.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cmdline.cpp?rev=2488&r1=2487&r2=2488&view=diff
==============================================================================
--- trunk/lib/cmdline.cpp (original)
+++ trunk/lib/cmdline.cpp Sat Mar 21 04:28:55 2009
@@ -123,19 +123,21 @@
 namespace mysqlpp {
 
 //// CommandLineBase::finish_parse /////////////////////////////////////
+// Called by subclass when it's finished parsing the command line.  It
+// does nothing if we're not still in a "successful" state.
 
 void
 CommandLineBase::finish_parse()
 {
-       const int nextras = argc_ - option_index();
-       if (nextras > 0) {
-               extra_args_.resize(nextras);
-               for (int i = 0; i < nextras; ++i) {
-                       extra_args_[i] = argv_[option_index() + i];
-               }
-       }
-
-       successful_ = true;
+       if (successful_) {
+               const int nextras = argc_ - option_index();
+               if (nextras > 0) {
+                       extra_args_.resize(nextras);
+                       for (int i = 0; i < nextras; ++i) {
+                               extra_args_[i] = argv_[option_index() + i];
+                       }
+               }
+       }
 }
 
 
@@ -169,6 +171,23 @@
 CommandLineBase::parse_next() const
 {
        return getopt(argc_, argv_, opts_);
+}
+
+
+//// CommandLineBase::parse_error //////////////////////////////////////
+// Called by subclasses when they encounter an error in parsing.  We
+// wrap up several details of handling that error: display the
+// message on stderr, call the subclass's print_usage() method, and
+// marks the object as no longer successful.
+
+void
+CommandLineBase::parse_error(const char* message)
+{
+       if (message) {
+               std::cerr << message << '\n';
+       }
+       print_usage();
+       successful_ = false;
 }
 
 
@@ -189,10 +208,11 @@
 run_mode_(0),
 server_(0),
 user_(user && *user ? user : 0),
-pass_(pass && *pass ? pass : "")
+pass_(pass && *pass ? pass : ""),
+usage_extra_(usage_extra)
 {
        int ch;
-       while ((ch = parse_next()) != EOF) {
+       while (successful() && ((ch = parse_next()) != EOF)) {
                switch (ch) {
                        case 'm': run_mode_ = atoi(option_argument()); break;
                        case 'p': pass_ = option_argument();           break;
@@ -200,7 +220,7 @@
                        case 'u': user_ = option_argument();           break;
                        case 'D': dtest_mode_ = true;                  break;
                        default:
-                               print_usage(usage_extra);
+                               parse_error();
                                return;
                }
        }
@@ -233,4 +253,89 @@
 }
 
 } // end namespace mysqlpp::examples
+
+
+////////////////////////////////////////////////////////////////////////
+// Command line parser for MySQL++'s ssqlsxlat tool.
+
+namespace ssqlsxlat {
+
+//// ssqlsxlat::CommandLine ctor ////////////////////////////////////////
+
+CommandLine::CommandLine(int argc, char* const argv[]) :
+CommandLineBase(argc, argv, "i:o:p:s:t:u:1:"),
+input_(0),
+output_(0),
+pass_(""),
+server_(0),
+user_(0),
+input_source_(input_unknown)
+{
+       // Parse the command line
+       int ch;
+       while (successful() && ((ch = parse_next()) != EOF)) {
+               switch (ch) {
+                       case 'o': output_ = option_argument(); break;
+                       case 'p': pass_ = option_argument();   break;
+                       case 's': server_ = option_argument(); break;
+                       case 'u': user_ = option_argument();   break;
+                       case 'i':
+                       case 't':
+                       case '1':
+                               if (input_) {
+                                       std::cerr << "Warning: overriding 
previous input "
+                                                       "source!  Only last -i, 
-t or -1 is "
+                                                       "effective.\n";
+                               }
+                               input_ = option_argument();
+                               input_source_ =
+                                               (ch == '1' ? input_ssqlsv1 :
+                                                ch == 'i' ? input_ssqlsv2 :
+                                                            input_table);
+                               break;
+
+                       default:
+                               parse_error();
+               }
+       }
+       finish_parse();
+
+       // Figure out whether command line makes sense, and if not, tell
+       // user about it.
+       if (successful()) {
+               if (input_source_ == input_unknown) {
+                       parse_error("No input source given!  Need -i, -t or 
-1.");
+               }
+               else if ((input_source_ != input_ssqlsv2) && !output_) {
+                       parse_error("Need -o if you give -t or -1!");
+               }
+       }
+}
+
+
+//// ssqlsxlat::CommandLine::print_usage ////////////////////////////////
+// Show a generic usage message suitable for ../ssqlsxlat/*.cpp  The
+// parameters specialize the message to a minor degree.
+
+void
+CommandLine::print_usage() const
+{
+       std::cerr << "usage: " << program_name() <<
+               " [ -i input.ssqls ] [ -1 input-ssqlsv1.cpp ]\n"
+                       "\t[ -u user ] [ -p password ] [ -s server ] [ -t table 
]\n"
+                       "\t[ -o parsedump.ssqls ]\n";
+       std::cerr << std::endl;
+       std::cerr <<
+                       "\t-i: parse SSQLSv2 DSL, generating C++ output at 
minimum\n"
+                       "\t-1: find SSQLSv1 declarations in C++ code, and try 
to\n"
+                       "\t    interpret as equivalent SSQLSv2; requires -o\n"
+                       "\t-u, -p, -s and -t: log into server, get schema 
details\n"
+                       "\t    for a table, and generate output as if parsed 
from\n"
+                       "\t    SSQLSv2 DSL; requires -o\n"
+                       "\t-o: write out .ssqls file containing info found by\n"
+                       "\t    processing -i, -t or -1\n";
+       std::cerr << std::endl;
+}
+
+} // end namespace mysqlpp::ssqlsxlat
 } // end namespace mysqlpp

Modified: trunk/lib/cmdline.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/cmdline.h?rev=2488&r1=2487&r2=2488&view=diff
==============================================================================
--- trunk/lib/cmdline.h (original)
+++ trunk/lib/cmdline.h Sat Mar 21 04:28:55 2009
@@ -66,15 +66,15 @@
                argc_(argc),
                argv_(argv),
                opts_(opts),
-               successful_(false)
+               successful_(argc > 0 && argv && opts)
                {
-                       assert(argc > 0 && argv && opts);
+                       assert(successful_);
                }
                virtual ~CommandLineBase() { }
 
-               // Save non-option arguments to extra_args_ list, and mark the
-               // object as "successful".  Subclass ctor should call this after
-               // parse_next() loop gets EOF.
+               // If object is still marked as "successful", save non-option
+               // arguments to extra_args_ list.  Subclass ctor should call
+               // this after the parse_next() loop gets EOF.
                void finish_parse();
 
                // Accessors for getopt() globals, so subclasses can ignore
@@ -82,11 +82,21 @@
                const char* option_argument() const;
                int option_index() const;
 
+               // Prints the passed message, calls subclass's print_usage(),
+               // and marks the object as no longer successful.
+               void parse_error(const char* message = 0);
+
                // Wrapper for getopt()
                int parse_next() const;
 
+               // Show a message explaining the program's proper usage
+               virtual void print_usage() const = 0;
+
                // Get the program's executable name
                const char* program_name() const { return argv_[0]; }
+
+               // Returns true if nothing has gone wrong since calling the 
ctor.
+               bool successful() const { return successful_; }
 
        private:
                //// Internal data
@@ -113,7 +123,8 @@
                                        const char* user = 0, const char* pass 
= 0,
                                        const char* usage_extra = 0);
 
-                       // Show a mesage explaining the program's proper usage
+                       // Show a message explaining the program's proper usage
+                       void print_usage() const { print_usage(usage_extra_); }
                        void print_usage(const char* extra) const;
 
                        // Read-only "get" accessors
@@ -131,8 +142,49 @@
                        const char* server_;
                        const char* user_;
                        const char* pass_;
+                       const char* usage_extra_;
                };
        } // end namespace mysqlpp::examples
+
+
+       // Command line parser for MySQL++'s ssqlsxlat tool
+       namespace ssqlsxlat {
+               class MYSQLPP_EXPORT CommandLine : public CommandLineBase
+               {
+               public:
+                       //// Public types
+                       enum input_source_t {
+                               input_unknown,
+                               input_ssqlsv1,
+                               input_ssqlsv2,
+                               input_table
+                       };
+
+                       //// Public interface
+                       // Ctor
+                       CommandLine(int argc, char* const argv[]);
+
+                       // Show a message explaining the program's proper usage
+                       void print_usage() const;
+
+                       // Read-only "get" accessors
+                       const char* input() const { return input_; }
+                       const char* output() const { return output_; }
+                       const char* pass() const { return pass_; }
+                       const char* server() const { return server_; }
+                       const char* user() const { return user_; }
+                       input_source_t input_source() const { return 
input_source_; }
+
+               private:
+                       //// Internal data: command line parse results
+                       const char* input_;
+                       const char* output_;
+                       const char* pass_;
+                       const char* server_;
+                       const char* user_;
+                       input_source_t input_source_;
+               };
+       } // end namespace mysqlpp::ssqlsxlat
 } // end namespace mysqlpp
 
 #endif // !defined(MYSQLPP_CMDLINE_H)

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=2488&r1=2487&r2=2488&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Sat Mar 21 04:28:55 2009
@@ -195,6 +195,11 @@
     </if>
   </template>
 
+  <!-- Build rules for ssqlsxlat tool -->
+  <exe id="ssqlsxlat" template="programs">
+    <sources>ssx/main.cpp</sources>
+  </exe>
+
   <!-- Define library testing programs' output targets, if enabled -->
   <if cond="BUILDTEST=='yes'">
     <exe id="test_array_index" template="programs">

Added: trunk/ssx/main.cpp
URL: http://svn.gna.org/viewcvs/mysqlpp/trunk/ssx/main.cpp?rev=2488&view=auto
==============================================================================
--- trunk/ssx/main.cpp (added)
+++ trunk/ssx/main.cpp Sat Mar 21 04:28:55 2009
@@ -1,0 +1,50 @@
+/***********************************************************************
+ ssx/main.cpp - Main driver module for ssqlsxlat, which does several
+       data translations related to the SSQLSv2 mechanism of MySQL++.  The
+       primary one is SSQLSv2 language files (*.ssqls) to C++ source code,
+       but there are others.  Run "ssqlsxlat -?" to get a complete list.
+
+ Copyright (c) 2009 by Educational Technology Resources, Inc.
+ Others may also hold copyrights on code in this file.  See the
+ CREDITS.txt file in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include <cmdline.h>
+
+#include <iostream>
+
+using namespace std;
+
+
+//// main //////////////////////////////////////////////////////////////
+
+int
+main(int argc, char* argv[])
+{
+       // Parse the command line
+       mysqlpp::ssqlsxlat::CommandLine cmdline(argc, argv);
+       if (cmdline) {
+               return 0;
+       }
+       else {
+               return 1;
+       }
+}
+


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to