Skip to content

Commit

Permalink
Get rid of asprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmiller committed Feb 15, 2018
1 parent f396b09 commit c1754e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 110 deletions.
6 changes: 0 additions & 6 deletions config.h.in
@@ -1,8 +1,5 @@
/* config.h.in. Generated from configure.ac by autoheader. */

/* Define to 1 if you have the `asprintf' function. */
#undef HAVE_ASPRINTF

/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H

Expand All @@ -19,9 +16,6 @@
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H

/* Define to 1 if you have the `pow' function. */
#undef HAVE_POW

/* Define to 1 if your system has a GNU libc compatible `realloc' function,
and to 0 otherwise. */
#undef HAVE_REALLOC
Expand Down
22 changes: 0 additions & 22 deletions configure
Expand Up @@ -17103,17 +17103,6 @@ fi



for ac_func in pow
do :
ac_fn_c_check_func "$LINENO" "pow" "ac_cv_func_pow"
if test "x$ac_cv_func_pow" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_POW 1
_ACEOF

fi
done

for ac_func in strdup
do :
ac_fn_c_check_func "$LINENO" "strdup" "ac_cv_func_strdup"
Expand All @@ -17125,17 +17114,6 @@ _ACEOF
fi
done

for ac_func in asprintf
do :
ac_fn_c_check_func "$LINENO" "asprintf" "ac_cv_func_asprintf"
if test "x$ac_cv_func_asprintf" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_ASPRINTF 1
_ACEOF

fi
done

for ac_header in wchar.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "wchar.h" "ac_cv_header_wchar_h" "$ac_includes_default"
Expand Down
2 changes: 0 additions & 2 deletions configure.ac
Expand Up @@ -37,9 +37,7 @@ AC_PROG_CXX

AC_PATH_PROG(PKG_CONFIG, pkg-config, no)

AC_CHECK_FUNCS([pow])
AC_CHECK_FUNCS([strdup])
AC_CHECK_FUNCS([asprintf])
AC_CHECK_HEADERS([wchar.h])
AC_C_CONST
AC_FUNC_MALLOC
Expand Down
94 changes: 14 additions & 80 deletions src/xlstool.c
Expand Up @@ -117,75 +117,6 @@ static const DWORD colors[] =
0x333333
};

#if HAVE_ASPRINTF != 1

#include <stdarg.h>

#ifdef MSDN
static int asprintf(char **ret, const char *format, ...)
{
int i, size=100;
char *p, *np;

va_list ap;

if ((p = malloc(size)) == NULL)
return -1;

while (1) {
va_start(ap, format);

i = _vsnprintf(p, size, format, ap);

va_end(ap);

if (i > -1 && i < size)
{
i++;
break;
}

if (i > -1) /* glibc 2.1 */
size = i+1; /* precisely what is needed */
else /* glibc 2.0 */
size *= 2; /* twice the old size */

if ((np = realloc(p, size)) == NULL) {
free(p);
return -1;
} else {
p = np;
}
}

*ret = p;
return i > 255 ? 255 : i;
}

#else

static int asprintf(char **ret, const char *format, ...)
{
int i;
char *str;

va_list ap;

va_start(ap, format);

i = vsnprintf(NULL, 0, format, ap) + 1;
str = malloc(i);
i = vsnprintf(str, i, format, ap);

va_end(ap);

*ret = str;
return i > 255 ? 255 : i;
}
#endif

#endif


void dumpbuf(BYTE* fname,long size,BYTE* buf)
{
Expand Down Expand Up @@ -620,6 +551,7 @@ char *xls_getfcell(xlsWorkBook* pWB, struct st_cell_data* cell, WORD *label)
WORD len;
WORD offset;
char *ret = NULL;
size_t retlen = 100;

xf=&pWB->xfs.xf[cell->xf];

Expand All @@ -634,13 +566,13 @@ char *xls_getfcell(xlsWorkBook* pWB, struct st_cell_data* cell, WORD *label)
break;
case XLS_RECORD_BLANK:
case XLS_RECORD_MULBLANK:
asprintf(&ret, "");
ret = strdup("");
break;
case XLS_RECORD_LABEL:
len = xlsShortVal(*label);
label++;
if(pWB->is5ver) {
asprintf(&ret,"%.*s", len, (char *)label);
ret = strndup((char *)label, len);
//printf("Found BIFF5 string of len=%d \"%s\"\n", len, ret);
} else
if ((*(BYTE *)label & 0x01) == 0) {
Expand All @@ -652,38 +584,40 @@ char *xls_getfcell(xlsWorkBook* pWB, struct st_cell_data* cell, WORD *label)
break;
case XLS_RECORD_RK:
case XLS_RECORD_NUMBER:
asprintf(&ret,"%lf", cell->d);
ret = malloc(retlen);
snprintf(ret, retlen, "%lf", cell->d);
break;
// if( RK || MULRK || NUMBER || FORMULA)
// if (cell->id==0x27e || cell->id==0x0BD || cell->id==0x203 || 6 (formula))
default:
ret = malloc(retlen);
switch (xf->format)
{
case 0:
asprintf(&ret,"%d",(int)cell->d);
snprintf(ret, retlen, "%d", (int)cell->d);
break;
case 1:
asprintf(&ret,"%d",(int)cell->d);
snprintf(ret, retlen, "%d", (int)cell->d);
break;
case 2:
asprintf(&ret,"%.1f",cell->d);
snprintf(ret, retlen, "%.1f", cell->d);
break;
case 9:
asprintf(&ret,"%d",(int)cell->d);
snprintf(ret, retlen, "%d", (int)cell->d);
break;
case 10:
asprintf(&ret,"%.2f",cell->d);
snprintf(ret, retlen, "%.2f", cell->d);
break;
case 11:
asprintf(&ret,"%.1e",cell->d);
snprintf(ret, retlen, "%.1e", cell->d);
break;
case 14:
//ret=ctime(cell->d);
asprintf(&ret,"%.0f",cell->d);
snprintf(ret, retlen, "%.0f", cell->d);
break;
default:
// asprintf(&ret,"%.4.2f (%i)",cell->d,xf->format);break;
asprintf(&ret,"%.2f",cell->d);
snprintf(ret, retlen, "%.2f", cell->d);
break;
}
break;
Expand Down

2 comments on commit c1754e9

@jennybc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intent of this to eliminate all asprintf() calls? Several remain in xls.c and I am wondering how to adapt in readxl.

@evanmiller
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the intent is to eliminate asprintf(). I believe the remaining asprintf() calls are surrounded by #ifdef DEBUG_DRAWINGS.

Please sign in to comment.