Commit ee8c50e3968a14ca2979ef110f319b8e5d5be9c1

Authored by Perry Werneck
1 parent 091ee6b8

Refactoring filename builders.

@@ -82,6 +82,8 @@ datarootdir=@datarootdir@ @@ -82,6 +82,8 @@ datarootdir=@datarootdir@
82 localedir=@localedir@ 82 localedir=@localedir@
83 docdir=@docdir@ 83 docdir=@docdir@
84 sysconfdir=@sysconfdir@ 84 sysconfdir=@sysconfdir@
  85 +datadir=$(datarootdir)/$(PRODUCT_NAME)
  86 +confdir=$(sysconfdir)/$(PRODUCT_NAME)
85 87
86 BASEDIR=@BASEDIR@ 88 BASEDIR=@BASEDIR@
87 89
src/core/linux/util.c
@@ -34,14 +34,59 @@ @@ -34,14 +34,59 @@
34 34
35 35
36 #include <config.h> 36 #include <config.h>
  37 +#include <stdarg.h>
37 #include <lib3270-internals.h> 38 #include <lib3270-internals.h>
38 39
39 -char * lib3270_build_data_filename(const char *name) 40 +static char * build_filename(const char *root, const char *str, va_list args)
40 { 41 {
41 - return lib3270_strdup_printf(LIB3270_STRINGIZE_VALUE_OF(DATADIR) "/%s",name); 42 + size_t szFilename = 1024 + strlen(root);
  43 + char * filename = (char *) lib3270_malloc(szFilename);
  44 +
  45 + strcpy(filename,root);
  46 +
  47 + while(str) {
  48 +
  49 + size_t szCurrent = strlen(filename);
  50 +
  51 + if(filename[szCurrent-1] != '/')
  52 + strcat(filename,"/");
  53 +
  54 + szCurrent += strlen(str);
  55 +
  56 + if(szCurrent >= szFilename)
  57 + {
  58 + szFilename += (szCurrent + 1024);
  59 + filename = lib3270_realloc(filename,szFilename);
  60 + }
  61 +
  62 + strcat(filename,str);
  63 +
  64 + str = va_arg(args, const char *);
  65 + }
  66 +
  67 + return (char *) lib3270_realloc(filename,strlen(filename)+1);
42 } 68 }
43 69
44 -char * lib3270_build_config_filename(const char *name) 70 +char * lib3270_build_data_filename(const char *str, ...)
45 { 71 {
46 - return lib3270_strdup_printf(LIB3270_STRINGIZE_VALUE_OF(CONFDIR) "/%s",name); 72 + va_list args;
  73 + va_start (args, str);
  74 +
  75 + char *filename = build_filename(LIB3270_STRINGIZE_VALUE_OF(DATADIR), str, args);
  76 +
  77 + va_end (args);
  78 +
  79 + return filename;
  80 +}
  81 +
  82 +char * lib3270_build_config_filename(const char *str, ...)
  83 +{
  84 + va_list args;
  85 + va_start (args, str);
  86 +
  87 + char *filename = build_filename(LIB3270_STRINGIZE_VALUE_OF(CONFDIR), str, args);
  88 +
  89 + va_end (args);
  90 +
  91 + return filename;
47 } 92 }
src/core/windows/util.c
@@ -249,6 +249,34 @@ int gettimeofday(struct timeval *tv, void GNUC_UNUSED(*ignored)) @@ -249,6 +249,34 @@ int gettimeofday(struct timeval *tv, void GNUC_UNUSED(*ignored))
249 return 0; 249 return 0;
250 } 250 }
251 251
  252 +LIB3270_EXPORT char * lib3270_get_installation_path()
  253 +{
  254 + char lpFilename[4096];
  255 +
  256 + memset(lpFilename,0,sizeof(lpFilename));
  257 + DWORD szPath = GetModuleFileName(hModule,lpFilename,sizeof(lpFilename));
  258 + lpFilename[szPath] = 0;
  259 +
  260 + char * ptr = strrchr(lpFilename,'\\');
  261 + if(ptr)
  262 + ptr[1] = 0;
  263 +
  264 + return strdup(lpFilename);
  265 +}
  266 +
  267 +char * lib3270_get_user_name()
  268 +{
  269 + char username[UNLEN + 1];
  270 + DWORD szName = UNLEN;
  271 +
  272 + memset(username,0,UNLEN + 1);
  273 + GetUserName(username, &szName);
  274 +
  275 + return strdup(username);
  276 +
  277 +}
  278 +
  279 +/*
252 LIB3270_EXPORT char * lib3270_build_data_filename(const char *name) 280 LIB3270_EXPORT char * lib3270_build_data_filename(const char *name)
253 { 281 {
254 // https://github.com/GNOME/glib/blob/master/glib/gwin32.c 282 // https://github.com/GNOME/glib/blob/master/glib/gwin32.c
@@ -274,31 +302,77 @@ char * lib3270_build_config_filename(const char *name) @@ -274,31 +302,77 @@ char * lib3270_build_config_filename(const char *name)
274 // On windows the data and config path are the same. 302 // On windows the data and config path are the same.
275 return lib3270_build_data_filename(name); 303 return lib3270_build_data_filename(name);
276 } 304 }
  305 +*/
277 306
278 -LIB3270_EXPORT char * lib3270_get_installation_path() 307 +static char * build_filename(const char *str, va_list args)
279 { 308 {
280 - char lpFilename[4096]; 309 + size_t szFilename = MAX_PATH;
  310 + char *ptr;
  311 + char * filename = (char *) lib3270_malloc(szFilename);
281 312
282 - memset(lpFilename,0,sizeof(lpFilename));  
283 - DWORD szPath = GetModuleFileName(hModule,lpFilename,sizeof(lpFilename));  
284 - lpFilename[szPath] = 0; 313 + memset(filename,0,szFilename);
285 314
286 - char * ptr = strrchr(lpFilename,'\\'); 315 +#ifdef DEBUG
  316 + filename[0] = '.';
  317 +#else
  318 + DWORD szPath = GetModuleFileName(hModule,filename,szFilename);
  319 + filename[szPath] = 0;
  320 +#endif // DEBUG
  321 +
  322 + ptr = strrchr(filename,'\\');
287 if(ptr) 323 if(ptr)
288 ptr[1] = 0; 324 ptr[1] = 0;
289 325
290 - return strdup(lpFilename); 326 + while(str) {
  327 +
  328 + size_t szCurrent = strlen(filename);
  329 +
  330 + for(ptr=filename;*ptr;ptr++)
  331 + {
  332 + if(*ptr == '/')
  333 + *ptr = '\\';
  334 + }
  335 +
  336 + if(filename[szCurrent-1] != '\\')
  337 + strcat(filename,"\\");
  338 +
  339 + szCurrent += strlen(str);
  340 +
  341 + if(szCurrent >= szFilename)
  342 + {
  343 + szFilename += (szCurrent + 1024);
  344 + filename = lib3270_realloc(filename,szFilename);
  345 + }
  346 +
  347 + strcat(filename,str);
  348 +
  349 + str = va_arg(args, const char *);
  350 + }
  351 +
  352 + return (char *) lib3270_realloc(filename,strlen(filename)+1);
291 } 353 }
292 354
293 -char * lib3270_get_user_name() 355 +char * lib3270_build_data_filename(const char *str, ...)
294 { 356 {
295 - char username[UNLEN + 1];  
296 - DWORD szName = UNLEN; 357 + va_list args;
  358 + va_start (args, str);
297 359
298 - memset(username,0,UNLEN + 1);  
299 - GetUserName(username, &szName); 360 + char *filename = build_filename(str, args);
300 361
301 - return strdup(username); 362 + va_end (args);
  363 +
  364 + return filename;
  365 +}
  366 +
  367 +char * lib3270_build_config_filename(const char *str, ...)
  368 +{
  369 + va_list args;
  370 + va_start (args, str);
  371 +
  372 + char *filename = build_filename(str, args);
  373 +
  374 + va_end (args);
302 375
  376 + return filename;
303 } 377 }
304 378
src/include/lib3270.h
@@ -51,18 +51,22 @@ @@ -51,18 +51,22 @@
51 51
52 #define LIB3270_GNUC_FORMAT(s,f) __attribute__ ((__format__ (__printf__, s, f))) 52 #define LIB3270_GNUC_FORMAT(s,f) __attribute__ ((__format__ (__printf__, s, f)))
53 #define LIB3270_DEPRECATED(func) func __attribute__ ((deprecated)) 53 #define LIB3270_DEPRECATED(func) func __attribute__ ((deprecated))
  54 + #define LIB3270_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
54 55
55 #elif defined(_WIN32) 56 #elif defined(_WIN32)
56 57
57 #define LIB3270_DEPRECATED(func) __declspec(deprecated) func 58 #define LIB3270_DEPRECATED(func) __declspec(deprecated) func
  59 + #define LIB3270_GNUC_NULL_TERMINATED __attribute__((__sentinel__))
58 60
59 #elif defined(__LCLINT__) 61 #elif defined(__LCLINT__)
60 62
61 #define LIB3270_DEPRECATED(func) func 63 #define LIB3270_DEPRECATED(func) func
  64 + #define LIB3270_GNUC_NULL_TERMINATED
62 65
63 #else 66 #else
64 67
65 #define LIB3270_DEPRECATED(func) func 68 #define LIB3270_DEPRECATED(func) func
  69 + #define LIB3270_GNUC_NULL_TERMINATED
66 70
67 #endif 71 #endif
68 72
@@ -1484,9 +1488,9 @@ @@ -1484,9 +1488,9 @@
1484 * @return Full path for the file (release it with lib3270_free). 1488 * @return Full path for the file (release it with lib3270_free).
1485 * 1489 *
1486 */ 1490 */
1487 - LIB3270_EXPORT char * lib3270_build_data_filename(const char *name); 1491 + LIB3270_EXPORT char * lib3270_build_data_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED;
1488 1492
1489 - LIB3270_EXPORT char * lib3270_build_config_filename(const char *name); 1493 + LIB3270_EXPORT char * lib3270_build_config_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED;
1490 1494
1491 LIB3270_EXPORT void lib3270_set_session_id(H3270 *hSession, char id); 1495 LIB3270_EXPORT void lib3270_set_session_id(H3270 *hSession, char id);
1492 LIB3270_EXPORT char lib3270_get_session_id(H3270 *hSession); 1496 LIB3270_EXPORT char lib3270_get_session_id(H3270 *hSession);
src/testprogram/testprogram.c
@@ -38,10 +38,9 @@ int main(int argc, char *argv[]) @@ -38,10 +38,9 @@ int main(int argc, char *argv[])
38 }; 38 };
39 // #pragma GCC diagnostic pop 39 // #pragma GCC diagnostic pop
40 40
41 - H3270 * h; 41 + H3270 * h = lib3270_session_new("");
42 int rc = 0; 42 int rc = 0;
43 43
44 - h = lib3270_session_new("");  
45 printf("3270 session %p created\n]",h); 44 printf("3270 session %p created\n]",h);
46 45
47 lib3270_set_url(h,NULL); 46 lib3270_set_url(h,NULL);
@@ -75,6 +74,17 @@ int main(int argc, char *argv[]) @@ -75,6 +74,17 @@ int main(int argc, char *argv[])
75 } 74 }
76 #endif // _WIN32 75 #endif // _WIN32
77 76
  77 + {
  78 + lib3270_autoptr(char) datafile = lib3270_build_data_filename("data","file.txt",NULL);
  79 + printf("Datafile: \"%s\"\n",datafile);
  80 + }
  81 +
  82 + {
  83 + lib3270_autoptr(char) datafile = lib3270_build_config_filename("test","file.conf",NULL);
  84 + printf("Configfile: \"%s\"\n",datafile);
  85 + }
  86 +
  87 + /*
78 if(lib3270_set_url(h,NULL)) 88 if(lib3270_set_url(h,NULL))
79 lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); 89 lib3270_set_url(h,"tn3270://fandezhi.efglobe.com");
80 90
@@ -101,6 +111,8 @@ int main(int argc, char *argv[]) @@ -101,6 +111,8 @@ int main(int argc, char *argv[])
101 111
102 } 112 }
103 113
  114 + */
  115 +
104 lib3270_session_free(h); 116 lib3270_session_free(h);
105 117
106 return 0; 118 return 0;