From 47b964ab20db7a7e69dded4b21570f43b96cfb73 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Sat, 30 Jan 2021 12:23:34 -0300 Subject: [PATCH] Adding utility functions. --- src/core/ft/ft.c | 14 ++------------ src/core/session.c | 10 ++++------ src/core/util.c | 67 +++++++++++++++++++++++++++++++++++++++++++++---------------------- src/include/lib3270.h | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 41 deletions(-) diff --git a/src/core/ft/ft.c b/src/core/ft/ft.c index ff3df09..22eb649 100644 --- a/src/core/ft/ft.c +++ b/src/core/ft/ft.c @@ -258,24 +258,16 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); H3270FT * ftHandle = (H3270FT *) session->ft; FILE * ft_local_file = NULL; int f; -// unsigned long length = 0L; -// trace("%s(%s)",__FUNCTION__,local); if(!lib3270_is_connected(session)) - { return ft_creation_failed(session,ENOTCONN,message,_( "Disconnected from host." )); - } if(ftHandle) - { return ft_creation_failed(session,EBUSY,message,_( "File transfer is already active in this session." )); - } // Check remote file if(!*remote) - { return ft_creation_failed(session,EINVAL,message,_( "The remote file name is invalid." )); - } // Open local file #ifdef _WIN32 @@ -285,9 +277,7 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); #endif // _WIN32 if(!ft_local_file) - { return ft_creation_failed(session,errno,message,strerror(errno)); - } // Set options lib3270_set_dft_buffersize(session, dft); @@ -296,14 +286,15 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); ftHandle = lib3270_malloc(sizeof(H3270FT)+strlen(local)+strlen(remote)+3); ftHandle->host = session; + session->ft = ftHandle; ftHandle->ft_last_cr = 0; + ftHandle->ft_is_cut = 0; ftHandle->ascii_flag = (flags & LIB3270_FT_OPTION_ASCII) ? 1 : 0; ftHandle->cr_flag = (flags & LIB3270_FT_OPTION_CRLF) ? 1 : 0; ftHandle->remap_flag = (flags & LIB3270_FT_OPTION_REMAP) ? 1 : 0; ftHandle->unix_text = (flags & LIB3270_FT_OPTION_UNIX) ? 1 : 0; - ftHandle->ft_is_cut = 0; ftHandle->flags = flags; ftHandle->local_file = ft_local_file; ftHandle->state = LIB3270_FT_STATE_AWAIT_ACK; @@ -331,7 +322,6 @@ static void set_ft_state(H3270FT *session, LIB3270_FT_STATE state); ftHandle->remote = ftHandle->local + strlen(ftHandle->local)+1; strcpy((char *) ftHandle->remote,remote); - session->ft = ftHandle; lib3270_reset_ft_callbacks(session); diff --git a/src/core/session.c b/src/core/session.c index 359e94c..5601889 100644 --- a/src/core/session.c +++ b/src/core/session.c @@ -465,14 +465,12 @@ void check_session_handle(H3270 **hSession) if(*hSession) return; - *hSession = lib3270_get_default_session_handle(); +// *hSession = lib3270_get_default_session_handle(); -#if defined(ANDROID) - __android_log_print(ANDROID_LOG_VERBOSE, PACKAGE_NAME, "%s called with empty session\n", __FUNCTION__); -#elif defined(DEBUG) - lib3270_write_log(*hSession, "lib3270", "%s called with empty session from %s",__FUNCTION__,fname); +#if defined(DEBUG) + lib3270_write_log(NULL, "lib3270", "%s called with empty session from %s",__FUNCTION__,fname); #else - lib3270_write_log(*hSession, "lib3270", "%s called with empty session",__FUNCTION__); + lib3270_write_log(NULL, "lib3270", "%s called with empty session",__FUNCTION__); #endif // ANDROID } diff --git a/src/core/util.c b/src/core/util.c index cb9bb12..ef7f6ae 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -60,7 +60,7 @@ char * lib3270_vsprintf(const char *fmt, va_list args) #if defined(HAVE_VASPRINTF) if(vasprintf(&r, fmt, args) < 0 || !r) - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); + lib3270_write_log(NULL, "lib3270", "Error in vasprintf"); #else @@ -70,7 +70,7 @@ char * lib3270_vsprintf(const char *fmt, va_list args) nc = vsnprintf(buf, sizeof(buf), fmt, args); if(nc < 0) { - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); + lib3270_write_log(NULL, "lib3270", "Error on vsnprintf"); } else if (nc < sizeof(buf)) { @@ -82,7 +82,11 @@ char * lib3270_vsprintf(const char *fmt, va_list args) { r = lib3270_malloc(nc + 1); if(vsnprintf(r, nc, fmt, args) < 0) - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); + { + lib3270_write_log(NULL, "lib3270", "Error on vsnprintf"); + free(r); + return NULL; + } } @@ -176,8 +180,6 @@ LIB3270_EXPORT void lib3270_autoptr_cleanup_LIB3270_POPUP(LIB3270_POPUP **ptr) LIB3270_EXPORT void * lib3270_realloc(void *p, int len) { p = realloc(p, len); - if(!p) - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); return p; } @@ -192,41 +194,62 @@ LIB3270_EXPORT void * lib3270_calloc(int elsize, int nelem, void *ptr) if(ptr) memset(ptr,0,sz); - else - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); return ptr; } LIB3270_EXPORT void * lib3270_malloc(int len) { - char *r; - - r = malloc(len); - if (r == (char *)NULL) - { - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); - return 0; - } + char *r = malloc(len); + if(r) + memset(r,0,len); - memset(r,0,len); return r; } LIB3270_EXPORT void * lib3270_strdup(const char *str) { - char *r; + return strdup(str); +} + +LIB3270_EXPORT char * lib3270_chomp(char *str) +{ - r = strdup(str); - if (r == (char *)NULL) + size_t len = strlen(str); + + while(len--) { - lib3270_popup_dialog(lib3270_get_default_session_handle(),LIB3270_NOTIFY_ERROR,_("Internal error"),"Out of memory",NULL); - return 0; + + if(isspace(str[len])) + { + str[len] = 0; + } else { + break; + } } - return r; + return str; + } +LIB3270_EXPORT char * lib3270_chug(char *str) +{ + + char *start; + + for (start = (char*) str; *start && isspace(*start); start++); + + memmove(str, start, strlen ((char *) start) + 1); + + return str; +} + +LIB3270_EXPORT char * lib3270_strip(char *str) +{ + return lib3270_chomp(lib3270_chug(str)); +} + + LIB3270_EXPORT const char * lib3270_get_version(void) { return PACKAGE_VERSION; diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 81dff9b..ff48cbe 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -1404,11 +1404,57 @@ */ LIB3270_EXPORT void * lib3270_calloc(int elsize, int nelem, void *ptr); + /** + * @brief Get a block of memory, fill it with zeros. + * + * @param len Length of memory block to get. + * + * @return Pointer to new memory block. + * + */ LIB3270_EXPORT void * lib3270_malloc(int len); + LIB3270_EXPORT void * lib3270_realloc(void *p, int len); LIB3270_EXPORT void * lib3270_strdup(const char *str); /** + * @brief Removes trailing white space from a string. + * + * This function doesn't allocate or reallocate any memory; + * it modifies in place. Therefore, it cannot be used + * on statically allocated strings. + * + * Reference: + * + * @see chug() and strip(). + * + * @return pointer to string. + * + */ + LIB3270_EXPORT char * lib3270_chomp(char *str); + + /** + * @brief Remove the leading white space from the string. + * + * Removes leading white space from a string, by moving the rest + * of the characters forward. + * + * This function doesn't allocate or reallocate any memory; + * it modifies the string in place. Therefore, it cannot be used on + * statically allocated strings. + * + * Reference: + * + * @see chomp() and strip(). + * + * @return pointer to string. + * + */ + LIB3270_EXPORT char * lib3270_chug(char *str); + + LIB3270_EXPORT char * lib3270_strip(char *str); + + /** * @brief Release allocated memory. * * @param p Memory block to release (can be NULL) @@ -1425,7 +1471,7 @@ * @return Internal's lib3270 session handle. * */ - LIB3270_EXPORT H3270 * lib3270_get_default_session_handle(void); + LIB3270_EXPORT H3270 * LIB3270_DEPRECATED(lib3270_get_default_session_handle(void)); /** * Get library version. -- libgit2 0.21.2