From f4d749681ae3a3b1c5488beaa2bc878b1c92086c Mon Sep 17 00:00:00 2001 From: oetiker Date: Fri, 19 Dec 2008 14:26:47 +0000 Subject: [PATCH] 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/trunk@1716 a5681a0c-68f1-0310-ab6d-d61299d08faa --- program/src/Makefile.am | 2 +- program/src/plbasename.c | 211 +++++++++++++++++++++++++++++ program/src/plbasename.h | 45 +++++++ program/src/rrd_graph.c | 2 + program/src/rrd_update.c | 2 +- program/win32/rrdlib.vcproj | 258 ++---------------------------------- 6 files changed, 270 insertions(+), 250 deletions(-) create mode 100644 program/src/plbasename.c create mode 100644 program/src/plbasename.h diff --git a/program/src/Makefile.am b/program/src/Makefile.am index 8913e4ad..35ed960e 100644 --- a/program/src/Makefile.am +++ b/program/src/Makefile.am @@ -119,7 +119,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 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 989a2b4e..d1343b98 100644 --- a/program/src/rrd_graph.c +++ b/program/src/rrd_graph.c @@ -10,7 +10,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 72a9905c..fa50929a 100644 --- a/program/src/rrd_update.c +++ b/program/src/rrd_update.c @@ -1967,7 +1967,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 bae4a95f..dd5947e6 100644 --- a/program/win32/rrdlib.vcproj +++ b/program/win32/rrdlib.vcproj @@ -50,6 +50,7 @@ PrecompiledHeaderThrough="precompiled.h" WarningLevel="3" DebugInformationFormat="3" + CompileAs="2" DisableSpecificWarnings="4996" /> - - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + -- 2.30.2