diff options
author | peterb12 <peterb12@c06c8d41-db1a-0410-9941-cceddc491573> | 2005-07-21 02:34:44 +0000 |
---|---|---|
committer | peterb12 <peterb12@c06c8d41-db1a-0410-9941-cceddc491573> | 2005-07-21 02:34:44 +0000 |
commit | 673bdae75485d14f759af597c3c62b99601f9a43 (patch) | |
tree | 368103f29fe0ce5dcf98060d9b5faa04590085fb /trunk/source/libutil.cc | |
parent | 7e900be770db24b0405fd2162491c405a425873e (diff) | |
download | crawl-ref-673bdae75485d14f759af597c3c62b99601f9a43.tar.gz crawl-ref-673bdae75485d14f759af597c3c62b99601f9a43.zip |
Initial revision
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@3 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'trunk/source/libutil.cc')
-rw-r--r-- | trunk/source/libutil.cc | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/trunk/source/libutil.cc b/trunk/source/libutil.cc new file mode 100644 index 0000000000..9be12392a7 --- /dev/null +++ b/trunk/source/libutil.cc @@ -0,0 +1,93 @@ +/* + * File: libutil.cc + * Summary: Functions that may be missing from some systems + * + * Change History (most recent first): + * + * <1> 2001/Nov/01 BWR Created + * + */ + +#include "AppHdr.h" +#include <stdio.h> +#include <ctype.h> + +void get_input_line( char *const buff, int len ) +{ + buff[0] = '\0'; // just in case + +#if defined(LINUX) + get_input_line_from_curses( buff, len ); // inplemented in liblinux.cc +#elif defined(MAC) || defined(WIN32CONSOLE) + getstr( buff, len ); // implemented in libmac.cc +#else + fgets( buff, len, stdin ); // much safer than gets() +#endif + + buff[ len - 1 ] = '\0'; // just in case + + // Removing white space from the end in order to get rid of any + // newlines or carriage returns that any of the above might have + // left there (ie fgets especially). -- bwr + const int end = strlen( buff ); + int i; + + for (i = end - 1; i >= 0; i++) + { + if (isspace( buff[i] )) + buff[i] = '\0'; + else + break; + } +} + +// The old school way of doing short delays via low level I/O sync. +// Good for systems like old versions of Solaris that don't have usleep. +#ifdef NEED_USLEEP + +#include <sys/time.h> +#include <sys/types.h> +#include <sys/unistd.h> + +void usleep(unsigned long time) +{ + struct timeval timer; + + timer.tv_sec = (time / 1000000L); + timer.tv_usec = (time % 1000000L); + + select(0, NULL, NULL, NULL, &timer); +} +#endif + +// Not the greatest version of snprintf, but a functional one that's +// a bit safer than raw sprintf(). Note that this doesn't do the +// special behaviour for size == 0, largely because the return value +// in that case varies depending on which standard is being used (SUSv2 +// returns an unspecified value < 1, whereas C99 allows str == NULL +// and returns the number of characters that would have been written). -- bwr +#ifdef NEED_SNPRINTF + +#include <stdarg.h> +#include <string.h> + +int snprintf( char *str, size_t size, const char *format, ... ) +{ + va_list argp; + va_start( argp, format ); + + char buff[ 10 * size ]; // hopefully enough + + vsprintf( buff, format, argp ); + strncpy( str, buff, size ); + str[ size - 1 ] = '\0'; + + int ret = strlen( str ); + if ((unsigned int) ret == size - 1 && strlen( buff ) >= size) + ret = -1; + + va_end( argp ); + + return (ret); +} +#endif |