options.hh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright © 2011 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef OPTIONS_HH
  27. #define OPTIONS_HH
  28. #include "hb.hh"
  29. #include <stdlib.h>
  30. #include <stddef.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <assert.h>
  34. #include <math.h>
  35. #include <locale.h>
  36. #include <errno.h>
  37. #include <fcntl.h>
  38. #ifdef HAVE_UNISTD_H
  39. #include <unistd.h> /* for isatty() */
  40. #endif
  41. #if defined(_WIN32) || defined(__CYGWIN__)
  42. #include <io.h> /* for setmode() under Windows */
  43. #endif
  44. #include <hb.h>
  45. #include <hb-ot.h>
  46. #include <glib.h>
  47. #include <glib/gprintf.h>
  48. static inline void fail (hb_bool_t suggest_help, const char *format, ...) G_GNUC_NORETURN G_GNUC_PRINTF (2, 3);
  49. static inline void
  50. fail (hb_bool_t suggest_help, const char *format, ...)
  51. {
  52. const char *msg;
  53. va_list vap;
  54. va_start (vap, format);
  55. msg = g_strdup_vprintf (format, vap);
  56. va_end (vap);
  57. const char *prgname = g_get_prgname ();
  58. g_printerr ("%s: %s\n", prgname, msg);
  59. if (suggest_help)
  60. g_printerr ("Try `%s --help' for more information.\n", prgname);
  61. exit (1);
  62. }
  63. struct option_parser_t
  64. {
  65. option_parser_t (const char *parameter_string = nullptr)
  66. : context (g_option_context_new (parameter_string)),
  67. to_free (g_ptr_array_new ())
  68. {}
  69. static void _g_free_g_func (void *p, void * G_GNUC_UNUSED) { g_free (p); }
  70. ~option_parser_t ()
  71. {
  72. g_option_context_free (context);
  73. g_ptr_array_foreach (to_free, _g_free_g_func, nullptr);
  74. g_ptr_array_free (to_free, TRUE);
  75. }
  76. void add_options ();
  77. static void
  78. post_parse_ (void *thiz, GError **error) {}
  79. template <typename Type>
  80. static auto
  81. post_parse_ (Type *thiz, GError **error) -> decltype (thiz->post_parse (error))
  82. { thiz->post_parse (error); }
  83. template <typename Type>
  84. static gboolean
  85. post_parse (GOptionContext *context G_GNUC_UNUSED,
  86. GOptionGroup *group G_GNUC_UNUSED,
  87. gpointer data,
  88. GError **error)
  89. {
  90. option_parser_t::post_parse_ (static_cast<Type *> (data), error);
  91. return !*error;
  92. }
  93. template <typename Type>
  94. void add_group (GOptionEntry *entries,
  95. const gchar *name,
  96. const gchar *description,
  97. const gchar *help_description,
  98. Type *closure,
  99. bool add_parse_hooks = true)
  100. {
  101. GOptionGroup *group = g_option_group_new (name, description, help_description,
  102. static_cast<gpointer>(closure), nullptr);
  103. g_option_group_add_entries (group, entries);
  104. if (add_parse_hooks)
  105. g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
  106. g_option_context_add_group (context, group);
  107. }
  108. template <typename Type>
  109. void add_main_group (GOptionEntry *entries,
  110. Type *closure)
  111. {
  112. GOptionGroup *group = g_option_group_new (nullptr, nullptr, nullptr,
  113. static_cast<gpointer>(closure), nullptr);
  114. g_option_group_add_entries (group, entries);
  115. /* https://gitlab.gnome.org/GNOME/glib/-/issues/2460 */
  116. //g_option_group_set_parse_hooks (group, nullptr, post_parse<Type>);
  117. g_option_context_set_main_group (context, group);
  118. }
  119. void set_summary (const char *summary)
  120. {
  121. g_option_context_set_summary (context, summary);
  122. }
  123. void set_description (const char *description)
  124. {
  125. g_option_context_set_description (context, description);
  126. }
  127. void free_later (char *p) {
  128. g_ptr_array_add (to_free, p);
  129. }
  130. bool parse (int *argc, char ***argv, bool ignore_error = false);
  131. GOptionContext *context;
  132. protected:
  133. GPtrArray *to_free;
  134. };
  135. static inline gchar *
  136. shapers_to_string ()
  137. {
  138. GString *shapers = g_string_new (nullptr);
  139. const char **shaper_list = hb_shape_list_shapers ();
  140. for (; *shaper_list; shaper_list++) {
  141. g_string_append (shapers, *shaper_list);
  142. g_string_append_c (shapers, ',');
  143. }
  144. g_string_truncate (shapers, MAX (0, (gint)shapers->len - 1));
  145. return g_string_free (shapers, false);
  146. }
  147. static G_GNUC_NORETURN gboolean
  148. show_version (const char *name G_GNUC_UNUSED,
  149. const char *arg G_GNUC_UNUSED,
  150. gpointer data G_GNUC_UNUSED,
  151. GError **error G_GNUC_UNUSED)
  152. {
  153. g_printf ("%s (%s) %s\n", g_get_prgname (), PACKAGE_NAME, PACKAGE_VERSION);
  154. char *shapers = shapers_to_string ();
  155. g_printf ("Available shapers: %s\n", shapers);
  156. g_free (shapers);
  157. if (strcmp (HB_VERSION_STRING, hb_version_string ()))
  158. g_printf ("Linked HarfBuzz library has a different version: %s\n", hb_version_string ());
  159. exit(0);
  160. }
  161. inline void
  162. option_parser_t::add_options ()
  163. {
  164. GOptionEntry entries[] =
  165. {
  166. {"version", 0, G_OPTION_FLAG_NO_ARG,
  167. G_OPTION_ARG_CALLBACK, (gpointer) &show_version, "Show version numbers", nullptr},
  168. {nullptr}
  169. };
  170. g_option_context_add_main_entries (context, entries, nullptr);
  171. }
  172. inline bool
  173. option_parser_t::parse (int *argc, char ***argv, bool ignore_error)
  174. {
  175. setlocale (LC_ALL, "");
  176. GError *parse_error = nullptr;
  177. if (!g_option_context_parse (context, argc, argv, &parse_error))
  178. {
  179. if (parse_error)
  180. {
  181. if (!ignore_error)
  182. fail (true, "%s", parse_error->message);
  183. g_error_free (parse_error);
  184. }
  185. else
  186. {
  187. if (!ignore_error)
  188. fail (true, "Option parse error");
  189. }
  190. return false;
  191. }
  192. return true;
  193. }
  194. /* fallback implementation for scalbn()/scalbnf() for pre-2013 MSVC */
  195. #if defined (_MSC_VER) && (_MSC_VER < 1800)
  196. #ifndef FLT_RADIX
  197. #define FLT_RADIX 2
  198. #endif
  199. __inline long double scalbn (long double x, int exp)
  200. {
  201. return x * (pow ((long double) FLT_RADIX, exp));
  202. }
  203. __inline float scalbnf (float x, int exp)
  204. {
  205. return x * (pow ((float) FLT_RADIX, exp));
  206. }
  207. #endif
  208. #endif