From ee8c50e3968a14ca2979ef110f319b8e5d5be9c1 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 12 Sep 2019 10:34:26 -0300 Subject: [PATCH] Refactoring filename builders. --- Makefile.in | 2 ++ src/core/linux/util.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- src/core/windows/util.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- src/include/lib3270.h | 8 ++++++-- src/testprogram/testprogram.c | 16 ++++++++++++++-- 5 files changed, 158 insertions(+), 21 deletions(-) diff --git a/Makefile.in b/Makefile.in index 3e94f43..1095bc7 100644 --- a/Makefile.in +++ b/Makefile.in @@ -82,6 +82,8 @@ datarootdir=@datarootdir@ localedir=@localedir@ docdir=@docdir@ sysconfdir=@sysconfdir@ +datadir=$(datarootdir)/$(PRODUCT_NAME) +confdir=$(sysconfdir)/$(PRODUCT_NAME) BASEDIR=@BASEDIR@ diff --git a/src/core/linux/util.c b/src/core/linux/util.c index e6509fd..57f495c 100644 --- a/src/core/linux/util.c +++ b/src/core/linux/util.c @@ -34,14 +34,59 @@ #include +#include #include -char * lib3270_build_data_filename(const char *name) +static char * build_filename(const char *root, const char *str, va_list args) { - return lib3270_strdup_printf(LIB3270_STRINGIZE_VALUE_OF(DATADIR) "/%s",name); + size_t szFilename = 1024 + strlen(root); + char * filename = (char *) lib3270_malloc(szFilename); + + strcpy(filename,root); + + while(str) { + + size_t szCurrent = strlen(filename); + + if(filename[szCurrent-1] != '/') + strcat(filename,"/"); + + szCurrent += strlen(str); + + if(szCurrent >= szFilename) + { + szFilename += (szCurrent + 1024); + filename = lib3270_realloc(filename,szFilename); + } + + strcat(filename,str); + + str = va_arg(args, const char *); + } + + return (char *) lib3270_realloc(filename,strlen(filename)+1); } -char * lib3270_build_config_filename(const char *name) +char * lib3270_build_data_filename(const char *str, ...) { - return lib3270_strdup_printf(LIB3270_STRINGIZE_VALUE_OF(CONFDIR) "/%s",name); + va_list args; + va_start (args, str); + + char *filename = build_filename(LIB3270_STRINGIZE_VALUE_OF(DATADIR), str, args); + + va_end (args); + + return filename; +} + +char * lib3270_build_config_filename(const char *str, ...) +{ + va_list args; + va_start (args, str); + + char *filename = build_filename(LIB3270_STRINGIZE_VALUE_OF(CONFDIR), str, args); + + va_end (args); + + return filename; } diff --git a/src/core/windows/util.c b/src/core/windows/util.c index b09074b..50d053a 100644 --- a/src/core/windows/util.c +++ b/src/core/windows/util.c @@ -249,6 +249,34 @@ int gettimeofday(struct timeval *tv, void GNUC_UNUSED(*ignored)) return 0; } +LIB3270_EXPORT char * lib3270_get_installation_path() +{ + char lpFilename[4096]; + + memset(lpFilename,0,sizeof(lpFilename)); + DWORD szPath = GetModuleFileName(hModule,lpFilename,sizeof(lpFilename)); + lpFilename[szPath] = 0; + + char * ptr = strrchr(lpFilename,'\\'); + if(ptr) + ptr[1] = 0; + + return strdup(lpFilename); +} + +char * lib3270_get_user_name() +{ + char username[UNLEN + 1]; + DWORD szName = UNLEN; + + memset(username,0,UNLEN + 1); + GetUserName(username, &szName); + + return strdup(username); + +} + +/* LIB3270_EXPORT char * lib3270_build_data_filename(const char *name) { // https://github.com/GNOME/glib/blob/master/glib/gwin32.c @@ -274,31 +302,77 @@ char * lib3270_build_config_filename(const char *name) // On windows the data and config path are the same. return lib3270_build_data_filename(name); } +*/ -LIB3270_EXPORT char * lib3270_get_installation_path() +static char * build_filename(const char *str, va_list args) { - char lpFilename[4096]; + size_t szFilename = MAX_PATH; + char *ptr; + char * filename = (char *) lib3270_malloc(szFilename); - memset(lpFilename,0,sizeof(lpFilename)); - DWORD szPath = GetModuleFileName(hModule,lpFilename,sizeof(lpFilename)); - lpFilename[szPath] = 0; + memset(filename,0,szFilename); - char * ptr = strrchr(lpFilename,'\\'); +#ifdef DEBUG + filename[0] = '.'; +#else + DWORD szPath = GetModuleFileName(hModule,filename,szFilename); + filename[szPath] = 0; +#endif // DEBUG + + ptr = strrchr(filename,'\\'); if(ptr) ptr[1] = 0; - return strdup(lpFilename); + while(str) { + + size_t szCurrent = strlen(filename); + + for(ptr=filename;*ptr;ptr++) + { + if(*ptr == '/') + *ptr = '\\'; + } + + if(filename[szCurrent-1] != '\\') + strcat(filename,"\\"); + + szCurrent += strlen(str); + + if(szCurrent >= szFilename) + { + szFilename += (szCurrent + 1024); + filename = lib3270_realloc(filename,szFilename); + } + + strcat(filename,str); + + str = va_arg(args, const char *); + } + + return (char *) lib3270_realloc(filename,strlen(filename)+1); } -char * lib3270_get_user_name() +char * lib3270_build_data_filename(const char *str, ...) { - char username[UNLEN + 1]; - DWORD szName = UNLEN; + va_list args; + va_start (args, str); - memset(username,0,UNLEN + 1); - GetUserName(username, &szName); + char *filename = build_filename(str, args); - return strdup(username); + va_end (args); + + return filename; +} + +char * lib3270_build_config_filename(const char *str, ...) +{ + va_list args; + va_start (args, str); + + char *filename = build_filename(str, args); + + va_end (args); + return filename; } diff --git a/src/include/lib3270.h b/src/include/lib3270.h index 89681f9..a9243b7 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -51,18 +51,22 @@ #define LIB3270_GNUC_FORMAT(s,f) __attribute__ ((__format__ (__printf__, s, f))) #define LIB3270_DEPRECATED(func) func __attribute__ ((deprecated)) + #define LIB3270_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) #elif defined(_WIN32) #define LIB3270_DEPRECATED(func) __declspec(deprecated) func + #define LIB3270_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) #elif defined(__LCLINT__) #define LIB3270_DEPRECATED(func) func + #define LIB3270_GNUC_NULL_TERMINATED #else #define LIB3270_DEPRECATED(func) func + #define LIB3270_GNUC_NULL_TERMINATED #endif @@ -1484,9 +1488,9 @@ * @return Full path for the file (release it with lib3270_free). * */ - LIB3270_EXPORT char * lib3270_build_data_filename(const char *name); + LIB3270_EXPORT char * lib3270_build_data_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED; - LIB3270_EXPORT char * lib3270_build_config_filename(const char *name); + LIB3270_EXPORT char * lib3270_build_config_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED; LIB3270_EXPORT void lib3270_set_session_id(H3270 *hSession, char id); LIB3270_EXPORT char lib3270_get_session_id(H3270 *hSession); diff --git a/src/testprogram/testprogram.c b/src/testprogram/testprogram.c index 24e9869..2f02066 100644 --- a/src/testprogram/testprogram.c +++ b/src/testprogram/testprogram.c @@ -38,10 +38,9 @@ int main(int argc, char *argv[]) }; // #pragma GCC diagnostic pop - H3270 * h; + H3270 * h = lib3270_session_new(""); int rc = 0; - h = lib3270_session_new(""); printf("3270 session %p created\n]",h); lib3270_set_url(h,NULL); @@ -75,6 +74,17 @@ int main(int argc, char *argv[]) } #endif // _WIN32 + { + lib3270_autoptr(char) datafile = lib3270_build_data_filename("data","file.txt",NULL); + printf("Datafile: \"%s\"\n",datafile); + } + + { + lib3270_autoptr(char) datafile = lib3270_build_config_filename("test","file.conf",NULL); + printf("Configfile: \"%s\"\n",datafile); + } + + /* if(lib3270_set_url(h,NULL)) lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); @@ -101,6 +111,8 @@ int main(int argc, char *argv[]) } + */ + lib3270_session_free(h); return 0; -- libgit2 0.21.2