[SR-Dev] git:janakj/flatstore: - added record delimiter and escape char params

Jan Janak jan at iptel.org
Sun Feb 15 18:56:45 CET 2009


Module: sip-router
Branch: janakj/flatstore
Commit: fd142b4aa5797786db9261b60dea295c6576e7a0
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=fd142b4aa5797786db9261b60dea295c6576e7a0

Author: Tomas Mandys <tomas.mandys at iptel.org>
Committer: Tomas Mandys <tomas.mandys at iptel.org>
Date:   Fri Jan  6 23:55:14 2006 +0000

- added record delimiter and escape char params
- escaping of delimiters in text
- zero delimiter supported via empty string delimiter param

---

 modules/db_flatstore/flat_con.c      |   14 ++--
 modules/db_flatstore/flatstore.c     |  106 ++++++++++++++++++++++------------
 modules/db_flatstore/flatstore_mod.c |   39 ++++++++++---
 modules/db_flatstore/flatstore_mod.h |   21 +++++--
 4 files changed, 120 insertions(+), 60 deletions(-)

diff --git a/modules/db_flatstore/flat_con.c b/modules/db_flatstore/flat_con.c
index e148429..392b9c3 100644
--- a/modules/db_flatstore/flat_con.c
+++ b/modules/db_flatstore/flat_con.c
@@ -1,4 +1,4 @@
-/* 
+/*
  * $Id$
  *
  * Flastore module connection structure
@@ -22,8 +22,8 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
@@ -59,7 +59,7 @@ static char* get_name(struct flat_id* id)
 					total_len, buf_len);
 		return 0;
 	}
-	
+
 	buf=pkg_malloc(buf_len);
 	if (buf==0){
 		LOG(L_ERR, "ERROR: get_name: memory allocation failure\n");
@@ -76,7 +76,7 @@ static char* get_name(struct flat_id* id)
 	ptr += id->table.len;
 
 	*ptr++ = '_';
-	
+
 	num = int2str(flat_pid, &num_len);
 	if (buf_len<(total_len+num_len)){
 		LOG(L_ERR, "ERROR:  get_name: the path is too long (%d and PATHMAX is"
@@ -114,7 +114,7 @@ struct flat_con* flat_new_connection(struct flat_id* id)
 
 	memset(res, 0, sizeof(struct flat_con));
 	res->ref = 1;
-	
+
 	res->id = id;
 
 	fn = get_name(id);
@@ -130,7 +130,7 @@ struct flat_con* flat_new_connection(struct flat_id* id)
 		pkg_free(res);
 		return 0;
 	}
-	
+
 	return res;
 }
 
diff --git a/modules/db_flatstore/flatstore.c b/modules/db_flatstore/flatstore.c
index 8b7bfc2..a69c321 100644
--- a/modules/db_flatstore/flatstore.c
+++ b/modules/db_flatstore/flatstore.c
@@ -1,5 +1,5 @@
-/* 
- * $Id$ 
+/*
+ * $Id$
  *
  * Flatstore module interface
  *
@@ -22,8 +22,8 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 /*
@@ -52,7 +52,7 @@ static int parse_flat_url(const char* url, const char** path)
 	}
 
 	len = strlen(url);
-	
+
 	*path = strchr(url, ':') + 1;
 	return 0;
 }
@@ -121,7 +121,7 @@ int flat_use_table(db_con_t* h, const char* t)
 			return -1;
 		}
 	}
-	
+
 	return 0;
 }
 
@@ -155,6 +155,8 @@ int flat_db_insert(db_con_t* h, db_key_t* k, db_val_t* v, int n)
 {
 	FILE* f;
 	int i;
+	char delims[4], *s;
+	size_t len;
 
 	f = CON_FILE(h);
 	if (!f) {
@@ -168,42 +170,72 @@ int flat_db_insert(db_con_t* h, db_key_t* k, db_val_t* v, int n)
 	}
 
 	for(i = 0; i < n; i++) {
-		switch(VAL_TYPE(v + i)) {
-		case DB_INT:
-			fprintf(f, "%d", VAL_INT(v + i));
-			break;
-
-		case DB_DOUBLE:
-			fprintf(f, "%f", VAL_DOUBLE(v + i));
-			break;
-
-		case DB_STRING:
-			fprintf(f, "%s", VAL_STRING(v + i));
-			break;
-
-		case DB_STR:
-			fprintf(f, "%.*s", VAL_STR(v + i).len, VAL_STR(v + i).s);
-			break;
-
-		case DB_DATETIME:
-			fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
-			break;
-
-		case DB_BLOB:
-			LOG(L_ERR, "flastore: Blobs not supported\n");
-			break;
-
-		case DB_BITMAP:
-			fprintf(f, "%u", VAL_BITMAP(v + i));
-			break;
-		}
 
+		if (!VAL_NULL(v + i)) {   // TODO: how to distinguish NULL from empty
+			switch(VAL_TYPE(v + i)) {
+				case DB_INT:
+					fprintf(f, "%d", VAL_INT(v + i));
+					break;
+
+				case DB_DOUBLE:
+					fprintf(f, "%f", VAL_DOUBLE(v + i));
+					break;
+
+				case DB_STRING: {
+					s = (char*) VAL_STRING(v + i);
+					delims[0] = flat_delimiter[0];
+					delims[1] = flat_record_delimiter[0];
+					delims[2] = flat_escape[0];
+					delims[3] = '\0';
+					while (*s) {
+						len = strcspn(s, delims);
+						fprintf(f, "%.*s", len, s);
+						s+= len;
+						if (*s) {
+							fprintf(f, "%c%c", flat_escape[0], *s);
+							s++;
+						}
+					}
+					break;
+				}
+				case DB_STR:
+				case DB_BLOB:
+					if (VAL_TYPE(v + i) == DB_STR) {
+						s = VAL_STR(v + i).s;
+						len = VAL_STR(v + i).len;
+					}
+					else {
+						s = VAL_BLOB(v + i).s;
+						len = VAL_BLOB(v + i).len;
+					}
+					while (len > 0) {
+						char *c;
+						for (c = s; len > 0 && *c != flat_delimiter[0] && *c != flat_record_delimiter[0] && *c != flat_escape[0]; c++, len--);
+						fprintf(f, "%.*s", c-s, s);
+						s = c;
+						if (len > 0) {
+							fprintf(f, "%c%c", flat_escape[0], *s);
+							s++;
+							len--;
+						}
+					}
+					break;
+
+				case DB_DATETIME:
+					fprintf(f, "%u", (unsigned int)VAL_TIME(v + i));
+					break;
+
+				case DB_BITMAP:
+					fprintf(f, "%u", VAL_BITMAP(v + i));
+					break;
+			}
+		}
 		if (i < (n - 1)) {
-			fprintf(f, "%c", *flat_delimiter);
+			fprintf(f, "%c", flat_delimiter[0]);
 		}
 	}
 
-	fprintf(f, "\n");
+	fprintf(f, "%c", flat_record_delimiter[0]);
 
 	if (flat_flush) {
 		fflush(f);
diff --git a/modules/db_flatstore/flatstore_mod.c b/modules/db_flatstore/flatstore_mod.c
index 4a2ca2b..54b280a 100644
--- a/modules/db_flatstore/flatstore_mod.c
+++ b/modules/db_flatstore/flatstore_mod.c
@@ -1,5 +1,5 @@
-/* 
- * $Id$ 
+/*
+ * $Id$
  *
  * Flatstore module interface
  *
@@ -22,8 +22,8 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 /*
@@ -58,18 +58,26 @@ int flat_pid;
  */
 int flat_flush = 1;
 
+/*
+ * Delimiter delimiting rows
+ */
+char *flat_record_delimiter = "\n";
 
 /*
  * Delimiter delimiting columns
  */
-char* flat_delimiter = "|";
+char *flat_delimiter = "|";
 
+/*
+ * Escape char escaping delimiters
+ */
+char *flat_escape = "\\";
 
 /*
  * Timestamp of the last log rotation request from
  * the FIFO interface
  */
-time_t* flat_rotate;	
+time_t* flat_rotate;
 
 time_t local_timestamp;
 
@@ -90,11 +98,14 @@ static cmd_export_t cmds[] = {
  */
 static param_export_t params[] = {
 	{"flush", INT_PARAM, &flat_flush},
+	{"field_delimiter", STR_PARAM, &flat_delimiter},
+	{"record_delimiter", STR_PARAM, &flat_record_delimiter},
+	{"escape_char", STR_PARAM, &flat_escape},
 	{0, 0, 0}
 };
 
 
-struct module_exports exports = {	
+struct module_exports exports = {
 	"flatstore",
 	cmds,
 	flat_rpc,    /* RPC methods */
@@ -109,8 +120,18 @@ struct module_exports exports = {
 
 static int mod_init(void)
 {
-	if (strlen(flat_delimiter) != 1) {
-		LOG(L_ERR, "flatstore:mod_init: Delimiter has to be exactly one character\n");
+	if (strlen(flat_delimiter) > 1) {
+		LOG(L_ERR, "flatstore:mod_init: Column delimiter has to be max. one character\n");
+		return -1;
+	}
+
+	if (strlen(flat_record_delimiter) > 1) {
+		LOG(L_ERR, "flatstore:mod_init: Record delimiter has to be max. one character\n");
+		return -1;
+	}
+
+	if (strlen(flat_escape) > 0) {
+		LOG(L_ERR, "flatstore:mod_init: Escape char has to be max. one character\n");
 		return -1;
 	}
 
diff --git a/modules/db_flatstore/flatstore_mod.h b/modules/db_flatstore/flatstore_mod.h
index 83bc593..3632ecb 100644
--- a/modules/db_flatstore/flatstore_mod.h
+++ b/modules/db_flatstore/flatstore_mod.h
@@ -1,5 +1,5 @@
-/* 
- * $Id$ 
+/*
+ * $Id$
  *
  * Flatstore module interface
  *
@@ -22,8 +22,8 @@
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License 
- * along with this program; if not, write to the Free Software 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 /*
@@ -43,18 +43,25 @@
  */
 extern int flat_pid;
 
-
 /*
  * Should we flush after each write to the database ?
  */
 extern int flat_flush;
 
+/*
+ * Delimiter delimiting rows
+ */
+extern char *flat_record_delimiter;
 
 /*
- * Delmiter delimiting columns
+ * Delimiter delimiting columns
  */
-extern char* flat_delimiter;
+extern char *flat_delimiter;
 
+/*
+ * Escape char escaning delimiters and itself
+ */
+extern char *flat_escape;
 
 /*
  * The timestamp of log rotation request from




More information about the sr-dev mailing list