1
1

* add support for a long -> string conversion (useful in RTE stuff)

This commit was SVN r1718.
Этот коммит содержится в:
Brian Barrett 2004-07-14 18:04:31 +00:00
родитель b86076859d
Коммит 312819d8a3
5 изменённых файлов: 127 добавлений и 2 удалений

Просмотреть файл

@ -17,6 +17,7 @@ headers = \
few.h \
if.h \
malloc.h \
numtostr.h \
output.h \
path.h \
sys_info.h \
@ -34,6 +35,7 @@ libutil_la_SOURCES = \
few.c \
if.c \
malloc.c \
numtostr.c \
output.c \
path.c \
proc_info.c \

50
src/util/numtostr.c Обычный файл
Просмотреть файл

@ -0,0 +1,50 @@
/*
* $HEADER$
*/
#include "ompi_config.h"
#include "util/numtostr.h"
#include <stdio.h>
char*
ltostr(long num)
{
/* waste a little bit of space, but always have a big enough buffer */
int buflen = sizeof(long) * 8;
char *buf = NULL;
int ret = 0;
buf = malloc(sizeof(char) * buflen);
if (NULL == buf) return NULL;
ret = snprintf(buf, buflen, "%ld", num);
if (ret < 0) {
free(buf);
return NULL;
}
return buf;
}
char*
dtostr(double num)
{
/* waste a little bit of space, but always have a big enough buffer */
int buflen = sizeof(long) * 8;
char *buf = NULL;
int ret = 0;
buf = malloc(sizeof(char) * buflen);
if (NULL == buf) return NULL;
ret = snprintf(buf, buflen, "%f", num);
if (ret < 0) {
free(buf);
return NULL;
}
return buf;
}

27
src/util/numtostr.h Обычный файл
Просмотреть файл

@ -0,0 +1,27 @@
/*
* $HEADER$
*/
#ifndef OMPI_NUMTOSTR_UTIL
#define OMPI_NUMTOSTR_UTIL
/**
* Convert a long integer to a char* string. The returned buffer is
* allocated by calling malloc() and must be freed by the caller.
*
* @param num (IN) Input number
* @return String containing number (NULL on failure)
*/
char* ltostr(long num);
/**
* Convert a double to a char* string. The returned buffer is allocated
* by calling malloc() and must be freed by the caller.
*
* @param num (IN) Input number
* @return String containing number (NULL on failure)
*/
char* dtostr(double num);
#endif /* OMPI_NUMTOSTR_UTIL */

Просмотреть файл

@ -7,11 +7,26 @@ include $(top_srcdir)/config/Makefile.options
AM_CPPFLAGS = -I$(top_srcdir)/test/support -DOMPI_ENABLE_DEBUG_OVERRIDE=1
noinst_PROGRAMS = \
ompi_numtostr \
ompi_os_path \
ompi_sys_info \
ompi_os_create_dirpath \
ompi_argv
ompi_numtostr_SOURCES = ompi_numtostr.c
ompi_numtostr_LDADD = \
$(top_builddir)/src/class/ompi_object.lo \
$(top_builddir)/src/util/malloc.lo \
$(top_builddir)/src/util/output.lo \
$(top_builddir)/src/util/argv.lo \
$(top_builddir)/src/util/strncpy.lo \
$(top_builddir)/src/threads/mutex.lo \
$(top_builddir)/src/threads/mutex_pthread.lo \
$(top_builddir)/src/threads/mutex_spinlock.lo \
$(top_builddir)/src/threads/mutex_spinwait.lo \
$(top_builddir)/src/util/numtostr.lo
ompi_numtostr_DEPENDENCIES = $(ompi_numtostr_LDADD)
ompi_argv_SOURCES = ompi_argv.c
ompi_argv_LDADD = \
$(top_builddir)/src/class/ompi_object.lo \

31
test/util/ompi_numtostr.c Обычный файл
Просмотреть файл

@ -0,0 +1,31 @@
#include "ompi_config.h"
#include "util/numtostr.h"
#include <string.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
char * tst;
char * expected;
tst = ltostr(10);
expected = malloc(sizeof(long) * 8);
snprintf(expected, sizeof(long) * 8, "%d", 10);
if (strcmp(tst, expected) != 0) {
exit(1);
}
free(tst);
free(expected);
tst = dtostr(5.32);
expected = malloc(sizeof(long) * 8);
snprintf(expected, sizeof(long) * 8, "%f", 5.32);
if (strcmp(tst, expected) != 0) {
exit(1);
}
return 0;
}