Mercurial > hg > octave-lyh
changeset 3620:0886bbb236cb
[project @ 2000-03-23 05:17:23 by jwe]
author | jwe |
---|---|
date | Thu, 23 Mar 2000 05:17:25 +0000 |
parents | a7fe24b18fa4 |
children | 7c686802265f |
files | ChangeLog configure.in src/ChangeLog src/cutils.c src/error.cc src/oct-stream.cc src/utils.cc src/utils.h |
diffstat | 8 files changed, 163 insertions(+), 37 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2000-03-22 John W. Eaton <jwe@bevo.che.wisc.edu> + + * configure.in: Check for vsnprintf. + 2000-03-21 John W. Eaton <jwe@bevo.che.wisc.edu> * Makefile.in (liboctave.$(SHLEXT)): Delete target before rebuilding.
--- a/configure.in +++ b/configure.in @@ -21,7 +21,7 @@ ### Software Foundation, 59 Temple Place - Suite 330, Boston, MA ### 02111-1307, USA. -AC_REVISION($Revision: 1.325 $) +AC_REVISION($Revision: 1.326 $) AC_PREREQ(2.9) AC_INIT(src/octave.cc) AC_CONFIG_HEADER(config.h) @@ -786,9 +786,10 @@ gethostname getpgrp getpid getppid getpwent getpwnam getpwuid \ gettimeofday getuid getwd localtime_r lstat memmove mkdir mkfifo \ on_exit pipe poll putenv rename rindex rmdir select setgrent \ - setpwent setvbuf sigaction sigpending sigprocmask sigsuspend stat \ - strcasecmp strdup strerror strftime stricmp strncasecmp strnicmp \ - strptime tempnam umask unlink usleep vfprintf vsprintf waitpid) + setpwent setvbuf sigaction sigpending sigprocmask sigsuspend \ + stat strcasecmp strdup strerror strftime stricmp strncasecmp \ + strnicmp strptime tempnam umask unlink usleep vfprintf vsprintf \ + vsnprintf waitpid) OCTAVE_SMART_PUTENV
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,15 @@ 2000-03-22 John W. Eaton <jwe@bevo.che.wisc.edu> + * oct-stream.cc (do_printf_conv): Use octave_format instead of + ostream::form. Return number of characters written. + (octave_base_stream::do_printf): Return number of characters written. + + * error.cc (verror, vwarning): Use octave_format instead of + ostream::vform. + + * utils.cc (octave_format, octave_vformat): New functions. + * cutils.c (octave_snprintf, octave_vsnprintf): New functions. + * oct-lvalue.h (dummy_val): New static variable. (octave_lvalue::octave_lvalue): Use it to initialize val.
--- a/src/cutils.c +++ b/src/cutils.c @@ -39,6 +39,9 @@ #include <sys/poll.h> #endif +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> #include <string.h> static void @@ -92,6 +95,46 @@ return strncasecmp (s1, s2, n); } +char * +octave_vsnprintf (size_t n, const char *fmt, va_list args) +{ +#if defined (HAVE_VSNPRINTF) + size_t size = 100; + + char *buf = malloc (size); + + while (1) + { + int nchars = vsnprintf (buf, size, fmt, args); + + if (nchars > -1) + return buf; + else + { + size *= 2; + buf = realloc (buf, size); + } + } +#else + return 0; +#endif +} + +char * +octave_snprintf (size_t n, const char *fmt, ...) +{ + char *retval = 0; + + va_list args; + va_start (args, fmt); + + retval = octave_vsnprintf (n, fmt, args); + + va_end (args); + + return retval; +} + /* ;;; Local Variables: *** ;;; mode: C++ ***
--- a/src/error.cc +++ b/src/error.cc @@ -72,8 +72,8 @@ std::cerr << name << ": "; } - octave_diary.vform (fmt, args); - std::cerr.vform (fmt, args); + octave_vformat (octave_diary, fmt, args); + octave_vformat (std::cerr, fmt, args); octave_diary << std::endl; std::cerr << std::endl; @@ -91,9 +91,12 @@ if (to_beep_or_not_to_beep_p) output_buf << "\a"; + if (name) output_buf << name << ": "; - output_buf.vform (fmt, args); + + octave_vformat (output_buf, fmt, args); + output_buf << std::endl << std::ends; char *msg = output_buf.str ();
--- a/src/oct-stream.cc +++ b/src/oct-stream.cc @@ -2100,56 +2100,60 @@ // Ugh again and again. template <class T> -void +int do_printf_conv (std::ostream& os, const char *fmt, int nsa, int sa_1, int sa_2, bool have_arg, T arg) { + int retval = 0; + switch (nsa) { case 2: if (have_arg) - os.form (fmt, sa_1, sa_2, arg); + retval = octave_format (os, fmt, sa_1, sa_2, arg); else - os.form (fmt, sa_1, sa_2); + retval = octave_format (os, fmt, sa_1, sa_2); break; case 1: if (have_arg) - os.form (fmt, sa_1, arg); + retval = octave_format (os, fmt, sa_1, arg); else - os.form (fmt, sa_1); + retval = octave_format (os, fmt, sa_1); break; case 0: if (have_arg) - os.form (fmt, arg); + retval = octave_format (os, fmt, arg); else - os.form (fmt); + retval = octave_format (os, fmt); break; default: ::error ("fprintf: internal error handling format"); break; } + + return retval; } -template void +template int do_printf_conv (std::ostream&, const char*, int, int, int, bool, int); -template void +template int do_printf_conv (std::ostream&, const char*, int, int, int, bool, long); -template void +template int do_printf_conv (std::ostream&, const char*, int, int, int, bool, double); -template void +template int do_printf_conv (std::ostream&, const char*, int, int, int, bool, const char*); int octave_base_stream::do_printf (printf_format_list& fmt_list, const octave_value_list& args) { - int retval = -1; + int retval = 0; std::ostream *osp = output_stream (); @@ -2197,7 +2201,8 @@ const char *fmt = elt->text; if (doing_percent || args == 0) - do_printf_conv (os, fmt, nsa, sa_1, sa_2, false, 0.0); + retval += do_printf_conv (os, fmt, nsa, sa_1, sa_2, + false, 0.0); else { if (elt->type == 's' && val_cache.looking_at_string ()) @@ -2205,8 +2210,8 @@ std::string val = val_cache.string_value (); if (val_cache) - do_printf_conv (os, fmt, nsa, sa_1, sa_2, true, - val.c_str ()); + retval += do_printf_conv (os, fmt, nsa, sa_1, + sa_2, true, val.c_str ()); else break; } @@ -2222,20 +2227,23 @@ case 'X': case 'u': case 'c': { if (elt->modifier == 'l') - do_printf_conv (os, fmt, nsa, sa_1, - sa_2, true, - static_cast<long> (val)); + retval + += do_printf_conv (os, fmt, nsa, + sa_1, sa_2, true, + static_cast<long> (val)); else - do_printf_conv (os, fmt, nsa, sa_1, - sa_2, true, - static_cast<int> (val)); + retval + += do_printf_conv (os, fmt, nsa, + sa_1, sa_2, true, + static_cast<int> (val)); } break; case 'f': case 'e': case 'E': case 'g': case 'G': - do_printf_conv (os, fmt, nsa, sa_1, - sa_2, true, val); + retval + += do_printf_conv (os, fmt, nsa, sa_1, + sa_2, true, val); break; default: @@ -2249,15 +2257,10 @@ } if (val_cache.no_more_values ()) - { - retval = 0; - break; - } + break; } - if (os) - retval += nsa + (doing_percent ? 0 : 1); - else + if (! os) { error ("fprintf: write error"); retval = -1;
--- a/src/utils.cc +++ b/src/utils.cc @@ -674,6 +674,56 @@ check_dimensions (nr, nc, warn_for); // May set error_state. } +extern int +octave_format (ostream& os, const char *fmt, ...) +{ + int retval = -1; + + va_list args; + va_start (args, fmt); + + retval = octave_vformat (os, fmt, args); + + va_end (args); + + return retval; +} + +extern int +octave_vformat (ostream& os, const char *fmt, va_list args) +{ + int retval = -1; + +#if defined (__GNUG__) + + ostrstream buf; + + buf.vform (fmt, args); + + buf << ends; + + char *s = buf.str (); + + retval = strlen (s); + + os << s; + +#else + + char *s = octave_vsnprintf (fmt, args); + + if (s) + { + os << s; + + retval = strlen (s); + } + +#endif + + return retval; +} + static int treat_neg_dim_as_zero (void) {
--- a/src/utils.h +++ b/src/utils.h @@ -23,6 +23,8 @@ #if !defined (octave_utils_h) #define octave_utils_h 1 +#include <cstdarg> + #include <iostream> #include <string> @@ -65,12 +67,22 @@ get_dimensions (const octave_value& a, const char *warn_for, int& nr, int& nc); +extern int +octave_format (ostream& os, const char *fmt, ...); + +extern int +octave_vformat (ostream& os, const char *fmt, va_list args); + extern "C" void octave_usleep (unsigned int useconds); extern "C" int octave_strcasecmp (const char *s1, const char *s2); extern "C" int octave_strncasecmp (const char *s1, const char *s2, size_t n); +extern "C" char *octave_snprintf (size_t n, const char *fmt, ...); + +extern "C" char *octave_vsnprintf (size_t n, const char *fmt, va_list args); + #endif /*