From: oetiker Date: Fri, 19 Dec 2008 14:26:47 +0000 (+0000) Subject: win32 fixes by Stefan . Ludewig . exitgames . com X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=af32fb738158a020ad4e623b3cb7d505f5f38b67;p=rrdtool-all.git win32 fixes by Stefan . Ludewig . exitgames . com * added portable plbasename since basename is not available on stock win32 * rrd_update was using %d to show a timestamp ... %lli is better since it also will show 64bit time_t correctly. git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.3@1716 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/program/src/Makefile.am b/program/src/Makefile.am index f91ae6cd..74d382f3 100644 --- a/program/src/Makefile.am +++ b/program/src/Makefile.am @@ -106,7 +106,7 @@ pkgconfig_DATA = librrd.pc # strftime is here because we do not usually need it. unices have propper # iso date support EXTRA_DIST= librrd.pc.in strftime.c strftime.h rrd_getopt.c rrd_getopt1.c rrd_getopt.h \ - win32comp.c rrd_thread_safe_nt.c get_ver.awk librrd.sym.in.in + win32comp.c rrd_thread_safe_nt.c get_ver.awk librrd.sym.in.in plbasename.c plbasename.h librrd.sym: librrd.sym.in grep -v LIBC_HAS_GETOPT_LONG librrd.sym.in >$@ diff --git a/program/src/plbasename.c b/program/src/plbasename.c new file mode 100644 index 00000000..dd695bba --- /dev/null +++ b/program/src/plbasename.c @@ -0,0 +1,211 @@ +#ifdef WIN32 +/* + * + * Cross-platform basename/dirname + * + * Copyright 2005 Syd Logan, All Rights Reserved + * + * This code is distributed without warranty. You are free to use this + * code for any purpose, however, if this code is republished or + * redistributed in its original form, as hardcopy or electronically, + * then you must include this copyright notice along with the code. + * + */ + +// minor changes 2008 by Stefan Ludewig stefan.ludewig@exitgames.com for WIN32 version RRDtool + +#include +#include +#include "plbasename.h" +#include +#if defined(TEST) +#include +#endif + +#if defined(__cplusplus) + +extern "C" { + +#endif + +const char * +PL_basename(const char *name) +{ + const char *base; + char *p; + static char *tmp = NULL; + int len; + + if (tmp) { + free(tmp); + tmp = NULL; + } + + if (!name || !strcmp(name, "")) + return ""; + + if (!strcmp(name, "/")) + return "/"; + + len = strlen(name); + if (name[len - 1] == '/') { + // ditch the trailing '/' + p = tmp = (char*)malloc(len); + strncpy(p, name, len - 1); + } else { + p = (char *) name; + } + + for (base = p; *p; p++) + if (*p == '/') + base = p + 1; + + return base; +} + +const char * +PL_dirname(const char *name) +{ + static char *ret = NULL; + int len; + int size = 0; + const char *p; + + if (ret) { + free(ret); + ret = NULL; + } + + if (!name || !strcmp(name, "") || !strstr(name, "/")) + return("."); + + if (!strcmp(name, "/")) + return(name); + + // find the last slash in the string + + len = strlen(name); + p = &name[len - 1]; + + if (*p == '/') p--; // skip the trailing / + + while (p != name && *p != '/') p--; + + size = p - name; + if (size) { + ret = (char*)malloc(size + 1); + memcpy(ret, name, size); + ret[size] = '\0'; + } else if (*p == '/') + return "/"; + else + return ""; + + return (const char *) ret; +} + +#if defined(__cplusplus) + +} + +#endif + +#if defined(TEST) + +int +main(int argc, char *argv[]) +{ +/* run the following tests: + + path dirname basename + "/usr/lib" "/usr" "lib" + "/usr/" "/" "usr" + "usr" "." "usr" + "/" "/" "/" + "." "." "." + ".." "." ".." + NULL "." "" + "" "." "" + "./.." "." ".." + + These results can be verified by running the unix commands + basename(1) and dirname(1). One tweek to the test strategy + used here would be, on darwin and linux, to shell out to + get the expected results vs hardcoding. +*/ + if (!strcmp(PL_basename("/usr/lib"), "lib")) + printf("PL_basename /usr/lib passed\n"); + else + printf("PL_basename /usr/lib failed expected lib\n"); + if (!strcmp(PL_dirname("/usr/lib"), "/usr")) + printf("PL_dirname /usr/lib passed\n"); + else + printf("PL_dirname /usr/lib failed expected /usr\n"); + if (!strcmp(PL_basename("/usr/"), "usr")) + printf("PL_basename /usr/ passed\n"); + else + printf("PL_basename /usr/ failed expected usr\n"); + if (!strcmp(PL_dirname("/usr/"), "/")) + printf("PL_dirname /usr/ passed\n"); + else + printf("PL_dirname /usr/ failed expected /\n"); + if (!strcmp(PL_basename("usr"), "usr")) + printf("PL_basename usr passed\n"); + else + printf("PL_basename usr failed expected usr\n"); + if (!strcmp(PL_dirname("usr"), ".")) + printf("PL_dirname usr passed\n"); + else + printf("PL_dirname usr failed expected .\n"); + if (!strcmp(PL_basename("/"), "/")) + printf("PL_basename / passed\n"); + else + printf("PL_basename / failed expected /\n"); + if (!strcmp(PL_dirname("/"), "/")) + printf("PL_dirname / passed\n"); + else + printf("PL_dirname / failed expected /\n"); + if (!strcmp(PL_basename("."), ".")) + printf("PL_basename . passed\n"); + else + printf("PL_basename . failed\n"); + if (!strcmp(PL_dirname("."), ".")) + printf("PL_dirname . passed\n"); + else + printf("PL_dirname . failed expected .\n"); + if (!strcmp(PL_basename(".."), "..")) + printf("PL_basename .. passed\n"); + else + printf("PL_basename .. failed expected ..\n"); + if (!strcmp(PL_dirname(".."), ".")) + printf("PL_dirname .. passed\n"); + else + printf("PL_dirname .. failed expected .\n"); + if (!strcmp(PL_basename(NULL), "")) + printf("PL_basename NULL passed\n"); + else + printf("PL_basename NULL failed expected \"\"\n"); + if (!strcmp(PL_dirname(NULL), ".")) + printf("PL_dirname NULL passed\n"); + else + printf("PL_dirname NULL failed expected .\n"); + if (!strcmp(PL_basename(""), "")) + printf("PL_basename \"\" passed\n"); + else + printf("PL_basename \"\" failed expected \"\"\n"); + if (!strcmp(PL_dirname(""), ".")) + printf("PL_dirname \"\" passed\n"); + else + printf("PL_dirname \"\" failed expected .\n"); + + if (!strcmp(PL_basename("./.."), "..")) + printf("PL_basename ./.. passed\n"); + else + printf("PL_basename ./.. failed expected ..\n"); + if (!strcmp(PL_dirname("./.."), ".")) + printf("PL_dirname ./.. passed\n"); + else + printf("PL_dirname ./.. failed expected .\n"); +} +#endif +#endif // WIN32 diff --git a/program/src/plbasename.h b/program/src/plbasename.h new file mode 100644 index 00000000..28786a11 --- /dev/null +++ b/program/src/plbasename.h @@ -0,0 +1,45 @@ +#ifdef WIN32 +/* + * + * Cross-platform basename/dirname + * + * Copyright 2005 Syd Logan, All Rights Reserved + * + * This code is distributed without warranty. You are free to use this + * code for any purpose, however, if this code is republished or + * redistributed in its original form, as hardcopy or electronically, + * then you must include this copyright notice along with the code. + * + */ + +// minor changes 2008 by Stefan Ludewig stefan.ludewig@exitgames.com for WIN32 version RRDtool + +#if !defined(__PL_BASENAME_H__) +#define __PL_BASENAME_H__ + +/* + path dirname basename + "/usr/lib" "/usr" "lib" + "/usr/" "/" "usr" + "usr" "." "usr" + "/" "/" "/" + "." "." "." + ".." "." ".." +*/ + +#if defined(__cplusplus) +extern "C" { +#endif + +const char *PL_basename(const char *name); +const char *PL_dirname(const char *name); + +#define basename(name) ((char*)PL_basename(name)) +#define dirname(name) ((char*)PL_dirname(name)) + +#if defined(__cplusplus) +} +#endif + +#endif +#endif // WIN32 diff --git a/program/src/rrd_graph.c b/program/src/rrd_graph.c index 03230776..c4e8bf0d 100644 --- a/program/src/rrd_graph.c +++ b/program/src/rrd_graph.c @@ -9,7 +9,9 @@ #ifdef WIN32 #include "strftime.h" +#include "plbasename.h" #endif + #include "rrd_tool.h" #if defined(WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) diff --git a/program/src/rrd_update.c b/program/src/rrd_update.c index aec00124..fbd25360 100644 --- a/program/src/rrd_update.c +++ b/program/src/rrd_update.c @@ -1943,7 +1943,7 @@ static int write_RRA_row( /* append info to the return hash */ *pcdp_summary = rrd_info_push(*pcdp_summary, sprintf_alloc - ("[%d]RRA[%s][%lu]DS[%s]", rra_time, + ("[%lli]RRA[%s][%lu]DS[%s]", rra_time, rrd->rra_def[rra_idx].cf_nam, rrd->rra_def[rra_idx].pdp_cnt, rrd->ds_def[ds_idx].ds_nam), diff --git a/program/win32/rrdlib.vcproj b/program/win32/rrdlib.vcproj index 00a8b012..dd5947e6 100644 --- a/program/win32/rrdlib.vcproj +++ b/program/win32/rrdlib.vcproj @@ -164,6 +164,10 @@ RelativePath="..\src\hash_32.c" > + + @@ -298,6 +302,10 @@ RelativePath="..\src\fnv.h" > + +