2011-08-11 11:54:31 +02:00
/*
2012-05-25 20:30:24 +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"
2011-09-16 22:40:44 +02:00
# ifdef HAVE_FREETYPE
2011-09-13 19:30:39 +02:00
# include <hb-ft.h>
2014-07-05 21:50:18 +02:00
# endif
2015-06-03 21:07:46 +02:00
# include <hb-ot.h>
2011-08-11 11:54:31 +02:00
2018-09-30 11:47:36 +02:00
static struct supported_font_funcs_t {
2014-07-05 21:50:18 +02:00
char name [ 4 ] ;
void ( * func ) ( hb_font_t * ) ;
} supported_font_funcs [ ] =
{
# ifdef HAVE_FREETYPE
{ " ft " , hb_ft_font_set_funcs } ,
# endif
{ " ot " , hb_ot_font_set_funcs } ,
} ;
2011-08-11 11:54:31 +02:00
2017-01-22 02:51:41 +01:00
static gboolean
parse_variations ( 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-01-22 02:51:41 +01:00
{
font_options_t * font_opts = ( font_options_t * ) data ;
char * s = ( char * ) arg ;
char * p ;
font_opts - > num_variations = 0 ;
g_free ( font_opts - > variations ) ;
2017-10-15 12:11:08 +02:00
font_opts - > variations = nullptr ;
2017-01-22 02:51:41 +01:00
if ( ! * s )
return true ;
/* count the variations first, so we can allocate memory */
p = s ;
do {
font_opts - > num_variations + + ;
p = strchr ( p , ' , ' ) ;
if ( p )
p + + ;
} while ( p ) ;
font_opts - > variations = ( hb_variation_t * ) calloc ( font_opts - > num_variations , sizeof ( * font_opts - > variations ) ) ;
2017-08-10 07:09:01 +02:00
if ( ! font_opts - > variations )
return false ;
2017-01-22 02:51:41 +01:00
/* now do the actual parsing */
p = s ;
font_opts - > num_variations = 0 ;
while ( p & & * p ) {
char * end = strchr ( p , ' , ' ) ;
if ( hb_variation_from_string ( p , end ? end - p : - 1 , & font_opts - > variations [ font_opts - > num_variations ] ) )
font_opts - > num_variations + + ;
2017-10-15 12:11:08 +02:00
p = end ? end + 1 : nullptr ;
2017-01-22 02:51:41 +01:00
}
return true ;
}
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 ;
}
2015-01-20 21:30:45 +01:00
static gboolean
parse_font_size ( const char * name G_GNUC_UNUSED ,
const char * arg ,
gpointer data ,
GError * * error G_GNUC_UNUSED )
{
font_options_t * font_opts = ( font_options_t * ) data ;
if ( 0 = = strcmp ( arg , " upem " ) )
{
font_opts - > font_size_y = font_opts - > font_size_x = FONT_SIZE_UPEM ;
return true ;
}
2015-04-10 00:04:42 +02:00
switch ( sscanf ( arg , " %lf%*[ ,]%lf " , & font_opts - > font_size_x , & font_opts - > font_size_y ) ) {
2018-09-30 11:49:08 +02:00
case 1 : font_opts - > font_size_y = font_opts - > font_size_x ; HB_FALLTHROUGH ;
2015-01-20 21:30:45 +01:00
case 2 : return true ;
default :
g_set_error ( error , G_OPTION_ERROR , G_OPTION_ERROR_BAD_VALUE ,
2017-11-21 04:39:47 +01:00
" %s argument should be one or two space-separated numbers " ,
2015-01-20 21:30:45 +01:00
name ) ;
return false ;
}
}
2018-02-23 00:24:12 +01:00
static gboolean
parse_font_ppem ( const char * name G_GNUC_UNUSED ,
const char * arg ,
gpointer data ,
GError * * error G_GNUC_UNUSED )
{
font_options_t * font_opts = ( font_options_t * ) data ;
switch ( sscanf ( arg , " %d%*[ ,]%d " , & font_opts - > x_ppem , & font_opts - > y_ppem ) ) {
2018-09-30 11:49:08 +02:00
case 1 : font_opts - > y_ppem = font_opts - > x_ppem ; HB_FALLTHROUGH ;
2018-02-23 00:24:12 +01:00
case 2 : return true ;
default :
g_set_error ( error , G_OPTION_ERROR , G_OPTION_ERROR_BAD_VALUE ,
" %s argument should be one or two space-separated numbers " ,
name ) ;
return false ;
}
}
2021-08-07 02:09:31 +02:00
void
face_options_t : : add_options ( option_parser_t * parser )
{
GOptionEntry entries [ ] =
{
{ " font-file " , 0 , 0 , G_OPTION_ARG_STRING , & this - > font_file , " Set font file-name " , " filename " } ,
{ " face-index " , 0 , 0 , G_OPTION_ARG_INT , & this - > face_index , " Set face index (default: 0) " , " index " } ,
{ nullptr }
} ;
parser - > add_group ( entries ,
" face " ,
" Font-face options: " ,
" Options for the font face " ,
this ) ;
}
2011-09-08 22:00:04 +02:00
void
2011-09-13 19:30:39 +02:00
font_options_t : : add_options ( option_parser_t * parser )
2011-09-08 22:00:04 +02:00
{
2021-08-07 02:09:31 +02:00
face_options_t : : add_options ( parser ) ;
2017-10-15 12:11:08 +02:00
char * text = nullptr ;
2014-07-05 21:50:18 +02:00
{
2018-09-16 19:33:48 +02:00
static_assert ( ( ARRAY_LENGTH_CONST ( supported_font_funcs ) > 0 ) ,
" No supported font-funcs found. " ) ;
2017-10-15 12:11:08 +02:00
GString * s = g_string_new ( nullptr ) ;
2014-07-05 21:50:18 +02:00
g_string_printf ( s , " Set font functions implementation to use (default: %s) \n \n Supported font function implementations are: %s " ,
supported_font_funcs [ 0 ] . name ,
supported_font_funcs [ 0 ] . name ) ;
for ( unsigned int i = 1 ; i < ARRAY_LENGTH ( supported_font_funcs ) ; i + + )
{
g_string_append_c ( s , ' / ' ) ;
g_string_append ( s , supported_font_funcs [ i ] . name ) ;
}
text = g_string_free ( s , FALSE ) ;
parser - > free_later ( text ) ;
}
2015-01-20 21:30:45 +01:00
char * font_size_text ;
2021-08-05 21:48:59 +02:00
if ( DEFAULT_FONT_SIZE = = FONT_SIZE_UPEM )
2015-01-20 21:30:45 +01:00
font_size_text = ( char * ) " Font size (default: upem) " ;
else
{
2021-08-05 21:48:59 +02:00
font_size_text = g_strdup_printf ( " Font size (default: %d) " , DEFAULT_FONT_SIZE ) ;
2015-01-20 21:30:45 +01:00
parser - > free_later ( font_size_text ) ;
}
2021-08-07 03:20:47 +02:00
int font_size_flags = DEFAULT_FONT_SIZE = = FONT_SIZE_NONE ? G_OPTION_FLAG_HIDDEN : 0 ;
2011-09-08 22:00:04 +02:00
GOptionEntry entries [ ] =
{
2021-08-07 03:13:21 +02:00
{ " font-size " , 0 , font_size_flags ,
2018-02-23 00:24:12 +01:00
G_OPTION_ARG_CALLBACK , ( gpointer ) & parse_font_size , font_size_text , " 1/2 integers or 'upem' " } ,
2021-08-07 03:13:21 +02:00
{ " font-ppem " , 0 , font_size_flags ,
G_OPTION_ARG_CALLBACK , ( gpointer ) & parse_font_ppem , " Set x,y pixels per EM (default: 0; disabled) " , " 1/2 integers " } ,
{ " font-ptem " , 0 , 0 ,
G_OPTION_ARG_DOUBLE , & this - > ptem , " Set font point-size (default: 0; disabled) " , " point-size " } ,
2018-02-23 00:24:12 +01:00
{ " font-funcs " , 0 , 0 , G_OPTION_ARG_STRING , & this - > font_funcs , text , " impl " } ,
2018-09-11 14:21:15 +02:00
{ " ft-load-flags " , 0 , 0 , G_OPTION_ARG_INT , & this - > ft_load_flags , " Set FreeType load-flags (default: 2) " , " integer " } ,
2017-10-15 12:11:08 +02:00
{ nullptr }
2011-09-08 22:00:04 +02:00
} ;
2011-09-13 19:30:39 +02:00
parser - > add_group ( entries ,
" font " ,
2021-08-07 02:09:31 +02:00
" Font-instance options: " ,
" Options for the font instance " ,
2011-09-13 19:30:39 +02:00
this ) ;
2017-01-22 02:51:41 +01:00
const gchar * variations_help = " Comma-separated list of font variations \n "
" \n "
2017-01-23 04:01:07 +01:00
" Variations are set globally. The format for specifying variation settings \n "
" follows. All valid CSS font-variation-settings values other than 'normal' \n "
2017-01-22 02:51:41 +01:00
" and 'inherited' are also accepted, though, not documented below. \n "
" \n "
2017-01-23 04:01:07 +01:00
" The format is a tag, optionally followed by an equals sign, followed by a \n "
" number. For example: \n "
2017-01-22 02:51:41 +01:00
" \n "
2017-01-23 04:01:07 +01:00
" \" wght=500 \" \n "
" \" slnt=-7.5 \" \n " ;
2017-01-22 02:51:41 +01:00
GOptionEntry entries2 [ ] =
{
{ " variations " , 0 , 0 , G_OPTION_ARG_CALLBACK , ( gpointer ) & parse_variations , variations_help , " list " } ,
2017-10-15 12:11:08 +02:00
{ nullptr }
2017-01-22 02:51:41 +01:00
} ;
parser - > add_group ( entries2 ,
" variations " ,
2018-01-17 18:47:50 +01:00
" Variations options: " ,
2017-01-23 04:01:07 +01:00
" Options for font variations used " ,
2017-01-22 02:51:41 +01:00
this ) ;
2011-09-08 22:00:04 +02:00
}
2011-08-11 11:54:31 +02:00
2011-09-08 22:00:04 +02:00
void
2011-09-13 19:30:39 +02:00
text_options_t : : add_options ( option_parser_t * parser )
2011-09-08 22:00:04 +02:00
{
GOptionEntry entries [ ] =
{
2017-09-02 04:09:54 +02:00
{ " 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 " } ,
2017-09-09 20:21:07 +02:00
{ " unicodes " , ' u ' , 0 , G_OPTION_ARG_CALLBACK , ( gpointer ) & parse_unicodes , " Set input Unicode codepoints " , " list of hex numbers " } ,
2012-11-14 00:12:24 +01: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 " } ,
2017-10-15 12:11:08 +02:00
{ nullptr }
2011-08-11 11:54:31 +02:00
} ;
2011-09-13 19:30:39 +02:00
parser - > add_group ( entries ,
" text " ,
" Text options: " ,
2017-01-23 04:01:07 +01:00
" Options for the input text " ,
2011-09-13 19:30:39 +02:00
this ) ;
}
void
2021-08-05 19:21:20 +02:00
output_options_t : : add_options ( option_parser_t * parser ,
const char * * supported_formats )
2011-09-13 19:30:39 +02:00
{
2012-12-21 22:46:53 +01:00
const char * text ;
2020-04-20 22:02:34 +02:00
if ( ! supported_formats )
2015-02-14 15:29:35 +01:00
text = " Set output serialization format " ;
2012-12-21 22:46:53 +01:00
else
2014-03-19 23:38:02 +01:00
{
char * items = g_strjoinv ( " / " , const_cast < char * * > ( supported_formats ) ) ;
2014-07-05 00:09:29 +02:00
text = g_strdup_printf ( " Set output format \n \n Supported output formats are: %s " , items ) ;
2014-03-19 23:38:02 +01:00
g_free ( items ) ;
2014-07-05 00:09:29 +02:00
parser - > free_later ( ( char * ) text ) ;
2014-03-19 23:38:02 +01:00
}
2012-12-21 22:46:53 +01:00
2011-09-13 19:30:39 +02:00
GOptionEntry entries [ ] =
{
2017-09-09 20:21:07 +02:00
{ " output-file " , ' o ' , 0 , G_OPTION_ARG_STRING , & this - > output_file , " Set output file-name (default: stdout) " , " filename " } ,
{ " output-format " , ' O ' , 0 , G_OPTION_ARG_STRING , & this - > output_format , text , " format " } ,
2017-10-15 12:11:08 +02:00
{ nullptr }
2011-09-13 19:30:39 +02:00
} ;
parser - > add_group ( entries ,
" output " ,
2015-02-14 15:59:44 +01:00
" Output destination & format options: " ,
2017-01-23 04:01:07 +01:00
" Options for the destination & form of the output " ,
2011-09-13 19:30:39 +02:00
this ) ;
}
2011-08-11 11:54:31 +02:00
2021-08-07 02:09:31 +02:00
face_options_t : : cache_t face_options_t : : cache { } ;
2011-08-11 11:54:31 +02:00
2021-08-07 02:09:31 +02:00
hb_blob_t *
face_options_t : : get_blob ( ) const
2011-09-13 19:30:39 +02:00
{
2021-08-07 02:09:31 +02:00
// XXX This does the job for now; will move to post_parse.
return cache . blob ;
}
hb_face_t *
face_options_t : : get_face ( ) const
{
if ( face )
return face ;
2011-09-13 19:30:39 +02:00
2018-04-20 07:59:06 +02:00
if ( ! font_file )
fail ( true , " No font file set " ) ;
2018-06-24 20:39:16 +02:00
const char * font_path = font_file ;
if ( 0 = = strcmp ( font_path , " - " ) )
{
2013-02-12 21:35:32 +01:00
# if defined(_WIN32) || defined(__CYGWIN__)
2018-04-20 07:59:06 +02:00
setmode ( fileno ( stdin ) , O_BINARY ) ;
2018-06-24 20:39:16 +02:00
font_path = " STDIN " ;
# else
font_path = " /dev/stdin " ;
2011-09-16 06:38:19 +02:00
# endif
2011-09-13 19:30:39 +02:00
}
2021-08-07 02:09:31 +02:00
if ( ! cache . font_path | | 0 ! = strcmp ( cache . font_path , font_path ) )
2021-07-29 03:05:12 +02:00
{
2021-08-07 02:09:31 +02:00
hb_blob_destroy ( cache . blob ) ;
cache . blob = hb_blob_create_from_file_or_fail ( font_path ) ;
2021-07-29 03:05:12 +02:00
2021-08-07 02:09:31 +02:00
free ( ( char * ) cache . font_path ) ;
cache . font_path = strdup ( font_path ) ;
2021-07-29 03:05:12 +02:00
2021-08-07 02:09:31 +02:00
if ( ! cache . blob )
fail ( false , " %s: Failed reading file " , font_path ) ;
2021-07-29 03:05:12 +02:00
2021-07-29 08:09:24 +02:00
hb_face_destroy ( cache . face ) ;
cache . face = nullptr ;
cache . face_index = ( unsigned ) - 1 ;
2021-07-29 03:05:12 +02:00
}
2018-06-17 14:34:55 +02:00
2021-08-07 02:09:31 +02:00
if ( cache . face_index ! = face_index )
2021-07-29 03:05:12 +02:00
{
2021-07-29 08:09:24 +02:00
hb_face_destroy ( cache . face ) ;
2021-08-07 02:09:31 +02:00
cache . face = hb_face_create ( cache . blob , face_index ) ;
cache . face_index = face_index ;
2021-07-29 03:05:12 +02:00
}
2011-09-13 19:30:39 +02:00
2021-08-07 02:09:31 +02:00
face = cache . face ;
return face ;
}
hb_font_t *
font_options_t : : get_font ( ) const
{
if ( font )
return font ;
auto * face = get_face ( ) ;
2011-09-13 19:30:39 +02:00
font = hb_font_create ( face ) ;
2015-01-20 21:30:45 +01:00
if ( font_size_x = = FONT_SIZE_UPEM )
font_size_x = hb_face_get_upem ( face ) ;
if ( font_size_y = = FONT_SIZE_UPEM )
font_size_y = hb_face_get_upem ( face ) ;
2018-02-23 00:24:12 +01:00
hb_font_set_ppem ( font , x_ppem , y_ppem ) ;
hb_font_set_ptem ( font , ptem ) ;
2015-01-20 21:30:45 +01:00
int scale_x = ( int ) scalbnf ( font_size_x , subpixel_bits ) ;
int scale_y = ( int ) scalbnf ( font_size_y , subpixel_bits ) ;
hb_font_set_scale ( font , scale_x , scale_y ) ;
2011-09-13 19:30:39 +02:00
2017-01-22 02:51:41 +01:00
hb_font_set_variations ( font , variations , num_variations ) ;
2017-10-15 12:11:08 +02:00
void ( * set_font_funcs ) ( hb_font_t * ) = nullptr ;
2014-07-05 21:50:18 +02:00
if ( ! font_funcs )
{
set_font_funcs = supported_font_funcs [ 0 ] . func ;
}
else
{
for ( unsigned int i = 0 ; i < ARRAY_LENGTH ( supported_font_funcs ) ; i + + )
2015-11-02 09:55:29 +01:00
if ( 0 = = g_ascii_strcasecmp ( font_funcs , supported_font_funcs [ i ] . name ) )
2014-07-05 21:50:18 +02:00
{
set_font_funcs = supported_font_funcs [ i ] . func ;
break ;
}
if ( ! set_font_funcs )
{
2017-10-15 12:11:08 +02:00
GString * s = g_string_new ( nullptr ) ;
2014-07-05 21:50:18 +02:00
for ( unsigned int i = 0 ; i < ARRAY_LENGTH ( supported_font_funcs ) ; i + + )
{
2019-08-24 15:27:14 +02:00
if ( i )
2014-07-05 21:50:18 +02:00
g_string_append_c ( s , ' / ' ) ;
g_string_append ( s , supported_font_funcs [ i ] . name ) ;
}
char * p = g_string_free ( s , FALSE ) ;
fail ( false , " Unknown font function implementation `%s'; supported values are: %s; default is %s " ,
font_funcs ,
p ,
supported_font_funcs [ 0 ] . name ) ;
//free (p);
}
}
set_font_funcs ( font ) ;
2018-09-11 14:21:15 +02:00
# ifdef HAVE_FREETYPE
hb_ft_font_set_load_flags ( font , ft_load_flags ) ;
# endif
2011-09-13 19:30:39 +02:00
return font ;
}
const char *
2021-08-01 15:59:25 +02:00
text_options_t : : get_line ( unsigned int * len , int eol )
2011-09-13 19:30:39 +02:00
{
2011-09-16 08:08:36 +02:00
if ( text ) {
2018-11-06 17:03:34 +01:00
if ( ! line )
{
line = text ;
2018-11-06 16:49:19 +01:00
line_len = text_len ;
2018-11-06 17:03:34 +01:00
}
2019-12-15 15:50:01 +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 ;
2021-07-28 16:13:49 +02:00
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 ) {
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 ) ;
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 ;
}
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
}
2011-09-16 07:16:41 +02:00
FILE *
2018-12-17 19:01:01 +01:00
output_options_t : : get_file_handle ( )
2011-09-16 07:16:41 +02:00
{
if ( fp )
return fp ;
if ( output_file )
fp = fopen ( output_file , " wb " ) ;
else {
2013-02-12 21:35:32 +01:00
# if defined(_WIN32) || defined(__CYGWIN__)
2015-12-17 17:28:38 +01:00
setmode ( fileno ( stdout ) , O_BINARY ) ;
2011-09-16 07:16:41 +02:00
# endif
fp = stdout ;
}
if ( ! fp )
2012-06-06 02:35:40 +02:00
fail ( false , " Cannot open output file `%s': %s " ,
2011-09-16 07:16:41 +02:00
g_filename_display_name ( output_file ) , strerror ( errno ) ) ;
return fp ;
}