Permalink
Browse files

Use asprintf to create data path

  • Loading branch information...
czchen committed Nov 8, 2012
1 parent 1236d39 commit 1643a84152ca7dedffc44b65b2fcc05d17e071a9
Showing with 51 additions and 14 deletions.
  1. +5 −1 configure.ac
  2. +14 −13 src/chewingutil.c
  3. +4 −0 src/porting_layer/include/plat_path.h
  4. +28 −0 src/porting_layer/src/plat_path.c
View
@@ -58,7 +58,7 @@ AC_LANG(C)
# Checks for library functions.
AC_FUNC_MALLOC
-AC_CHECK_FUNCS([strtok_r])
+AC_CHECK_FUNCS([strtok_r asprintf])
# plat_mmap_posix
AC_FUNC_MMAP
@@ -75,6 +75,9 @@ AC_SUBST(CC_FOR_BUILD)
# Default CFLAGS
AM_CFLAGS="$CFLAGS -Wall"
+AS_IF([test x$ac_cv_func_asprintf == xyes],
+ [AM_CPPFLAGS="$AM_CPPFLAGS -D_GNU_SOURCE"])
+
AX_WITH_CURSES
AM_CONDITIONAL([ENABLE_TEXT_UI], [test x$ax_cv_ncursesw = "xyes"])
@@ -166,6 +169,7 @@ AC_DEFINE(UNDER_POSIX, 1,
esac
AC_SUBST(AM_CFLAGS)
+AC_SUBST(AM_CPPFLAGS)
AC_CONFIG_LINKS([
data/pinyin.tab:data/pinyin.tab
View
@@ -13,6 +13,9 @@
*/
/* This file is encoded in UTF-8 */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
#include <ctype.h>
#include <string.h>
@@ -30,6 +33,12 @@
#include "userphrase-private.h"
#include "private.h"
+#if HAVE_ASPRINTF
+#include <stdio.h>
+#else
+#include "plat_path.h"
+#endif
+
extern const char *zhuin_tab[];
static void MakePreferInterval( ChewingData *pgdata );
static void ShiftInterval( ChewingOutput *pgo, ChewingData *pgdata );
@@ -1359,15 +1368,11 @@ int InitSymbolTable( ChewingData *pgdata, const char *prefix )
pgdata->static_data.n_symbol_entry = 0;
pgdata->static_data.symbol_table = NULL;
- int filename_len = snprintf( NULL, 0, "%s" PLAT_SEPARATOR "%s",
+ ret = asprintf( &filename, "%s" PLAT_SEPARATOR "%s",
prefix, SYMBOL_TABLE_FILE );
- filename = ALC( char, filename_len + 1 );
- if ( !filename )
+ if ( ret == -1 )
goto end;
- snprintf( filename, filename_len + 1, "%s" PLAT_SEPARATOR "%s",
- prefix, SYMBOL_TABLE_FILE );
-
file = fopen( filename, "r" );
if ( !file )
goto end;
@@ -1467,15 +1472,11 @@ int InitEasySymbolInput( ChewingData *pgdata, const char *prefix )
char *line = NULL;
int ret = -1;
- int filename_len = snprintf( NULL, 0, "%s" PLAT_SEPARATOR "%s",
- prefix, SOFTKBD_TABLE_FILE );
- filename = ALC( char, filename_len + 1 );
- if ( !filename )
+ ret = asprintf( &filename, "%s" PLAT_SEPARATOR "%s",
+ prefix, SOFTKBD_TABLE_FILE );
+ if ( ret == -1 )
goto end;
- snprintf( filename, filename_len + 1, "%s" PLAT_SEPARATOR "%s",
- prefix, SOFTKBD_TABLE_FILE );
-
file = fopen( filename, "r" );
if ( !file )
goto end;
@@ -34,4 +34,8 @@ int find_path_by_files(
char *output,
size_t output_len );
+#if !HAVE_ASPRINTF
+int asprintf( char **strp, const char *fmt, ... );
+#endif
+
#endif // PATH_PRIVATE_H
@@ -13,6 +13,9 @@
#endif
#include "plat_path.h"
+#if !HAVE_ASPRINTF
+#include <stdarg.h>
+#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
@@ -99,6 +102,31 @@ static char * strtok_r (char *s, const char *delim, char **save_ptr)
}
#endif
+#if !HAVE_ASPRINTF
+int asprintf( char **strp, const char *fmt, ... )
+{
+ char *buf;
+ size_t len;
+ va_list ap;
+
+ va_start( ap, fmt );
+ len = vsnprintf( NULL, 0, fmt, ap );
+ va_end( ap );
+
+ buf = (char *) malloc( len + 1 );
+ if ( !buf )
+ return -1;
+
+ va_start( ap, fmt );
+ len = vsnprintf( buf, len + 1, fmt, ap );
+ va_end( ap );
+
+ *strp = buf;
+
+ return len;
+}
+#endif
+
static int are_all_files_readable(
const char *path,
const char * const *files,

0 comments on commit 1643a84

Please sign in to comment.