harfbuzz/util/options.cc

192 lines
4.9 KiB
C++
Raw Normal View History

2011-08-11 11:54:31 +02:00
/*
* Copyright © 2011,2012 Google, Inc.
2011-08-11 11:54:31 +02:00
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#include "options.hh"
static gboolean
parse_text (const char *name G_GNUC_UNUSED,
const char *arg,
gpointer data,
GError **error G_GNUC_UNUSED)
{
text_options_t *text_opts = (text_options_t *) data;
if (text_opts->text)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Either --text or --unicodes can be provided but not both");
return false;
}
text_opts->text_len = -1;
text_opts->text = g_strdup (arg);
return true;
}
static gboolean
parse_unicodes (const char *name G_GNUC_UNUSED,
const char *arg,
gpointer data,
GError **error G_GNUC_UNUSED)
{
text_options_t *text_opts = (text_options_t *) data;
if (text_opts->text)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Either --text or --unicodes can be provided but not both");
return false;
}
2017-10-15 12:11:08 +02:00
GString *gs = g_string_new (nullptr);
if (0 == strcmp (arg, "*"))
{
g_string_append_c (gs, '*');
}
else
{
char *s = (char *) arg;
char *p;
while (s && *s)
{
#define DELIMITERS "<+>{},;&#\\xXuUnNiI\n\t\v\f\r "
while (*s && strchr (DELIMITERS, *s))
s++;
if (!*s)
break;
errno = 0;
hb_codepoint_t u = strtoul (s, &p, 16);
if (errno || s == p)
{
g_string_free (gs, TRUE);
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
2019-12-31 13:23:02 +01:00
"Failed parsing Unicode values at: '%s'", s);
return false;
}
g_string_append_unichar (gs, u);
s = p;
}
}
text_opts->text_len = gs->len;
text_opts->text = g_string_free (gs, FALSE);
return true;
}
2011-09-08 22:00:04 +02:00
void
text_options_t::add_options (option_parser_t *parser)
2011-09-08 22:00:04 +02:00
{
GOptionEntry entries[] =
{
{"text", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_text, "Set input text", "string"},
2012-11-14 00:15:09 +01:00
{"text-file", 0, 0, G_OPTION_ARG_STRING, &this->text_file, "Set input text file-name\n\n If no text is provided, standard input is used for input.\n", "filename"},
{"unicodes", 'u', 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_unicodes, "Set input Unicode codepoints", "list of hex numbers"},
{"text-before", 0, 0, G_OPTION_ARG_STRING, &this->text_before, "Set text context before each line", "string"},
{"text-after", 0, 0, G_OPTION_ARG_STRING, &this->text_after, "Set text context after each line", "string"},
2017-10-15 12:11:08 +02:00
{nullptr}
2011-08-11 11:54:31 +02:00
};
parser->add_group (entries,
"text",
"Text options:",
"Options for the input text",
this);
}
const char *
text_options_t::get_line (unsigned int *len, int eol)
{
2011-09-16 08:08:36 +02:00
if (text) {
2018-11-06 17:03:34 +01:00
if (!line)
{
line = text;
line_len = text_len;
2018-11-06 17:03:34 +01:00
}
if (line_len == UINT_MAX)
2015-11-03 20:34:47 +01:00
line_len = strlen (line);
2011-09-16 08:08:36 +02:00
2015-11-03 20:34:47 +01:00
if (!line_len) {
2011-09-16 08:08:36 +02:00
*len = 0;
2017-10-15 12:11:08 +02:00
return nullptr;
2011-09-16 08:08:36 +02:00
}
2015-11-03 20:34:47 +01:00
const char *ret = line;
const char *p = (const char *) memchr (line, eol, line_len);
2011-09-16 08:08:36 +02:00
unsigned int ret_len;
if (!p) {
2015-11-03 20:34:47 +01:00
ret_len = line_len;
line += ret_len;
line_len = 0;
2011-09-16 08:08:36 +02:00
} else {
ret_len = p - ret;
2015-11-03 20:34:47 +01:00
line += ret_len + 1;
line_len -= ret_len + 1;
2011-09-16 08:08:36 +02:00
}
*len = ret_len;
return ret;
}
if (!fp) {
if (!text_file)
2012-06-06 02:35:40 +02:00
fail (true, "At least one of text or text-file must be set");
2011-09-16 08:08:36 +02:00
if (0 != strcmp (text_file, "-"))
fp = fopen (text_file, "r");
else
fp = stdin;
2011-08-11 11:54:31 +02:00
2011-09-16 08:08:36 +02:00
if (!fp)
2012-06-06 02:35:40 +02:00
fail (false, "Failed opening text file `%s': %s",
2011-09-16 08:08:36 +02:00
text_file, strerror (errno));
2017-10-15 12:11:08 +02:00
gs = g_string_new (nullptr);
}
2011-09-16 08:08:36 +02:00
g_string_set_size (gs, 0);
char buf[BUFSIZ];
while (fgets (buf, sizeof (buf), fp))
{
unsigned bytes = strlen (buf);
if (bytes && (int) (unsigned char) buf[bytes - 1] == eol)
{
2011-09-16 08:08:36 +02:00
bytes--;
g_string_append_len (gs, buf, bytes);
break;
}
g_string_append_len (gs, buf, bytes);
2011-08-11 11:54:31 +02:00
}
2011-09-16 08:08:36 +02:00
if (ferror (fp))
fail (false, "Failed reading text: %s", strerror (errno));
2011-09-16 08:08:36 +02:00
*len = gs->len;
2017-10-15 12:11:08 +02:00
return !*len && feof (fp) ? nullptr : gs->str;
2011-08-11 11:54:31 +02:00
}