2021-08-07 21:38:19 +02:00
struct text_options_t
{
~ text_options_t ( )
{
g_free ( text_before ) ;
g_free ( text_after ) ;
g_free ( text ) ;
g_free ( text_file ) ;
if ( gs )
g_string_free ( gs , true ) ;
if ( fp & & fp ! = stdin )
fclose ( fp ) ;
}
void add_options ( option_parser_t * parser ) ;
void post_parse ( GError * * error G_GNUC_UNUSED )
{
if ( text & & text_file )
g_set_error ( error ,
G_OPTION_ERROR , G_OPTION_ERROR_BAD_VALUE ,
" Only one of text and text-file can be set " ) ;
}
2021-08-10 03:08:34 +02:00
const char * get_line ( unsigned int * len ) ;
2021-08-07 21:38:19 +02:00
char * text_before = nullptr ;
char * text_after = nullptr ;
int text_len = - 1 ;
char * text = nullptr ;
char * text_file = nullptr ;
private :
FILE * fp = nullptr ;
GString * gs = nullptr ;
} ;
2017-09-02 04:09:54 +02:00
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 ;
}
2018-11-06 16:49:19 +01:00
text_opts - > text_len = - 1 ;
2017-09-02 04:09:54 +02:00
text_opts - > text = g_strdup ( arg ) ;
return true ;
}
static gboolean
parse_unicodes ( const char * name G_GNUC_UNUSED ,
2019-08-24 15:27:14 +02:00
const char * arg ,
gpointer data ,
GError * * error G_GNUC_UNUSED )
2017-09-02 04:09:54 +02:00
{
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 ) ;
2019-08-24 15:27:14 +02:00
if ( 0 = = strcmp ( arg , " * " ) )
2017-09-02 04:09:54 +02:00
{
2019-06-26 22:23:24 +02:00
g_string_append_c ( gs , ' * ' ) ;
}
else
{
char * s = ( char * ) arg ;
char * p ;
2019-08-24 15:27:14 +02:00
2019-06-26 22:23:24 +02:00
while ( s & & * s )
2017-09-02 04:09:54 +02:00
{
2021-08-07 07:24:28 +02:00
# define DELIMITERS "<+>{},;&#\\xXuUnNiI\n\t\v\f\r "
2019-06-26 22:23:24 +02:00
while ( * s & & strchr ( DELIMITERS , * s ) )
2019-08-24 15:27:14 +02:00
s + + ;
2019-06-26 22:23:24 +02:00
if ( ! * s )
2019-08-24 15:27:14 +02:00
break ;
2019-06-26 22:23:24 +02:00
errno = 0 ;
hb_codepoint_t u = strtoul ( s , & p , 16 ) ;
if ( errno | | s = = p )
{
2019-08-24 15:27:14 +02:00
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 ) ;
2019-08-24 15:27:14 +02:00
return false ;
2019-06-26 22:23:24 +02:00
}
2019-08-24 15:27:14 +02:00
2019-06-26 22:23:24 +02:00
g_string_append_unichar ( gs , u ) ;
2019-08-24 15:27:14 +02:00
2019-06-26 22:23:24 +02:00
s = p ;
2017-09-02 04:09:54 +02:00
}
}
2018-11-06 16:49:19 +01:00
text_opts - > text_len = gs - > len ;
2017-09-02 04:09:54 +02:00
text_opts - > text = g_string_free ( gs , FALSE ) ;
return true ;
}
2011-09-13 19:30:39 +02:00
const char *
2021-08-10 03:08:34 +02:00
text_options_t : : get_line ( unsigned int * len )
2011-09-13 19:30:39 +02:00
{
2021-08-07 21:38:19 +02:00
if ( text )
{
2021-08-10 03:08:34 +02:00
if ( text_len = = - 2 )
2018-11-06 17:03:34 +01:00
{
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
}
2021-08-10 03:08:34 +02:00
if ( text_len = = - 1 )
text_len = strlen ( text ) ;
2011-09-16 08:08:36 +02:00
2021-08-10 03:08:34 +02:00
* len = text_len ;
text_len = - 2 ;
return text ;
2011-09-16 08:08:36 +02:00
}
2021-08-07 21:38:19 +02:00
if ( ! fp )
{
2011-09-13 19:30:39 +02:00
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-13 19:30:39 +02:00
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 ) ) ;
2011-09-13 19:30:39 +02:00
2017-10-15 12:11:08 +02:00
gs = g_string_new ( nullptr ) ;
2011-09-13 19:30:39 +02:00
}
2011-09-16 08:08:36 +02:00
g_string_set_size ( gs , 0 ) ;
char buf [ BUFSIZ ] ;
2021-08-01 15:59:25 +02:00
while ( fgets ( buf , sizeof ( buf ) , fp ) )
{
unsigned bytes = strlen ( buf ) ;
2021-08-10 03:08:34 +02:00
if ( bytes & & buf [ bytes - 1 ] = = ' \n ' )
2021-08-01 15:59:25 +02:00
{
2011-09-16 08:08:36 +02:00
bytes - - ;
g_string_append_len ( gs , buf , bytes ) ;
break ;
}
2021-08-01 15:59:25 +02:00
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 ) )
2021-08-01 15:59:25 +02:00
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
}
2021-08-07 21:38:19 +02:00
void
text_options_t : : add_options ( option_parser_t * parser )
{
GOptionEntry entries [ ] =
{
{ " text " , 0 , 0 , G_OPTION_ARG_CALLBACK , ( gpointer ) & parse_text , " Set input text " , " string " } ,
2021-08-10 21:59:46 +02:00
{ " text-file " , 0 , 0 , G_OPTION_ARG_STRING , & this - > text_file , " Set input text file-name " , " filename " } ,
{ " unicodes " , ' u ' , 0 , G_OPTION_ARG_CALLBACK , ( gpointer ) & parse_unicodes , " Set input Unicode codepoints \n \n If no text is provided, standard input is used for input. \n " , " list of hex numbers " } ,
2021-08-07 21:38:19 +02:00
{ " 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 " } ,
{ nullptr }
} ;
parser - > add_group ( entries ,
" text " ,
" Text options: " ,
" Options for the input text " ,
this ) ;
}