]> granicus.if.org Git - postgis/commitdiff
Code refactoring in rtpg_ utility functions for better memory usage and syntax cleanu...
authorBborie Park <bkpark at ucdavis.edu>
Sat, 10 Dec 2011 01:01:37 +0000 (01:01 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Sat, 10 Dec 2011 01:01:37 +0000 (01:01 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8341 b70326c6-7e19-0410-871a-916f4a2858ee

raster/rt_core/rt_api.c
raster/rt_core/rt_api.h
raster/rt_pg/rt_pg.c

index 8bf707ad439a1e9fffbea80a59b2fadbae2cf09a..891af44653f07d55ccf89998bbff251d1cf22410 100644 (file)
@@ -887,77 +887,119 @@ rt_pixtype_get_min_value(rt_pixtype pixtype) {
 
 /*- rt_band ----------------------------------------------------------*/
 
+/**
+ * Create an in-db rt_band with no data
+ *
+ * @param width     : number of pixel columns
+ * @param height    : number of pixel rows
+ * @param pixtype   : pixel type for the band
+ * @param hasnodata : indicates if the band has nodata value
+ * @param nodataval : the nodata value, will be appropriately
+ *                    truncated to fit the pixtype size.
+ * @param data      : pointer to actual band data, required to
+ *                    be aligned accordingly to
+ *                    rt_pixtype_aligment(pixtype) and big enough
+ *                    to hold raster width*height values.
+ *                    Data will NOT be copied, ownership is left
+ *                    to caller which is responsible to keep it
+ *                    allocated for the whole lifetime of the returned
+ *                    rt_band.
+ *
+ * @return an rt_band, or 0 on failure
+ */
 rt_band
-rt_band_new_inline(uint16_t width, uint16_t height,
-        rt_pixtype pixtype, uint32_t hasnodata, double nodataval,
-        uint8_t* data) {
-    rt_band band = NULL;
-
-
-    assert(NULL != data);
+rt_band_new_inline(
+       uint16_t width, uint16_t height,
+       rt_pixtype pixtype,
+       uint32_t hasnodata, double nodataval,
+       uint8_t* data
+) {
+       rt_band band = NULL;
 
-    band = rtalloc(sizeof (struct rt_band_t));
-    if (!band) {
-        rterror("rt_band_new_inline: Out of memory allocating rt_band");
-        return 0;
-    }
+       assert(NULL != data);
 
-    RASTER_DEBUGF(3, "Created rt_band @ %p with pixtype %s",
-            band, rt_pixtype_name(pixtype));
+       band = rtalloc(sizeof(struct rt_band_t));
+       if (band == NULL) {
+               rterror("rt_band_new_inline: Out of memory allocating rt_band");
+               return NULL;
+       }
 
+       RASTER_DEBUGF(3, "Created rt_band @ %p with pixtype %s",
+               band, rt_pixtype_name(pixtype)
+       );
 
-    band->pixtype = pixtype;
-    band->offline = 0;
-    band->width = width;
-    band->height = height;
-    band->hasnodata = hasnodata;
-    band->nodataval = nodataval;
-    band->data.mem = data;
-    band->ownsData = 0;
-    band->isnodata = FALSE;
+       band->pixtype = pixtype;
+       band->offline = 0;
+       band->width = width;
+       band->height = height;
+       band->hasnodata = hasnodata;
+       band->nodataval = nodataval;
+       band->data.mem = data;
+       band->ownsData = 0;
+       band->isnodata = FALSE;
 
-    return band;
+       return band;
 }
 
+/**
+ * Create an out-db rt_band
+ *
+ * @param width     : number of pixel columns
+ * @param height    : number of pixel rows
+ * @param pixtype   : pixel type for the band
+ * @param nodataval : the nodata value, will be appropriately
+ *                    truncated to fit the pixtype size.
+ * @param bandNum   : 0-based band number in the external file
+ *                    to associate this band with.
+ * @param path      : NULL-terminated path string pointing to the file
+ *                    containing band data. The string will NOT be
+ *                    copied, ownership is left to caller which is
+ *                    responsible to keep it allocated for the whole
+ *                    lifetime of the returned rt_band.
+ *
+ * @return an rt_band, or 0 on failure
+ */
 rt_band
-rt_band_new_offline(uint16_t width, uint16_t height,
-        rt_pixtype pixtype, uint32_t hasnodata, double nodataval,
-        uint8_t bandNum, const char* path) {
-    rt_band band = NULL;
-
-
-    assert(NULL != path);
+rt_band_new_offline(
+       uint16_t width, uint16_t height,
+       rt_pixtype pixtype,
+       uint32_t hasnodata, double nodataval,
+       uint8_t bandNum, const char* path
+) {
+       rt_band band = NULL;
 
-    band = rtalloc(sizeof (struct rt_band_t));
-    if (!band) {
-        rterror("rt_band_new_offline: Out of memory allocating rt_band");
-        return 0;
-    }
+       assert(NULL != path);
 
+       band = rtalloc(sizeof(struct rt_band_t));
+       if (band == NULL) {
+               rterror("rt_band_new_offline: Out of memory allocating rt_band");
+               return NULL;
+       }
 
-    RASTER_DEBUGF(3, "Created rt_band @ %p with pixtype %s",
-            band, rt_pixtype_name(pixtype));
+       RASTER_DEBUGF(3, "Created rt_band @ %p with pixtype %s",
+               band, rt_pixtype_name(pixtype)
+       ); 
 
-    band->pixtype = pixtype;
-    band->offline = 1;
-    band->width = width;
-    band->height = height;
-    band->hasnodata = hasnodata;
-    band->nodataval = nodataval;
-    band->data.offline.bandNum = bandNum;
+       band->pixtype = pixtype;
+       band->offline = 1;
+       band->width = width;
+       band->height = height;
+       band->hasnodata = hasnodata;
+       band->nodataval = nodataval;
+       band->isnodata = FALSE;
 
-    /* memory for data.offline.path should be managed externally */
-    band->data.offline.path = (char *) path;
+       /* XXX QUESTION (jorgearevalo): What does exactly ownsData mean?? I think that
+        * ownsData = 0 ==> the memory for band->data is externally owned
+        * ownsData = 1 ==> the memory for band->data is internally owned
+        */
+       band->ownsData = 0;
 
-    /* XXX QUESTION (jorgearevalo): What does exactly ownsData mean?? I think that
-     * ownsData = 0 ==> the memory for band->data is externally owned
-     * ownsData = 1 ==> the memory for band->data is internally owned
-     */
-    band->ownsData = 0;
+       band->data.offline.bandNum = bandNum;
 
-    band->isnodata = FALSE;
+       /* memory for data.offline.path should be managed externally */
+       band->data.offline.path = (char *) path;
 
-    return band;
+       return band;
 }
 
 int
@@ -969,6 +1011,11 @@ rt_band_is_offline(rt_band band) {
     return band->offline;
 }
 
+/**
+ * Destroy a raster band
+ *
+ * @param band : the band to destroy
+ */
 void
 rt_band_destroy(rt_band band) {
 
@@ -3977,6 +4024,16 @@ rt_raster_get_band(rt_raster raster, int n) {
     return raster->bands[n];
 }
 
+/**
+ * Add band data to a raster.
+ *
+ * @param raster : the raster to add a band to
+ * @param band : the band to add, ownership left to caller.
+ *               Band dimensions are required to match with raster ones.
+ * @param index : the position where to insert the new band (0 based)
+ *
+ * @return identifier (position) for the just-added raster, or -1 on error
+ */
 int32_t
 rt_raster_add_band(rt_raster raster, rt_band band, int index) {
     rt_band *oldbands = NULL;
@@ -4039,7 +4096,18 @@ rt_raster_add_band(rt_raster raster, rt_band band, int index) {
     return index;
 }
 
-
+/**
+ * Generate a new inline band and add it to a raster.
+ *
+ * @param raster : the raster to add a band to
+ * @param pixtype: the pixel type for the new band
+ * @param initialvalue: initial value for pixels
+ * @param hasnodata: indicates if the band has a nodata value
+ * @param nodatavalue: nodata value for the new band
+ * @param index: position to add the new band in the raster
+ *
+ * @return identifier (position) for the just-added raster, or -1 on error
+ */
 int32_t
 rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype,
         double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
index 37eed27fc76e8aa5015a50039580d4a66d2a474c..89621dd7dc719c235d4f3ca3014f0bbd5ab5f832 100644 (file)
@@ -298,7 +298,7 @@ double rt_pixtype_get_min_value(rt_pixtype pixtype);
 /*- rt_band ----------------------------------------------------------*/
 
 /**
- * Create an in-buffer rt_band with no data
+ * Create an in-db rt_band with no data
  *
  * @param width     : number of pixel columns
  * @param height    : number of pixel rows
@@ -317,12 +317,15 @@ double rt_pixtype_get_min_value(rt_pixtype pixtype);
  *
  * @return an rt_band, or 0 on failure
  */
-rt_band rt_band_new_inline(uint16_t width, uint16_t height,
-                           rt_pixtype pixtype, uint32_t hasnodata,
-                           double nodataval, uint8_t* data);
+rt_band rt_band_new_inline(
+       uint16_t width, uint16_t height,
+       rt_pixtype pixtype,
+       uint32_t hasnodata, double nodataval,
+       uint8_t* data
+);
 
 /**
- * Create an on-disk rt_band
+ * Create an out-db rt_band
  *
  * @param width     : number of pixel columns
  * @param height    : number of pixel rows
@@ -339,9 +342,12 @@ rt_band rt_band_new_inline(uint16_t width, uint16_t height,
  *
  * @return an rt_band, or 0 on failure
  */
-rt_band rt_band_new_offline(uint16_t width, uint16_t height,
-                            rt_pixtype pixtype, uint32_t hasnodata,
-                            double nodataval, uint8_t bandNum, const char* path);
+rt_band rt_band_new_offline(
+       uint16_t width, uint16_t height,
+       rt_pixtype pixtype,
+       uint32_t hasnodata, double nodataval,
+       uint8_t bandNum, const char* path
+);
 
 /**
  * Return non-zero if the given band data is on
@@ -381,12 +387,18 @@ uint16_t rt_band_get_height(rt_band band);
  */
 void* rt_band_get_data(rt_band band);
 
-/* Destroy a raster band */
+/**
+ * Destroy a raster band
+ *
+ * @param band : the band to destroy
+ */
 void rt_band_destroy(rt_band band);
 
 /**
  * Get hasnodata flag value
+ *
  * @param band : the band on which to check the hasnodata flag
+ *
  * @return the hasnodata flag.
  */
 int rt_band_get_hasnodata_flag(rt_band band);
@@ -724,7 +736,7 @@ uint16_t rt_raster_get_height(rt_raster raster);
 int32_t rt_raster_add_band(rt_raster raster, rt_band band, int index);
 
 /**
- * Generate a new band data and add it to a raster.
+ * Generate a new inline band and add it to a raster.
  *
  * @param raster : the raster to add a band to
  * @param pixtype: the pixel type for the new band
index 88eb7e1f26cc6f7c689a7136b38c5b1a3d8eec03..d1a30b373a019848d2318efc78ffbc0707281b7b 100644 (file)
@@ -71,16 +71,16 @@ PG_MODULE_MAGIC;
  * functions.
  ****************************************************************/
 /* Internal funcs */
-static char *rtpg_replace(
+static char *rtpg_strreplace(
        const char *str,
        const char *oldstr, const char *newstr,
        int *count
 );
 static char *rtpg_strtoupper(char *str);
-static char    *rtpg_chartrim(char* input, char *remove); /* for RASTER_reclass */
-static char **rtpg_strsplit(const char *str, const char *delimiter, int *n); /* for RASTER_reclass */
-static char *rtpg_removespaces(char *str); /* for RASTER_reclass */
-static char *rtpg_trim(char* input); /* for RASTER_asGDALRaster */
+static char    *rtpg_chartrim(const char* input, char *remove);
+static char **rtpg_strsplit(const char *str, const char *delimiter, int *n);
+static char *rtpg_removespaces(char *str);
+static char *rtpg_trim(const char* input);
 static char *rtpg_getSRTextSPI(int srid);
 
 /***************************************************************
@@ -275,87 +275,83 @@ Datum RASTER_mapAlgebraFctNgb(PG_FUNCTION_ARGS);
      - Always allocates memory for the result.
 --------------------------------------------------------------------------- */
 static char*
-rtpg_replace(const char *str, const char *oldstr, const char *newstr, int *count)
-{
-   const char *tmp = str;
-   char *result;
-   int   found = 0;
-   int   length, reslen;
-   int   oldlen = strlen(oldstr);
-   int   newlen = strlen(newstr);
-   int   limit = (count != NULL && *count > 0) ? *count : -1;
-
-   tmp = str;
-   while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
-      found++, tmp += oldlen;
-
-   length = strlen(str) + found * (newlen - oldlen);
-
-   if ( (result = (char *)palloc(length+1)) == NULL) {
-      fprintf(stderr, "Not enough memory\n");
-      found = -1;
-   } else {
-      tmp = str;
-      limit = found; /* Countdown */
-      reslen = 0; /* length of current result */
-      /* Replace each old string found with new string  */
-      while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
-         length = (tmp - str); /* Number of chars to keep intouched */
-         strncpy(result + reslen, str, length); /* Original part keeped */
-         strcpy(result + (reslen += length), newstr); /* Insert new string */
-         reslen += newlen;
-         tmp += oldlen;
-         str = tmp;
-      }
-      strcpy(result + reslen, str); /* Copies last part and ending nul char */
-   }
-   if (count != NULL) *count = found;
-   return result;
-}
+rtpg_strreplace(
+       const char *str,
+       const char *oldstr, const char *newstr,
+       int *count
+) {
+       const char *tmp = str;
+       char *result;
+       int found = 0;
+       int length, reslen;
+       int oldlen = strlen(oldstr);
+       int newlen = strlen(newstr);
+       int limit = (count != NULL && *count > 0) ? *count : -1;
+
+       tmp = str;
+       while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
+               found++, tmp += oldlen;
+
+       length = strlen(str) + found * (newlen - oldlen);
+       if ((result = (char *) palloc(length + 1)) == NULL) {
+               fprintf(stderr, "Not enough memory\n");
+               found = -1;
+       }
+       else {
+               tmp = str;
+               limit = found; /* Countdown */
+               reslen = 0; /* length of current result */
 
+               /* Replace each old string found with new string  */
+               while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
+                       length = (tmp - str); /* Number of chars to keep intouched */
+                       strncpy(result + reslen, str, length); /* Original part keeped */
+                       strcpy(result + (reslen += length), newstr); /* Insert new string */
 
-static char *
-rtpg_strtoupper(char * str)
-{
-    int j;
+                       reslen += newlen;
+                       tmp += oldlen;
+                       str = tmp;
+               }
+               strcpy(result + reslen, str); /* Copies last part and ending null char */
+       }
 
-    for(j = 0; j < strlen(str); j++)
-        str[j] = toupper(str[j]);
+       if (count != NULL) *count = found;
+       return result;
+}
 
-    return str;
+static char *
+rtpg_strtoupper(char * str) {
+       int j;
 
+       for (j = strlen(str) - 1; j >= 0; j--)
+               str[j] = toupper(str[j]);
+
+       return str;
 }
 
 static char*
-rtpg_chartrim(char *input, char *remove) {
-       char *start;
-       char *ptr;
+rtpg_chartrim(const char *input, char *remove) {
+       char *rtn = NULL;
+       char *ptr = NULL;
 
-       if (!input) {
-               return NULL;
-       }
-       else if (!*input) {
-               return input;
-       }
-
-       start = (char *) palloc(sizeof(char) * strlen(input) + 1);
-       if (NULL == start) {
-               fprintf(stderr, "Not enough memory\n");
+       if (!input)
                return NULL;
-       }
-       strcpy(start, input);
+       else if (!*input)
+               return (char *) input;
 
        /* trim left */
-       while (strchr(remove, *start) != NULL) {
-               start++;
-       }
+       while (strchr(remove, *input) != NULL)
+               input++;
 
        /* trim right */
-       ptr = start + strlen(start);
+       ptr = ((char *) input) + strlen(input);
        while (strchr(remove, *--ptr) != NULL);
        *(++ptr) = '\0';
 
-       return start;
+       rtn = rtalloc(sizeof(char) * (strlen(input) + 1));
+       strcpy(rtn, input);
+
+       return rtn;
 }
 
 /* split a string based on a delimiter */
@@ -427,46 +423,45 @@ rtpg_strsplit(const char *str, const char *delimiter, int *n) {
 static char *
 rtpg_removespaces(char *str) {
        char *rtn;
+       char *tmp;
 
-       rtn = rtpg_replace(str, " ", "", NULL);
-       rtn = rtpg_replace(rtn, "\n", "", NULL);
-       rtn = rtpg_replace(rtn, "\t", "", NULL);
-       rtn = rtpg_replace(rtn, "\f", "", NULL);
-       rtn = rtpg_replace(rtn, "\r", "", NULL);
+       rtn = rtpg_strreplace(str, " ", "", NULL);
+
+       tmp = rtpg_strreplace(rtn, "\n", "", NULL);
+       pfree(rtn);
+       rtn = rtpg_strreplace(tmp, "\t", "", NULL);
+       pfree(tmp);
+       tmp = rtpg_strreplace(rtn, "\f", "", NULL);
+       pfree(rtn);
+       rtn = rtpg_strreplace(tmp, "\r", "", NULL);
+       pfree(tmp);
 
        return rtn;
 }
 
 static char*
-rtpg_trim(char *input) {
-       char *start;
+rtpg_trim(const char *input) {
+       char *rtn;
        char *ptr;
 
-       if (!input) {
+       if (!input)
                return NULL;
-       }
-       else if (!*input) {
-               return input;
-       }
-
-       start = (char *) palloc(sizeof(char) * strlen(input) + 1);
-       if (NULL == start) {
-               fprintf(stderr, "Not enough memory\n");
-               return NULL;
-       }
-       strcpy(start, input);
+       else if (!*input)
+               return (char *) input;
 
        /* trim left */
-       while (isspace(*start)) {
-               start++;
-       }
+       while (isspace(*input))
+               input++;
 
        /* trim right */
-       ptr = start + strlen(start);
+       ptr = ((char *) input) + strlen(input);
        while (isspace(*--ptr));
        *(++ptr) = '\0';
 
-       return start;
+       rtn = rtalloc(sizeof(char) * (strlen(input) + 1));
+       strcpy(rtn, input);
+
+       return rtn;
 }
 
 static char*
@@ -2872,7 +2867,7 @@ Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
 
                     if (initexpr != NULL) {
                                                                                                count = 0;
-                        newexpr = rtpg_replace(initexpr, "RAST", strnewval, &count);
+                        newexpr = rtpg_strreplace(initexpr, "RAST", strnewval, &count);
 
                         POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebraExpr: (%dx%d), "
                                 "r = %s, newexpr = %s",
@@ -8595,16 +8590,21 @@ Datum RASTER_mapAlgebra2(PG_FUNCTION_ARGS)
                        */
                        for (i = 0; i < spicount; i++) {
                                if (!PG_ARGISNULL(exprpos[i])) {
-                                       expr = rtpg_strtoupper(text_to_cstring(PG_GETARG_TEXT_P(exprpos[i])));
-                                       POSTGIS_RT_DEBUGF(3, "raw expr #%d: %s", i, expr);
+                                       char *tmp = rtpg_strtoupper(text_to_cstring(PG_GETARG_TEXT_P(exprpos[i])));
+                                       POSTGIS_RT_DEBUGF(3, "raw expr #%d: %s", i, tmp);
+
                                        len = 0;
-                                       expr = rtpg_replace(expr, "RAST1", "$1", &len);
+                                       expr = rtpg_strreplace(tmp, "RAST1", "$1", &len);
+                                       pfree(tmp);
                                        if (len) {
                                                argcount[i]++;
                                                argexists[i][0] = 1;
                                        }
+
                                        len = 0;
-                                       expr = rtpg_replace(expr, "RAST2", (argexists[i][0] ? "$2" : "$1"), &len);
+                                       tmp = rtpg_strreplace(expr, "RAST2", (argexists[i][0] ? "$2" : "$1"), &len);
+                                       pfree(expr);
+                                       expr = tmp;
                                        if (len) {
                                                argcount[i]++;
                                                argexists[i][1] = 1;