Ticket #29002: patch-getopt.diff
File patch-getopt.diff, 9.5 KB (added by ryandesign (Ryan Carsten Schmidt), 14 years ago) |
---|
-
src/sdcv.cpp
This is upstream r27 to remove getopt because of problems on Mac OS X
30 30 #include <glib.h> 31 31 #include <glib/gi18n.h> 32 32 #include <glib/gstdio.h> 33 #include <getopt.h>34 33 #include <string> 35 34 #include <vector> 36 35 #include <memory> … … 39 38 #include "readline.hpp" 40 39 #include "utils.hpp" 41 40 42 const char gVersion[] = VERSION;41 static const char gVersion[] = VERSION; 43 42 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 57 43 struct PrintDictInfo { 58 44 void operator()(const std::string& filename, bool) { 59 45 DictInfo dict_info; … … 65 51 }; 66 52 67 53 struct 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 } 71 63 void operator()(const std::string& filename, bool) { 72 64 DictInfo dict_info; 73 65 if (dict_info.load_from_ifo_file(filename, false) && … … 76 68 disable_list.push_back(dict_info.ifo_file_name); 77 69 } 78 70 private: 79 const strlist_t&enable_list;71 strlist_t enable_list; 80 72 strlist_t& disable_list; 81 73 }; 82 74 75 namespace { 76 void 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 } 85 namespace glib { 86 typedef ResourceWrapper<gchar *, gchar *, free_str_array> StrArr; 87 } 88 83 89 int main(int argc, char *argv[]) 84 90 { 85 91 setlocale(LC_ALL, ""); … … 87 93 bindtextdomain (PACKAGE, LOCALEDIR); 88 94 textdomain (PACKAGE); 89 95 #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"));143 96 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; 146 104 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) { 148 140 printf(_("Console version of Stardict, version %s\n"), gVersion); 149 141 return EXIT_SUCCESS; 150 }142 } 151 143 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) { 154 148 if (stardict_data_dir) 155 data_dir =stardict_data_dir;149 data_dir = stardict_data_dir; 156 150 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); 158 154 } 159 155 160 156 … … 178 174 strlist_t disable_list; 179 175 //DictInfoList dict_info_list; 180 176 181 if (use_ book_name) {177 if (use_dict_list) { 182 178 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); 184 180 for_each_file(dicts_dir_list, ".ifo", empty_list, 185 181 empty_list, create_disable_list); 186 182 } -
src/utils.cpp
27 27 28 28 #include "utils.hpp" 29 29 30 bool stdio_getline(FILE *in, st ring & str)30 bool stdio_getline(FILE *in, std::string & str) 31 31 { 32 32 str.clear(); 33 33 int ch; … … 38 38 return true; 39 39 } 40 40 41 st ring utf8_to_locale_ign_err(conststring& utf8_str)41 std::string utf8_to_locale_ign_err(const std::string& utf8_str) 42 42 { 43 43 gsize bytes_read, bytes_written; 44 44 GError *err=NULL; 45 st ring res;45 std::string res; 46 46 47 47 const char * charset; 48 48 if (g_get_charset(&charset)) -
src/utils.hpp
1 1 #ifndef _UTILS_HPP_ 2 2 #define _UTILS_HPP_ 3 3 4 #include <glib.h> 4 5 #include <string> 5 using std::string;6 6 7 extern bool stdio_getline(FILE *in, string &str); 7 template <typename T, typename unref_res_t, void (*unref_res)(unref_res_t *)> 8 class ResourceWrapper { 9 public: 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 } 29 private: 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 }; 40 public: 41 // enable 'if (sp)' 42 operator Tester*() const 43 { 44 if (!*this) return 0; 45 static Tester t; 46 return &t; 47 } 48 }; 49 50 namespace glib { 51 typedef ResourceWrapper<gchar, void, g_free> CharStr; 52 typedef ResourceWrapper<GError, GError, g_error_free> Error; 53 } 54 55 56 extern bool stdio_getline(FILE *in, std::string &str); 8 57 extern char *locale_to_utf8(const char *locstr); 9 extern st ring utf8_to_locale_ign_err(conststring& utf8_str);58 extern std::string utf8_to_locale_ign_err(const std::string& utf8_str); 10 59 11 60 #endif//!_UTILS_HPP_ -
src/libwrapper.cpp
220 220 221 221 void Library::print_search_result(FILE *out, const TSearchResult & res) 222 222 { 223 st ring loc_bookname, loc_def, loc_exp;223 std::string loc_bookname, loc_def, loc_exp; 224 224 if(!utf8_output){ 225 225 loc_bookname=utf8_to_locale_ign_err(res.bookname); 226 226 loc_def=utf8_to_locale_ign_err(res.def); … … 375 375 } 376 376 377 377 } else { 378 st ring loc_str;378 std::string loc_str; 379 379 if (!utf8_output) 380 380 loc_str=utf8_to_locale_ign_err(str); 381 381 -
src/Makefile.am
1 1 SUBDIRS=lib 2 2 bin_PROGRAMS = sdcv 3 3 sdcv_SOURCES = \ 4 sdcv.cpp getopt.c getopt1.c getopt.h\4 sdcv.cpp \ 5 5 libwrapper.cpp libwrapper.hpp \ 6 6 readline.cpp readline.hpp \ 7 7 utils.cpp utils.hpp