comparison src/utils.cc @ 4302:ebc2d8e4968b

[project @ 2003-01-22 22:02:23 by jwe]
author jwe
date Wed, 22 Jan 2003 22:02:23 +0000
parents 4e2d2516da22
children 1e7f4405e037
comparison
equal deleted inserted replaced
4301:309ef552d7c6 4302:ebc2d8e4968b
41 41
42 // Include setjmp.h, not csetjmp since the latter might only define 42 // Include setjmp.h, not csetjmp since the latter might only define
43 // the ANSI standard C interface. 43 // the ANSI standard C interface.
44 44
45 #include <setjmp.h> 45 #include <setjmp.h>
46
47 #include "quit.h"
46 48
47 #include "dir-ops.h" 49 #include "dir-ops.h"
48 #include "file-ops.h" 50 #include "file-ops.h"
49 #include "file-stat.h" 51 #include "file-stat.h"
50 #include "lo-sstream.h" 52 #include "lo-sstream.h"
856 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY 858 #if defined (__GNUG__) && !CXX_ISO_COMPLIANT_LIBRARY
857 859
858 std::streambuf *sb = os.rdbuf (); 860 std::streambuf *sb = os.rdbuf ();
859 861
860 if (sb) 862 if (sb)
861 retval = sb->vform (fmt, args); 863 {
864 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
865
866 retval = sb->vform (fmt, args);
867
868 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
869 }
862 870
863 #else 871 #else
864 872
865 char *s = octave_vsnprintf (fmt, args); 873 char *s = octave_vsnprintf (fmt, args);
866 874
870 878
871 retval = strlen (s); 879 retval = strlen (s);
872 } 880 }
873 881
874 #endif 882 #endif
883
884 return retval;
885 }
886
887 /* XXX FIXME XXX -- we really need a configure test for this. */
888
889 #if defined __GNUC__ && __GNUC__ >= 3
890 #define HAVE_C99_VSNPRINTF 1
891 #endif
892
893 // We manage storage. User should not free it, and its contents are
894 // only valid until next call to vsnprintf.
895
896 // Interrupts might happen if someone makes a call with something that
897 // will require a very large buffer. If we are interrupted in that
898 // case, we should make the buffer size smaller for the next call.
899
900 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF \
901 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \
902 delete [] buf; \
903 buf = 0; \
904 size = initial_size; \
905 octave_throw_interrupt_exception (); \
906 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2
907
908 char *
909 octave_vsnprintf (const char *fmt, va_list args)
910 {
911 static const size_t initial_size = 100;
912
913 static size_t size = initial_size;
914
915 static char *buf = 0;
916
917 size_t nchars;
918
919 if (! buf)
920 buf = new char [size];
921
922 if (! buf)
923 return 0;
924
925 #if defined (HAVE_C99_VSNPRINTF)
926
927 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF;
928
929 nchars = octave_raw_vsnprintf (buf, size, fmt, args);
930
931 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
932
933 if (nchars >= size)
934 {
935 size = nchars + 1;
936
937 delete [] buf;
938
939 buf = new char [size];
940
941 if (buf)
942 {
943 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF;
944
945 vsnprintf (buf, size, fmt, args);
946
947 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
948 }
949 }
950
951 #else
952
953 while (1)
954 {
955 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF;
956
957 nchars = octave_raw_vsnprintf (buf, size, fmt, args);
958
959 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
960
961 if (nchars > -1)
962 return buf;
963 else
964 {
965 delete [] buf;
966
967 size *= 2;
968
969 buf = new char [size];
970
971 if (! buf)
972 return 0;
973 }
974 }
975
976 #endif
977
978 return buf;
979 }
980
981 char *
982 octave_snprintf (const char *fmt, ...)
983 {
984 char *retval = 0;
985
986 va_list args;
987 va_start (args, fmt);
988
989 retval = octave_vsnprintf (fmt, args);
990
991 va_end (args);
875 992
876 return retval; 993 return retval;
877 } 994 }
878 995
879 void 996 void