[GRASS-SVN] r61095 - in grass/trunk: include include/defs lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Tue Jul 1 02:17:40 PDT 2014
Author: zarch
Date: 2014-07-01 02:17:40 -0700 (Tue, 01 Jul 2014)
New Revision: 61095
Modified:
grass/trunk/include/defs/gis.h
grass/trunk/include/gis.h
grass/trunk/lib/gis/basename.c
Log:
basename: Add basename functions as discussed in #2136
Modified: grass/trunk/include/defs/gis.h
===================================================================
--- grass/trunk/include/defs/gis.h 2014-07-01 07:11:25 UTC (rev 61094)
+++ grass/trunk/include/defs/gis.h 2014-07-01 09:17:40 UTC (rev 61095)
@@ -135,6 +135,11 @@
/* basename.c */
char *G_basename(char *, const char *);
+size_t G_get_num_decimals(const char *);
+char *G_double_to_basename_format(double, size_t, size_t);
+char *G_get_basename_separator();
+char *G_join_basename_strings(const char**, size_t);
+char *G_generate_basename(const char*, double, size_t, size_t);
/* bres_line.c */
void G_bresenham_line(int, int, int, int, int (*)(int, int));
Modified: grass/trunk/include/gis.h
===================================================================
--- grass/trunk/include/gis.h 2014-07-01 07:11:25 UTC (rev 61094)
+++ grass/trunk/include/gis.h 2014-07-01 09:17:40 UTC (rev 61095)
@@ -137,6 +137,9 @@
#define GPATH_MAX 4096
+/* Basename default separator */
+#define GBASENAME_SEP "__"
+
/* Macros for type size independent integers */
/* Use these for portability to ensure integers are truly 32bit */
/* and are handled in a uniform manner */
Modified: grass/trunk/lib/gis/basename.c
===================================================================
--- grass/trunk/lib/gis/basename.c 2014-07-01 07:11:25 UTC (rev 61094)
+++ grass/trunk/lib/gis/basename.c 2014-07-01 09:17:40 UTC (rev 61095)
@@ -16,8 +16,11 @@
#include <grass/gis.h>
+#include <math.h>
+#include <stdio.h>
#include <ctype.h>
#include <string.h>
+#include <stdlib.h>
/**
@@ -44,3 +47,135 @@
return filename;
}
+
+
+
+/*!
+ * \brief Get number of decimals from a string
+ *
+ * \param str String to analyse
+ *
+ * \return number of decimals
+ */
+size_t G_get_num_decimals(const char *str)
+{
+ int sep = '.';
+ size_t len;
+ char *sep_ptr = strchr(str, sep);
+ if (sep_ptr == NULL)
+ return 0;
+ len = strlen(str);
+ return len - (size_t)(sep_ptr - str) - 1;
+}
+
+/*!
+ * \brief Convert a double to a string substituting the dot with underscore
+ * 12.3456 => '12_3456'
+ *
+ * \param number the double number that will be convert to string
+ * \param ndigits the number of integer digits in the output string
+ * \param ndecimals the number of decimals in the output string
+ *
+ * \return a formatted string
+ */
+char *G_double_to_basename_format(double number,
+ size_t ndigits, size_t ndecimals)
+{
+ double integer, decimal;
+ integer = floor(number);
+ char intfmt[GNAME_MAX] = "%d";
+ char intstr[GNAME_MAX];
+ char decfmt[GNAME_MAX] = "";
+ char decstr[GNAME_MAX] = "";
+ char *result;
+
+ if (ndigits != 0){
+ sprintf(intfmt, "%%0%zud", ndigits);
+ }
+ sprintf(intstr, intfmt, (int)integer);
+
+ if (ndecimals != 0){
+ sprintf(decfmt, "_%%0%zud", ndecimals);
+ decimal = ((number - integer) * pow(10., (double)ndecimals));
+ sprintf(decstr, decfmt, (int)decimal);
+ }
+ result = G_malloc(strlen(intstr) + strlen(decstr) + 1);
+ sprintf(result, "%s%s", intstr, decstr);
+ return result;
+}
+
+
+/*!
+ * \brief Return the environmental basename variable or the default value
+ */
+char *G_get_basename_separator(){
+ char *envvar = "GRASS_BASENAME_SEPARATOR";
+ char *envsep;
+
+ envsep = getenv(envvar);
+ return (envsep != NULL && strlen(envsep) > 0) ? envsep: GBASENAME_SEP;
+}
+
+
+/*!
+ * \brief join an array of strings using the basename separator
+ *
+ * \param strings is an array of strings
+ * \param len is the length of the array
+ *
+ * \return a joined string
+ */
+char *G_join_basename_strings(const char**strings, size_t len)
+{
+ size_t i, length, lensep;
+ char *result;
+ char *separator;
+
+ separator = G_get_basename_separator();
+
+ lensep = strlen(separator);
+ length = lensep * (len - 1) + 1;
+ for (i = 0; i < len; i++){
+ length += strlen(strings[i]);
+ }
+ result = G_malloc(length);
+
+ if (result)
+ {
+ strcpy(result, strings[0]);
+ for (i = 1; i < len; i++){
+ strcat(result, separator);
+ strcat(result, strings[i]);
+ }
+ }
+
+ return result;
+}
+
+
+/*!
+ * \brief Generate the format string
+ *
+ * \param basename String with the basename
+ * \param digits Number of digits number
+ * \param decimals Number of decimal number
+ * \param filler String used to fill, default is 0
+ *
+ * \return Format string
+ */
+char *G_generate_basename(const char *basename, double number,
+ size_t ndigits, size_t ndecimals)
+{
+ char *separator, *numberstr, *result;
+
+ separator = G_get_basename_separator();
+ numberstr = G_double_to_basename_format(number, ndigits, ndecimals);
+
+ result = G_malloc(strlen(basename) + strlen(separator) + strlen(numberstr) + 1);
+
+ if (result)
+ sprintf(result, "%s%s%s", basename, separator, numberstr);
+ return result;
+}
+
+
More information about the grass-commit
mailing list