1993
|
1 // utils.cc |
1967
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1967
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <climits> |
2926
|
29 #include <cstdlib> |
|
30 #include <cstdio> |
1967
|
31 |
2926
|
32 #include <string> |
|
33 |
|
34 #ifdef HAVE_UNISTD_H |
|
35 #ifdef HAVE_SYS_TYPES_H |
|
36 #include <sys/types.h> |
|
37 #endif |
|
38 #include <unistd.h> |
|
39 #endif |
|
40 |
|
41 #include "lo-error.h" |
1967
|
42 #include "lo-mappers.h" |
|
43 #include "lo-utils.h" |
|
44 |
|
45 // Convert X to the nearest integer value. Should not pass NaN to |
|
46 // this function. |
|
47 |
|
48 int |
|
49 NINT (double x) |
|
50 { |
|
51 if (x > INT_MAX) |
|
52 return INT_MAX; |
|
53 else if (x < INT_MIN) |
|
54 return INT_MIN; |
|
55 else |
|
56 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
57 } |
|
58 |
|
59 double |
|
60 D_NINT (double x) |
|
61 { |
|
62 if (xisinf (x) || xisnan (x)) |
|
63 return x; |
|
64 else |
|
65 return floor (x + 0.5); |
|
66 } |
|
67 |
2926
|
68 // Save a string. |
|
69 |
|
70 char * |
|
71 strsave (const char *s) |
|
72 { |
|
73 if (! s) |
|
74 return 0; |
|
75 |
|
76 int len = strlen (s); |
|
77 char *tmp = new char [len+1]; |
|
78 tmp = strcpy (tmp, s); |
|
79 return tmp; |
|
80 } |
|
81 |
|
82 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
83 // library. |
|
84 |
|
85 // XXX FIXME XXX -- make this do the right thing if we don't have a |
|
86 // SMART_PUTENV. |
|
87 |
|
88 void |
|
89 octave_putenv (const string& name, const string& value) |
|
90 { |
|
91 int new_len = name.length () + value.length () + 2; |
|
92 |
|
93 char *new_item = static_cast<char*> (malloc (new_len)); |
|
94 |
|
95 sprintf (new_item, "%s=%s", name.c_str (), value.c_str ()); |
|
96 |
|
97 // As far as I can see there's no way to distinguish between the |
|
98 // various errors; putenv doesn't have errno values. |
|
99 |
|
100 if (putenv (new_item) < 0) |
|
101 (*current_liboctave_error_handler) ("putenv (%s) failed", new_item); |
|
102 } |
|
103 |
|
104 string |
|
105 octave_fgets (FILE *f) |
|
106 { |
|
107 string retval; |
|
108 |
|
109 int grow_size = 1024; |
|
110 int max_size = grow_size; |
|
111 |
|
112 char *buf = static_cast<char *> (malloc (max_size)); |
|
113 char *bufptr = buf; |
|
114 int len = 0; |
|
115 |
|
116 do |
|
117 { |
|
118 if (fgets (bufptr, grow_size, f)) |
|
119 { |
|
120 len = strlen (bufptr); |
|
121 |
|
122 if (len == grow_size - 1) |
|
123 { |
|
124 int tmp = bufptr - buf + grow_size - 1; |
|
125 grow_size *= 2; |
|
126 max_size += grow_size; |
|
127 buf = static_cast<char *> (realloc (buf, max_size)); |
|
128 bufptr = buf + tmp; |
|
129 |
|
130 if (*(bufptr-1) == '\n') |
|
131 { |
|
132 *bufptr = '\0'; |
|
133 retval = buf; |
|
134 } |
|
135 } |
|
136 else if (bufptr[len-1] != '\n') |
|
137 { |
|
138 bufptr[len++] = '\n'; |
|
139 bufptr[len] = '\0'; |
|
140 retval = buf; |
|
141 } |
|
142 else |
|
143 retval = buf; |
|
144 } |
|
145 else |
|
146 { |
|
147 if (len == 0) |
|
148 { |
|
149 free (buf); |
|
150 |
|
151 buf = 0; |
|
152 } |
|
153 |
|
154 break; |
|
155 } |
|
156 } |
|
157 while (retval.empty ()); |
|
158 |
|
159 if (buf) |
|
160 free (buf); |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
1967
|
165 /* |
|
166 ;;; Local Variables: *** |
|
167 ;;; mode: C++ *** |
|
168 ;;; End: *** |
|
169 */ |