Ticket #29002: patch-getopt.diff

File patch-getopt.diff, 9.5 KB (added by ryandesign (Ryan Carsten Schmidt), 13 years ago)
  • src/sdcv.cpp

    This is upstream r27 to remove getopt because of problems on Mac OS X
     
    3030#include <glib.h>
    3131#include <glib/gi18n.h>
    3232#include <glib/gstdio.h>
    33 #include <getopt.h>
    3433#include <string>
    3534#include <vector>
    3635#include <memory>
     
    3938#include "readline.hpp"
    4039#include "utils.hpp"
    4140
    42 const char gVersion[] = VERSION;
     41static const char gVersion[] = VERSION;
    4342
    44 
    45 struct option longopts[] ={
    46         {"version", no_argument, NULL, 'v' },
    47         {"help", no_argument, NULL, 'h' },
    48         {"list-dicts", no_argument, NULL, 'l'},
    49         {"use-dict", required_argument, NULL, 'u'},
    50         {"non-interactive", no_argument, NULL, 'n'},
    51         {"utf8-output", no_argument, NULL, 0},
    52         {"utf8-input", no_argument, NULL, 1},
    53         {"data-dir", required_argument, NULL, 2},
    54         { NULL, 0, NULL, 0 }
    55 };
    56 
    5743struct PrintDictInfo {
    5844        void operator()(const std::string& filename, bool) {
    5945                DictInfo dict_info;
     
    6551};
    6652
    6753struct CreateDisableList {
    68         CreateDisableList(const strlist_t& enable_list_, strlist_t& disable_list_) :
    69                 enable_list(enable_list_), disable_list(disable_list_)
    70         {}
     54        CreateDisableList(gchar **use_dist_list, strlist_t& disable_list_) :
     55                disable_list(disable_list_)
     56        {
     57                gchar **p = use_dist_list;
     58                while (*p) {
     59                        enable_list.push_back(*p);
     60                        ++p;
     61                }
     62        }
    7163        void operator()(const std::string& filename, bool) {
    7264                DictInfo dict_info;
    7365                if (dict_info.load_from_ifo_file(filename, false) &&
     
    7668                        disable_list.push_back(dict_info.ifo_file_name);               
    7769        }
    7870private:
    79         const strlist_t& enable_list;
     71        strlist_t enable_list;
    8072        strlist_t& disable_list;
    8173};
    8274
     75namespace {
     76void free_str_array(gchar **arr)
     77{
     78        gchar **p;
     79
     80        for (p = arr; *p; ++p)
     81                g_free(*p);
     82        g_free(arr);
     83}
     84}
     85namespace glib {
     86        typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr;
     87}
     88
    8389int main(int argc, char *argv[])
    8490{
    8591        setlocale(LC_ALL, "");
     
    8793        bindtextdomain (PACKAGE, LOCALEDIR);
    8894        textdomain (PACKAGE);
    8995#endif   
    90         int optc;
    91         bool h = false, v = false, show_list_dicts=false,
    92                 use_book_name=false, non_interactive=false,
    93                 utf8_output=false, utf8_input=false;
    94         strlist_t enable_list;
    95         string data_dir;
    96         int option_index = 0;
    97         while ((optc = getopt_long (argc, argv, "hvu:ln", longopts,
    98                                     &option_index))!=-1)
    99                 switch (optc){
    100                 case 0:
    101                         utf8_output=true;
    102                         break;
    103                 case 1:   
    104                         utf8_input=true;
    105                         break;
    106                 case 2:
    107                         data_dir=optarg;
    108                         break;
    109                 case 'v':
    110                         v = true;
    111                         break;
    112                 case 'h':
    113                         h = true;
    114                         break;
    115                 case 'l':
    116                         show_list_dicts=true;
    117                         break;
    118                 case 'u':
    119                         use_book_name=true;
    120                         enable_list.push_back(locale_to_utf8(optarg));
    121                         break;
    122                 case 'n':
    123                         non_interactive=true;
    124                         break;
    125                 case '?':
    126                         fprintf(stderr,
    127                                 _("Unknown option.\nTry '%s --help' for more information.\n"),
    128                                 argv[0]);
    129                         return EXIT_FAILURE;
    130                 }
    131  
    132         if (h) {
    133                 printf("sdcv - console version of StarDict.\n");
    134                 printf(_("Usage: %s [OPTIONS] words\n"), argv[0]);
    135                 printf(_("-h, --help               display this help and exit\n"));
    136                 printf(_("-v, --version            display version information and exit\n"));
    137                 printf(_("-l, --list-dicts         display list of available dictionaries and exit\n"));
    138                 printf(_("-u, --use-dict bookname  for search use only dictionary with this bookname\n"));
    139                 printf(_("-n, --non-interactive    for use in scripts\n"));
    140                 printf(_("--utf8-output            output must be in utf8\n"));
    141                 printf(_("--utf8-input             input of sdcv in utf8\n"));
    142                 printf(_("--data-dir path/to/dir   use this directory as path to stardict data directory\n"));
    14396
    144                 return EXIT_SUCCESS;
    145         }
     97        gboolean show_version = FALSE;
     98        gboolean show_list_dicts = FALSE;
     99        glib::StrArr use_dict_list;
     100        gboolean non_interactive = FALSE;
     101        gboolean utf8_output = FALSE;
     102        gboolean utf8_input = FALSE;
     103        glib::CharStr opt_data_dir;
    146104
    147         if (v) {
     105        GOptionEntry entries[] = {
     106                {"version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
     107                 _("display version information and exit"), NULL },
     108                {"list-dicts", 'l', 0, G_OPTION_ARG_NONE, &show_list_dicts,
     109                 _("display list of available dictionaries and exit"), NULL},
     110                {"use-dict", 'u', 0, G_OPTION_ARG_STRING_ARRAY, get_addr(use_dict_list),
     111                 _("for search use only dictionary with this bookname"),
     112                 _("bookname")},
     113                {"non-interactive", 'n', 0, G_OPTION_ARG_NONE, &non_interactive,
     114                 _("for use in scripts"), NULL},
     115                {"utf8-output", '0', 0, G_OPTION_ARG_NONE, &utf8_output,
     116                 _("output must be in utf8"), NULL},
     117                {"utf8-input", '1', 0, G_OPTION_ARG_NONE, &utf8_input,
     118                 _("input of sdcv in utf8"), NULL},
     119                {"data-dir", '2', 0, G_OPTION_ARG_STRING, get_addr(opt_data_dir),
     120                 _("use this directory as path to stardict data directory"),
     121                 _("path/to/dir")},
     122                { NULL },
     123        };
     124
     125        glib::Error error;
     126        GOptionContext *context;
     127
     128         context = g_option_context_new(_(" words"));
     129         g_option_context_set_help_enabled(context, TRUE);
     130         g_option_context_add_main_entries(context, entries, NULL);
     131         gboolean parse_res = g_option_context_parse(context, &argc, &argv, get_addr(error));
     132         g_option_context_free(context);
     133         if (!parse_res) {
     134                 fprintf(stderr, _("Invalid command line arguments: %s\n"),
     135                         error->message);               
     136                 return EXIT_FAILURE;
     137         }
     138
     139         if (show_version) {
    148140                printf(_("Console version of Stardict, version %s\n"), gVersion);
    149141                return EXIT_SUCCESS;
    150         }
     142         }
    151143
    152         const gchar *stardict_data_dir=g_getenv("STARDICT_DATA_DIR");
    153         if (data_dir.empty()) {
     144
     145        const gchar *stardict_data_dir = g_getenv("STARDICT_DATA_DIR");
     146        std::string data_dir;
     147        if (!opt_data_dir) {
    154148                if (stardict_data_dir)
    155                         data_dir=stardict_data_dir;
     149                        data_dir = stardict_data_dir;
    156150                else
    157                         data_dir="/usr/share/stardict/dic";
     151                        data_dir = "/usr/share/stardict/dic";
     152        } else {
     153                data_dir = get_impl(opt_data_dir);
    158154        }
    159155
    160156
     
    178174        strlist_t disable_list;
    179175        //DictInfoList  dict_info_list;
    180176 
    181         if (use_book_name) {
     177        if (use_dict_list) {
    182178                strlist_t empty_list;
    183                 CreateDisableList create_disable_list(enable_list, disable_list);
     179                CreateDisableList create_disable_list(get_impl(use_dict_list), disable_list);
    184180                for_each_file(dicts_dir_list, ".ifo", empty_list,
    185181                              empty_list, create_disable_list);
    186182        }
  • src/utils.cpp

     
    2727
    2828#include "utils.hpp"
    2929
    30 bool stdio_getline(FILE *in, string & str)
     30bool stdio_getline(FILE *in, std::string & str)
    3131{
    3232  str.clear();
    3333  int ch;
     
    3838  return true;
    3939}
    4040
    41 string utf8_to_locale_ign_err(const string& utf8_str)
     41std::string utf8_to_locale_ign_err(const std::string& utf8_str)
    4242{
    4343        gsize bytes_read, bytes_written;
    4444        GError *err=NULL;
    45         string res;
     45        std::string res;
    4646 
    4747        const char * charset;
    4848        if (g_get_charset(&charset))
  • src/utils.hpp

     
    11#ifndef _UTILS_HPP_
    22#define _UTILS_HPP_
    33
     4#include <glib.h>
    45#include <string>
    5 using std::string;
    66
    7 extern bool stdio_getline(FILE *in, string &str);
     7template <typename T, typename unref_res_t, void (*unref_res)(unref_res_t *)>
     8class ResourceWrapper {
     9public:
     10        ResourceWrapper(T *p = NULL) : p_(p) {}
     11        ~ResourceWrapper() { free_resource(); }
     12        T *operator->() const { return p_; }
     13        bool operator!() const { return p_ == NULL; }
     14
     15        void reset(T *newp) {
     16                if (p_ != newp) {
     17                        free_resource();
     18                        p_ = newp;
     19                }
     20        }
     21
     22        friend inline T *get_impl(const ResourceWrapper& rw) {
     23                return rw.p_;
     24        }
     25
     26        friend inline T **get_addr(ResourceWrapper& rw) {
     27                return &rw.p_;
     28        }
     29private:
     30        T *p_;
     31
     32        void free_resource() { if (p_) unref_res(p_); }
     33
     34// Helper for enabling 'if (sp)'
     35        struct Tester {
     36            Tester() {}
     37        private:
     38            void operator delete(void*);
     39        };
     40public:
     41        // enable 'if (sp)'
     42        operator Tester*() const
     43        {
     44            if (!*this) return 0;
     45            static Tester t;
     46            return &t;
     47        }
     48};
     49
     50namespace glib {
     51        typedef ResourceWrapper<gchar, void, g_free> CharStr;
     52        typedef ResourceWrapper<GError, GError, g_error_free> Error;
     53}
     54
     55
     56extern bool stdio_getline(FILE *in, std::string &str);
    857extern char *locale_to_utf8(const char *locstr);
    9 extern string utf8_to_locale_ign_err(const string& utf8_str);
     58extern std::string utf8_to_locale_ign_err(const std::string& utf8_str);
    1059
    1160#endif//!_UTILS_HPP_
  • src/libwrapper.cpp

     
    220220
    221221void Library::print_search_result(FILE *out, const TSearchResult & res)
    222222{
    223         string loc_bookname, loc_def, loc_exp;
     223        std::string loc_bookname, loc_def, loc_exp;
    224224        if(!utf8_output){
    225225                loc_bookname=utf8_to_locale_ign_err(res.bookname);
    226226                loc_def=utf8_to_locale_ign_err(res.def);
     
    375375                }
    376376   
    377377        } else {
    378                 string loc_str;
     378                std::string loc_str;
    379379                if (!utf8_output)
    380380                        loc_str=utf8_to_locale_ign_err(str);
    381381   
  • src/Makefile.am

     
    11SUBDIRS=lib
    22bin_PROGRAMS = sdcv
    33sdcv_SOURCES = \
    4         sdcv.cpp getopt.c getopt1.c getopt.h \
     4        sdcv.cpp \
    55        libwrapper.cpp libwrapper.hpp \
    66        readline.cpp readline.hpp \
    77        utils.cpp utils.hpp