[GRASS-SVN] r62366 - in grass/branches/releasebranch_7_0: . include include/defs lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 23 13:27:57 PDT 2014


Author: annakrat
Date: 2014-10-23 13:27:57 -0700 (Thu, 23 Oct 2014)
New Revision: 62366

Modified:
   grass/branches/releasebranch_7_0/
   grass/branches/releasebranch_7_0/include/defs/gis.h
   grass/branches/releasebranch_7_0/include/gis.h
   grass/branches/releasebranch_7_0/lib/gis/basename.c
Log:
basename: Add basename functions as discussed in #2136 (merge from trunk, r61095, r62364)


Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
   - /grass/trunk:62346,62352,62354,62356
   + /grass/trunk:61095,62346,62352,62354,62356,62364

Modified: grass/branches/releasebranch_7_0/include/defs/gis.h
===================================================================
--- grass/branches/releasebranch_7_0/include/defs/gis.h	2014-10-23 20:26:24 UTC (rev 62365)
+++ grass/branches/releasebranch_7_0/include/defs/gis.h	2014-10-23 20:27:57 UTC (rev 62366)
@@ -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/branches/releasebranch_7_0/include/gis.h
===================================================================
--- grass/branches/releasebranch_7_0/include/gis.h	2014-10-23 20:26:24 UTC (rev 62365)
+++ grass/branches/releasebranch_7_0/include/gis.h	2014-10-23 20:27:57 UTC (rev 62366)
@@ -138,6 +138,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/branches/releasebranch_7_0/lib/gis/basename.c
===================================================================
--- grass/branches/releasebranch_7_0/lib/gis/basename.c	2014-10-23 20:26:24 UTC (rev 62365)
+++ grass/branches/releasebranch_7_0/lib/gis/basename.c	2014-10-23 20:27:57 UTC (rev 62366)
@@ -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