[GRASS-SVN] r67295 - in grass/trunk: include/defs lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Dec 20 23:58:36 PST 2015


Author: mmetz
Date: 2015-12-20 23:58:36 -0800 (Sun, 20 Dec 2015)
New Revision: 67295

Modified:
   grass/trunk/include/defs/gis.h
   grass/trunk/lib/gis/compress.c
   grass/trunk/lib/gis/compress.h
Log:
libgis: improve compressor interface

Modified: grass/trunk/include/defs/gis.h
===================================================================
--- grass/trunk/include/defs/gis.h	2015-12-21 03:38:49 UTC (rev 67294)
+++ grass/trunk/include/defs/gis.h	2015-12-21 07:58:36 UTC (rev 67295)
@@ -271,7 +271,9 @@
 const char *G_find_vector2(const char *, const char *);
 
 /* compress.c */
-int G_get_compressor(char *);
+int G_compressor_number(char *);
+char *G_compressor_name(int);
+int G_check_compressor(int);
 int G_write_compressed(int, unsigned char *, int, int);
 int G_write_unompressed(int, unsigned char *, int);
 int G_read_compressed(int, int, unsigned char *, int, int);

Modified: grass/trunk/lib/gis/compress.c
===================================================================
--- grass/trunk/lib/gis/compress.c	2015-12-21 03:38:49 UTC (rev 67294)
+++ grass/trunk/lib/gis/compress.c	2015-12-21 07:58:36 UTC (rev 67295)
@@ -92,33 +92,49 @@
 #define G_COMPRESSED_NO (unsigned char)'0'
 #define G_COMPRESSED_YES (unsigned char)'1'
 
-int G_get_compressor(char *compressor)
+/* get compressor number
+ * return -1 on error
+ * return number >= 0 for known processor */
+int G_compressor_number(char *name)
 {
-    /* 0: NONE (no compressor)
-     * 1: RLE
-     * 2: ZLIB's DEFLATE (default)
-     * 3: LZ4
-     * 4: BZIP2
-     */
+    int i;
+    
+    if (!name)
+	return -1;
 
-    if (!compressor)
-	return 2;
-    if (G_strncasecmp(compressor, "NONE", 4) == 0)
-	return 0;
-    if (G_strncasecmp(compressor, "RLE", 3) == 0)
-	return 1;
-    if (G_strncasecmp(compressor, "ZLIB", 4) == 0)
-	return 2;
-    if (G_strncasecmp(compressor, "LZ4", 3) == 0)
-	return 3;
-    if (G_strncasecmp(compressor, "BZ", 2) == 0)
-	return 4;
+    for (i = 0; compressor[i].name ; i++) {
+	if (G_strcasecmp(name, compressor[i].name) == 0)
+	    return i;
+    }
 
-    G_warning(_("Unknown compressor <%s>, using default ZLIB compressor"),
-              compressor);
-    return 2;
+    return -1;
 }
 
+/* get compressor name
+ * return NULL on error
+ * return string (name) of known processor */
+char *G_compressor_name(int number)
+{
+    if (number < 0 || number >= n_compressors)
+	return NULL;
+
+    return compressor[number].name;
+}
+
+/* check compressor number
+ * return -1 on error
+ * return 0 known but not available
+ * return 1 known and available */
+int G_check_compressor(int number)
+{
+    if (number < 0 || number >= n_compressors) {
+	G_warning(_("Request for unsupported compressor"));
+	return -1;
+    }
+
+    return compressor[number].available;
+}
+
 int
 G_no_compress(unsigned char *src, int src_sz, unsigned char *dst,
 		int dst_sz)
@@ -171,22 +187,14 @@
  */
 int
 G_compress(unsigned char *src, int src_sz, unsigned char *dst,
-		int dst_sz, int compressor)
+		int dst_sz, int number)
 {
-    if (compressor == 0)
-	return G_no_compress(src, src_sz, dst, dst_sz);
-    if (compressor == 1)
-	return G_rle_compress(src, src_sz, dst, dst_sz);
-    if (compressor == 2)
-	return G_zlib_compress(src, src_sz, dst, dst_sz);
-    if (compressor == 3)
-	return G_lz4_compress(src, src_sz, dst, dst_sz);
-    if (compressor == 4)
-	return G_bz2_compress(src, src_sz, dst, dst_sz);
+    if (number < 0 || number >= n_compressors) {
+	G_fatal_error(_("Request for unsupported compressor"));
+	return -1;
+    }
 
-    G_fatal_error(_("Request for unsupported compressor"));
-    
-    return -1;
+    return compressor[number].compress(src, src_sz, dst, dst_sz);
 }
 
 /* G_*_expand() returns
@@ -195,22 +203,14 @@
  */
 int
 G_expand(unsigned char *src, int src_sz, unsigned char *dst,
-		int dst_sz, int compressor)
+		int dst_sz, int number)
 {
-    if (compressor == 0)
-	return G_no_expand(src, src_sz, dst, dst_sz);
-    if (compressor == 1)
-	return G_rle_expand(src, src_sz, dst, dst_sz);
-    if (compressor == 2)
-	return G_zlib_expand(src, src_sz, dst, dst_sz);
-    if (compressor == 3)
-	return G_lz4_expand(src, src_sz, dst, dst_sz);
-    if (compressor == 4)
-	return G_bz2_expand(src, src_sz, dst, dst_sz);
+    if (number < 0 || number >= n_compressors) {
+	G_fatal_error(_("Request for unsupported compressor"));
+	return -1;
+    }
 
-    G_fatal_error(_("Request for unsupported compressor"));
-    
-    return -1;
+    return compressor[number].expand(src, src_sz, dst, dst_sz);
 }
 
 int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes,

Modified: grass/trunk/lib/gis/compress.h
===================================================================
--- grass/trunk/lib/gis/compress.h	2015-12-21 03:38:49 UTC (rev 67294)
+++ grass/trunk/lib/gis/compress.h	2015-12-21 07:58:36 UTC (rev 67295)
@@ -12,9 +12,19 @@
 /* adding a new compressor:
  * add the corresponding functions G_*compress() and G_*_expand()
  * if needed, add checks to configure.in and include/config.in
- * modify G_get_compressor(), G_compress(), G_expand()
+ * modify compress.h
+ * modify G_compress(), G_expand()
  */
 
+
+/* compress.c : no compression */
+int
+G_no_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz);
+int
+G_no_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz);
+
 /* cmprrle.c : Run Length Encoding (RLE) */
 int
 G_rle_compress(unsigned char *src, int src_sz, unsigned char *dst,
@@ -48,3 +58,39 @@
 	      int dst_sz);
 
 /* add more here */
+
+typedef int compress_fn(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz);
+typedef int expand_fn(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz);
+
+struct compressor_list
+{
+    int available;
+    compress_fn *compress;
+    expand_fn *expand;
+    char *name;
+};
+
+/* DO NOT CHANGE the order
+ * 0: None
+ * 1: RLE
+ * 2: ZLIB
+ * 3: LZ4
+ * 4: BZIP2 */
+ 
+static int n_compressors = 5; 
+
+struct compressor_list compressor[] = {
+    {1, G_no_compress, G_no_expand, "NONE"},
+    {1, G_rle_compress, G_rle_expand, "RLE"},
+    {1, G_zlib_compress, G_zlib_expand, "ZLIB"},
+    {1, G_lz4_compress, G_lz4_expand, "LZ4"},
+#ifdef HAVE_BZLIB_H
+    {1, G_bz2_compress, G_bz2_expand, "BZIP2"},
+#else
+    {0, G_bz2_compress, G_bz2_expand, "BZIP2"},
+#endif
+    {0, NULL, NULL, NULL}
+};
+



More information about the grass-commit mailing list