]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/getopt-long.cc
Doc-es: various updates.
[lilypond.git] / flower / getopt-long.cc
index a70acba4180d63edb03449703dd9d784e160e0bc..9adc0c3592d2a2de84ea96e7e4b6935988ec4479 100644 (file)
@@ -1,27 +1,36 @@
 /*
-   process command line, GNU style.
+  This file is part of LilyPond, the GNU music typesetter.
 
-   this is Copyleft (c)  1996--2003 Han-Wen Nienhuys, <hanwen@cs.uu.nl>
- */
+  Copyright (C) 1996--2015 Han-Wen Nienhuys, <hanwen@xs4all.nl>
 
-#include <string.h>
-#include <stdio.h>
-#include <assert.h>
-#include <stdlib.h>
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
 
-#include <iostream>
+  LilyPond 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-#include "config.h"
 #include "getopt-long.hh"
-#include "international.hh"
-#include "string-convert.hh"
 
+#include <cstring>
+#include <cassert>
+#include <cstdlib>
+
+#include "config.hh"
+#include "international.hh"
 
 #if !HAVE_GETTEXT
-inline char*
-gettext (char consts)
+inline char *
+gettext (char const *s)
 {
-  return (char*)s;
+  return (char *)s;
 }
 #else
 #include <libintl.h>
@@ -41,22 +50,22 @@ Getopt_long::get_argument_index ()
 const Long_option_init *
 Getopt_long::parselong ()
 {
-  char const *optnm = arg_value_char_a_a_[array_index_] + 2 ;
+  char const *optnm = arg_value_char_a_a_[array_index_] + 2;
   assert (*optnm);
 
   char const *endopt = strchr (optnm, '=');
-  int searchlen  = (endopt) ? endopt - optnm : strlen (optnm);
+  size_t searchlen = (endopt) ? (size_t) (endopt - optnm) : strlen (optnm);
 
-  found_option_=0;
-  for (int i=0; i< table_len_; i++)
+  found_option_ = 0;
+  for (int i = 0; i < table_len_; i++)
     {
       char const *ln = option_a_[i].longname_str0_;
 
       if (ln && !strncmp (ln, optnm, searchlen))
-       {
-         found_option_ = option_a_+i;
-         break;
-       }
+        {
+          found_option_ = option_a_ + i;
+          break;
+        }
     }
 
   if (!found_option_)
@@ -67,47 +76,45 @@ Getopt_long::parselong ()
   array_index_++;
   argument_index_ = 0;
 
-
   if (found_option_->take_arg_str0_)
     {
       if (endopt)
-       optional_argument_str0_ = endopt +1; // a '='
+        optional_argument_str0_ = endopt + 1; // a '='
       else
-       {
-         optional_argument_str0_ = arg_value_char_a_a_[array_index_];
-         array_index_++;
-       }
+        {
+          optional_argument_str0_ = arg_value_char_a_a_[array_index_];
+          array_index_++;
+        }
       if (!optional_argument_str0_)
-       report (E_ARGEXPECT);
-
+        report (E_ARGEXPECT);
     }
   else
     {
       optional_argument_str0_ = 0;
       if (endopt)
-       report (E_NOARGEXPECT);
+        report (E_NOARGEXPECT);
     }
 
   return found_option_;
 }
 
-String
+string
 Long_option_init::to_string () const
 {
-  String str;
+  string str;
   if (shortname_char_)
-    str +=  "-" + shortname_char_;
+    str += string ("-") + shortname_char_;
   if (shortname_char_ && longname_str0_)
     str += ", ";
   if (longname_str0_)
-    str += String ("`--") + longname_str0_ + "'";
+    str += string ("--") + longname_str0_;
   return str;
 }
 
-String
+string
 Long_option_init::str_for_help () const
 {
-  String s;
+  string s;
   if (shortname_char_)
     s = "-" + ::to_string (shortname_char_);
   else
@@ -121,10 +128,10 @@ Long_option_init::str_for_help () const
   if (take_arg_str0_)
     {
       if (longname_str0_)
-       s = s + "=";
+        s = s + "=";
       else
-       s = s + " ";
-      
+        s = s + " ";
+
       s = s + gettext (take_arg_str0_);
     }
   return s;
@@ -138,48 +145,47 @@ Getopt_long::report (Errorcod c)
   if (!error_out_)
     return;
 
-  String str = arg_value_char_a_a_[0];
+  string str = arg_value_char_a_a_[0];
   str += ": ";
   switch (c)
     {
     case E_ARGEXPECT:
       str += _f ("option `%s' requires an argument",
-       found_option_->to_string ());
+                 found_option_->to_string ());
       break;
-    case  E_NOARGEXPECT:
-      str += _f ("option `%s' doesn't allow an argument",
-       found_option_->to_string ());
+    case E_NOARGEXPECT:
+      str += _f ("option `%s' does not allow an argument",
+                 found_option_->to_string ());
       break;
     case E_UNKNOWNOPTION:
       str += _f ("unrecognized option: `%s'",
-      String (argument_index_ 
-             ? String ("-" + String_convert::form_string ("%c", 
-               arg_value_char_a_a_[array_index_][argument_index_]))
-             : String (arg_value_char_a_a_[array_index_])));
+                 string (argument_index_
+                         ? string ("-" + string (1, arg_value_char_a_a_[array_index_][argument_index_]))
+                         : string (arg_value_char_a_a_[array_index_])));
       break;
     case E_ILLEGALARG:
       str += _f ("invalid argument `%s' to option `%s'",
-        optional_argument_str0_, found_option_->to_string ());
+                 optional_argument_str0_, found_option_->to_string ());
       break;
     default:
       assert (false);
     }
-  fprintf(error_out_, "%s\n", str.to_str0 ());
+  fprintf (error_out_, "%s\n", str.c_str ());
   exit (2);
 }
 
 const Long_option_init *
 Getopt_long::parseshort ()
 {
-  char c=arg_value_char_a_a_[array_index_][argument_index_];
-  found_option_=0;
+  char c = arg_value_char_a_a_[array_index_][argument_index_];
+  found_option_ = 0;
   assert (c);
 
-  for (int i=0; i < table_len_; i++)
+  for (int i = 0; i < table_len_; i++)
     if (option_a_[i].shortname_char_ == c)
       {
-       found_option_  = option_a_+i;
-       break;
+        found_option_ = option_a_ + i;
+        break;
       }
 
   if (!found_option_)
@@ -196,18 +202,16 @@ Getopt_long::parseshort ()
     }
   optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
 
-  array_index_ ++;
+  array_index_++;
   argument_index_ = 0;
 
   if (!optional_argument_str0_[0])
     {
       optional_argument_str0_ = arg_value_char_a_a_[array_index_];
-      array_index_ ++;
+      array_index_++;
     }
   if (!optional_argument_str0_)
-    {
-      report (E_ARGEXPECT);
-    }
+    report (E_ARGEXPECT);
 
   return found_option_;
 }
@@ -225,34 +229,31 @@ Getopt_long::operator () ()
   if (argument_index_)
     return parseshort ();
 
-  const char * argument = arg_value_char_a_a_[array_index_];
+  char const *argument = arg_value_char_a_a_[array_index_];
 
   if (argument[0] != '-')
     return 0;
 
-  if (argument[1] == '-') {// what to do with "command  --  bla"
-    if (argument[2])
-      return parselong ();
-    else
-      return 0;
-  }
+  if (argument[1] == '-')  // what to do with "command  --  bla"
+    {
+      if (argument[2])
+        return parselong ();
+      else
+        return 0;
+    }
   else
     {
       if (argument[ 1 ])
-       {
-         argument_index_ = 1;
-         return parseshort ();
-       }
+        {
+          argument_index_ = 1;
+          return parseshort ();
+        }
       else
-       {
-         return 0;
-       }
+        return 0;
     }
 }
 
-
-
-Getopt_long::Getopt_long (int c, char  **v, Long_option_init *lo)
+Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
 {
   option_a_ = lo;
   error_out_ = stderr;
@@ -262,16 +263,15 @@ Getopt_long::Getopt_long (int c, char  **v, Long_option_init *lo)
   argument_index_ = 0;
 
   //    reached end of option table?
-  table_len_ =0;
-  for (int i = 0;  option_a_[i].longname_str0_ ||option_a_[i].shortname_char_; i++)
-    table_len_ ++;
-
+  table_len_ = 0;
+  for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
+    table_len_++;
 }
 
 bool
 Getopt_long::ok () const
 {
-  return  array_index_ < argument_count_;
+  return array_index_ < argument_count_;
 }
 
 void
@@ -279,7 +279,7 @@ Getopt_long::next ()
 {
   error_ = E_NOERROR;
   while (array_index_ < argument_count_
-        && !arg_value_char_a_a_[array_index_][argument_index_])
+         && !arg_value_char_a_a_[array_index_][argument_index_])
     {
       array_index_++;
       argument_index_ = 0;
@@ -291,53 +291,51 @@ Getopt_long::current_arg ()
 {
   if (array_index_ >= argument_count_)
     return 0;
-  char const * a = arg_value_char_a_a_[array_index_];
+  char const *a = arg_value_char_a_a_[array_index_];
   return a + argument_index_;
 }
 
 char const *
 Getopt_long::get_next_arg ()
 {
-  char const * a = current_arg ();
+  char const *a = current_arg ();
   if (a)
     {
-      array_index_ ++;
-      argument_index_= 0;
+      array_index_++;
+      argument_index_ = 0;
     }
   return a;
 }
 
-
 const int EXTRA_SPACES = 5;
 
-String
-Long_option_init::table_string (Long_option_init *l) 
+string
+Long_option_init::table_string (Long_option_init *l)
 {
-  String argstr = "ARG";
-  String tabstr = "";
+  string tabstr = "";
 
-  int wid = 0;
-  for (int i=0; l[i].shortname_char_ || l[i].longname_str0_; i++)
-    {
-      wid = wid >? l[i].str_for_help ().length ();
-    }
+  size_t wid = 0;
+  for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
+    wid = max (wid, l[i].str_for_help ().length ());
 
-  for (int i=0; l[i].shortname_char_ || l[i].longname_str0_; i++)
+  for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
     {
-      String s  =  "  " + l[i].str_for_help ();
-      s += String_convert::char_string (' ', wid - s.length () + EXTRA_SPACES);
+      string s = "  " + l[i].str_for_help ();
+      s += string (wid - s.length () + EXTRA_SPACES, ' ');
 
-      tabstr += s + gettext (l[i].help_str0_) + "\n";
+      string help_text (gettext (l[i].help_str0_));
+      replace_all (&help_text, "\n",
+                   "\n" + string (wid + EXTRA_SPACES + 2, ' '));
+      tabstr += s + help_text + "\n";
     }
 
-    
   return tabstr;
 }
 
 int
 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
 {
-  if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
+  if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_ - b.shortname_char_)
     return a.shortname_char_ - b.shortname_char_;
 
   if (b.shortname_char_ && a.longname_str0_)
@@ -350,6 +348,6 @@ Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
       char s[2] = {a.shortname_char_, 0};
       return strcmp (s, b.longname_str0_);
     }
-  
+
   return strcmp (a.longname_str0_, b.longname_str0_);
 }