Commit 47b964ab20db7a7e69dded4b21570f43b96cfb73
1 parent
e3de3fcb
Exists in
master
and in
2 other branches
Adding utility functions.
Showing
4 changed files
with
98 additions
and
41 deletions
Show diff stats
src/core/ft/ft.c
... | ... | @@ -258,24 +258,16 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); |
258 | 258 | H3270FT * ftHandle = (H3270FT *) session->ft; |
259 | 259 | FILE * ft_local_file = NULL; |
260 | 260 | int f; |
261 | -// unsigned long length = 0L; | |
262 | 261 | |
263 | -// trace("%s(%s)",__FUNCTION__,local); | |
264 | 262 | if(!lib3270_is_connected(session)) |
265 | - { | |
266 | 263 | return ft_creation_failed(session,ENOTCONN,message,_( "Disconnected from host." )); |
267 | - } | |
268 | 264 | |
269 | 265 | if(ftHandle) |
270 | - { | |
271 | 266 | return ft_creation_failed(session,EBUSY,message,_( "File transfer is already active in this session." )); |
272 | - } | |
273 | 267 | |
274 | 268 | // Check remote file |
275 | 269 | if(!*remote) |
276 | - { | |
277 | 270 | return ft_creation_failed(session,EINVAL,message,_( "The remote file name is invalid." )); |
278 | - } | |
279 | 271 | |
280 | 272 | // Open local file |
281 | 273 | #ifdef _WIN32 |
... | ... | @@ -285,9 +277,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); |
285 | 277 | #endif // _WIN32 |
286 | 278 | |
287 | 279 | if(!ft_local_file) |
288 | - { | |
289 | 280 | return ft_creation_failed(session,errno,message,strerror(errno)); |
290 | - } | |
291 | 281 | |
292 | 282 | // Set options |
293 | 283 | lib3270_set_dft_buffersize(session, dft); |
... | ... | @@ -296,14 +286,15 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); |
296 | 286 | ftHandle = lib3270_malloc(sizeof(H3270FT)+strlen(local)+strlen(remote)+3); |
297 | 287 | |
298 | 288 | ftHandle->host = session; |
289 | + session->ft = ftHandle; | |
299 | 290 | |
300 | 291 | ftHandle->ft_last_cr = 0; |
292 | + ftHandle->ft_is_cut = 0; | |
301 | 293 | |
302 | 294 | ftHandle->ascii_flag = (flags & LIB3270_FT_OPTION_ASCII) ? 1 : 0; |
303 | 295 | ftHandle->cr_flag = (flags & LIB3270_FT_OPTION_CRLF) ? 1 : 0; |
304 | 296 | ftHandle->remap_flag = (flags & LIB3270_FT_OPTION_REMAP) ? 1 : 0; |
305 | 297 | ftHandle->unix_text = (flags & LIB3270_FT_OPTION_UNIX) ? 1 : 0; |
306 | - ftHandle->ft_is_cut = 0; | |
307 | 298 | ftHandle->flags = flags; |
308 | 299 | ftHandle->local_file = ft_local_file; |
309 | 300 | ftHandle->state = LIB3270_FT_STATE_AWAIT_ACK; |
... | ... | @@ -331,7 +322,6 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); |
331 | 322 | ftHandle->remote = ftHandle->local + strlen(ftHandle->local)+1; |
332 | 323 | strcpy((char *) ftHandle->remote,remote); |
333 | 324 | |
334 | - session->ft = ftHandle; | |
335 | 325 | |
336 | 326 | lib3270_reset_ft_callbacks(session); |
337 | 327 | ... | ... |
src/core/session.c
... | ... | @@ -465,14 +465,12 @@ void check_session_handle(H3270 **hSession) |
465 | 465 | if(*hSession) |
466 | 466 | return; |
467 | 467 | |
468 | - *hSession = lib3270_get_default_session_handle(); | |
468 | +// *hSession = lib3270_get_default_session_handle(); | |
469 | 469 | |
470 | -#if defined(ANDROID) | |
471 | - __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "%s called with empty session\n", __FUNCTION__); | |
472 | -#elif defined(DEBUG) | |
473 | - lib3270_write_log(*hSession, "lib3270", "%s called with empty session from %s",__FUNCTION__,fname); | |
470 | +#if defined(DEBUG) | |
471 | + lib3270_write_log(NULL, "lib3270", "%s called with empty session from %s",__FUNCTION__,fname); | |
474 | 472 | #else |
475 | - lib3270_write_log(*hSession, "lib3270", "%s called with empty session",__FUNCTION__); | |
473 | + lib3270_write_log(NULL, "lib3270", "%s called with empty session",__FUNCTION__); | |
476 | 474 | #endif // ANDROID |
477 | 475 | } |
478 | 476 | ... | ... |
src/core/util.c
... | ... | @@ -60,7 +60,7 @@ char * lib3270_vsprintf(const char *fmt, va_list args) |
60 | 60 | #if defined(HAVE_VASPRINTF) |
61 | 61 | |
62 | 62 | if(vasprintf(&r, fmt, args) < 0 || !r) |
63 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
63 | + lib3270_write_log(NULL, "lib3270", "Error in vasprintf"); | |
64 | 64 | |
65 | 65 | #else |
66 | 66 | |
... | ... | @@ -70,7 +70,7 @@ char * lib3270_vsprintf(const char *fmt, va_list args) |
70 | 70 | nc = vsnprintf(buf, sizeof(buf), fmt, args); |
71 | 71 | if(nc < 0) |
72 | 72 | { |
73 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
73 | + lib3270_write_log(NULL, "lib3270", "Error on vsnprintf"); | |
74 | 74 | } |
75 | 75 | else if (nc < sizeof(buf)) |
76 | 76 | { |
... | ... | @@ -82,7 +82,11 @@ char * lib3270_vsprintf(const char *fmt, va_list args) |
82 | 82 | { |
83 | 83 | r = lib3270_malloc(nc + 1); |
84 | 84 | if(vsnprintf(r, nc, fmt, args) < 0) |
85 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
85 | + { | |
86 | + lib3270_write_log(NULL, "lib3270", "Error on vsnprintf"); | |
87 | + free(r); | |
88 | + return NULL; | |
89 | + } | |
86 | 90 | |
87 | 91 | } |
88 | 92 | |
... | ... | @@ -176,8 +180,6 @@ LIB3270_EXPORT void lib3270_autoptr_cleanup_LIB3270_POPUP(LIB3270_POPUP **ptr) |
176 | 180 | LIB3270_EXPORT void * lib3270_realloc(void *p, int len) |
177 | 181 | { |
178 | 182 | p = realloc(p, len); |
179 | - if(!p) | |
180 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
181 | 183 | return p; |
182 | 184 | } |
183 | 185 | |
... | ... | @@ -192,41 +194,62 @@ LIB3270_EXPORT void * lib3270_calloc(int elsize, int nelem, void *ptr) |
192 | 194 | |
193 | 195 | if(ptr) |
194 | 196 | memset(ptr,0,sz); |
195 | - else | |
196 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
197 | 197 | |
198 | 198 | return ptr; |
199 | 199 | } |
200 | 200 | |
201 | 201 | LIB3270_EXPORT void * lib3270_malloc(int len) |
202 | 202 | { |
203 | - char *r; | |
204 | - | |
205 | - r = malloc(len); | |
206 | - if (r == (char *)NULL) | |
207 | - { | |
208 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
209 | - return 0; | |
210 | - } | |
203 | + char *r = malloc(len); | |
204 | + if(r) | |
205 | + memset(r,0,len); | |
211 | 206 | |
212 | - memset(r,0,len); | |
213 | 207 | return r; |
214 | 208 | } |
215 | 209 | |
216 | 210 | LIB3270_EXPORT void * lib3270_strdup(const char *str) |
217 | 211 | { |
218 | - char *r; | |
212 | + return strdup(str); | |
213 | +} | |
214 | + | |
215 | +LIB3270_EXPORT char * lib3270_chomp(char *str) | |
216 | +{ | |
219 | 217 | |
220 | - r = strdup(str); | |
221 | - if (r == (char *)NULL) | |
218 | + size_t len = strlen(str); | |
219 | + | |
220 | + while(len--) | |
222 | 221 | { |
223 | - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); | |
224 | - return 0; | |
222 | + | |
223 | + if(isspace(str[len])) | |
224 | + { | |
225 | + str[len] = 0; | |
226 | + } else { | |
227 | + break; | |
228 | + } | |
225 | 229 | } |
226 | 230 | |
227 | - return r; | |
231 | + return str; | |
232 | + | |
228 | 233 | } |
229 | 234 | |
235 | +LIB3270_EXPORT char * lib3270_chug(char *str) | |
236 | +{ | |
237 | + | |
238 | + char *start; | |
239 | + | |
240 | + for (start = (char*) str; *start && isspace(*start); start++); | |
241 | + | |
242 | + memmove(str, start, strlen ((char *) start) + 1); | |
243 | + | |
244 | + return str; | |
245 | +} | |
246 | + | |
247 | +LIB3270_EXPORT char * lib3270_strip(char *str) | |
248 | +{ | |
249 | + return lib3270_chomp(lib3270_chug(str)); | |
250 | +} | |
251 | + | |
252 | + | |
230 | 253 | LIB3270_EXPORT const char * lib3270_get_version(void) |
231 | 254 | { |
232 | 255 | return PACKAGE_VERSION; | ... | ... |
src/include/lib3270.h
... | ... | @@ -1404,11 +1404,57 @@ |
1404 | 1404 | */ |
1405 | 1405 | LIB3270_EXPORT void * lib3270_calloc(int elsize, int nelem, void *ptr); |
1406 | 1406 | |
1407 | + /** | |
1408 | + * @brief Get a block of memory, fill it with zeros. | |
1409 | + * | |
1410 | + * @param len Length of memory block to get. | |
1411 | + * | |
1412 | + * @return Pointer to new memory block. | |
1413 | + * | |
1414 | + */ | |
1407 | 1415 | LIB3270_EXPORT void * lib3270_malloc(int len); |
1416 | + | |
1408 | 1417 | LIB3270_EXPORT void * lib3270_realloc(void *p, int len); |
1409 | 1418 | LIB3270_EXPORT void * lib3270_strdup(const char *str); |
1410 | 1419 | |
1411 | 1420 | /** |
1421 | + * @brief Removes trailing white space from a string. | |
1422 | + * | |
1423 | + * This function doesn't allocate or reallocate any memory; | |
1424 | + * it modifies in place. Therefore, it cannot be used | |
1425 | + * on statically allocated strings. | |
1426 | + * | |
1427 | + * Reference: <https://git.gnome.org/browse/glib/tree/glib/gstrfuncs.c> | |
1428 | + * | |
1429 | + * @see chug() and strip(). | |
1430 | + * | |
1431 | + * @return pointer to string. | |
1432 | + * | |
1433 | + */ | |
1434 | + LIB3270_EXPORT char * lib3270_chomp(char *str); | |
1435 | + | |
1436 | + /** | |
1437 | + * @brief Remove the leading white space from the string. | |
1438 | + * | |
1439 | + * Removes leading white space from a string, by moving the rest | |
1440 | + * of the characters forward. | |
1441 | + * | |
1442 | + * This function doesn't allocate or reallocate any memory; | |
1443 | + * it modifies the string in place. Therefore, it cannot be used on | |
1444 | + * statically allocated strings. | |
1445 | + * | |
1446 | + * Reference: <https://git.gnome.org/browse/glib/tree/glib/gstrfuncs.c> | |
1447 | + * | |
1448 | + * @see chomp() and strip(). | |
1449 | + * | |
1450 | + * @return pointer to string. | |
1451 | + * | |
1452 | + */ | |
1453 | + LIB3270_EXPORT char * lib3270_chug(char *str); | |
1454 | + | |
1455 | + LIB3270_EXPORT char * lib3270_strip(char *str); | |
1456 | + | |
1457 | + /** | |
1412 | 1458 | * @brief Release allocated memory. |
1413 | 1459 | * |
1414 | 1460 | * @param p Memory block to release (can be NULL) |
... | ... | @@ -1425,7 +1471,7 @@ |
1425 | 1471 | * @return Internal's lib3270 session handle. |
1426 | 1472 | * |
1427 | 1473 | */ |
1428 | - LIB3270_EXPORT H3270 * lib3270_get_default_session_handle(void); | |
1474 | + LIB3270_EXPORT H3270 * LIB3270_DEPRECATED(lib3270_get_default_session_handle(void)); | |
1429 | 1475 | |
1430 | 1476 | /** |
1431 | 1477 | * Get library version. | ... | ... |