]> rtime.felk.cvut.cz Git - ulut.git/blobdiff - ulut/ul_dbufmore.c
uLUt: ul_dbuff replace old years unused vca_log to ul_log when debugging is enabled.
[ulut.git] / ulut / ul_dbufmore.c
index 99a98c71f951c565d6a69cd57aecc58502b102af..cc220a333b359c9c1904deb642e6cb91b3810248 100644 (file)
 #include "ul_utmalloc.h"
 #include "ul_dbuff.h"
 
+#ifdef UL_DBUFF_LOGGING
+#include "ul_log.h"
+extern UL_LOG_CUST(ulogd_dbuff)
+#endif /*UL_DBUFF_LOGGING*/
+
 typedef unsigned char byte;
 
-//#define UL_DBUFF_LOGGING
-#ifdef UL_DBUFF_LOGGING
-    #include "can_vca.h"
-    
-    #define LOG_FATAL   0
-    #define LOG_ERR     1
-    #define LOG_MSG     2
-    #define LOG_INF     3
-    #define LOG_DEB     4
-#endif
 //#undef DEBUG
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_set_capacity - change capacity of buffer to at least @new_capacity
  * @buf: buffer structure
@@ -94,7 +88,7 @@ int ul_dbuff_set_capacity(ul_dbuff_t *buf, int new_capacity)
                 else {
                     /* Old data are not changed if realloc fails, capacity remains at old value */
                     #ifdef UL_DBUFF_LOGGING
-                    vca_log("dbuff", LOG_DEB, "realloc buffer to %d failed\n", new_capacity);
+                    ul_logdeb("realloc buffer to %d failed\n", new_capacity);
                     #endif
                 }
             }
@@ -102,12 +96,11 @@ int ul_dbuff_set_capacity(ul_dbuff_t *buf, int new_capacity)
     }
     if(buf->len > buf->capacity) buf->len = buf->capacity;
     #ifdef UL_DBUFF_LOGGING
-    vca_log("dbuff", LOG_DEB, "capacity changed from %d to %ld, required %d\n", old_capacity, buf->capacity, new_capacity);
+    ul_logdeb("capacity changed from %d to %ld, required %d\n", old_capacity, buf->capacity, new_capacity);
     #endif
     return buf->capacity;
 }                                                                               
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_set_len - sets a new len of the buffer, change the capacity if neccessary 
  * @buf: buffer structure
@@ -124,15 +117,14 @@ int ul_dbuff_set_len(ul_dbuff_t *buf, int new_len)
         ul_dbuff_set_capacity(buf, new_cap);
     }
     if(new_len > buf->capacity) {
-        buf->data = buf->sbuff;
-        strncpy(buf->data, "set_len ERROR", buf->capacity);
+        /*buf->data = buf->sbuff;*/
+        /*strncpy(buf->data, "set_len ERROR", buf->capacity);*/
         new_len = buf->capacity;
     }
     buf->len = new_len;
     return buf->len;
 }
  
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_cpy - copies bytes to buffer and change its capacity if neccessary like memset
  * @buf: buffer structure
@@ -148,7 +140,6 @@ int ul_dbuff_set(ul_dbuff_t *buf, byte b, int n)
     return buf->len;
 }
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_cpy - copies bytes to buffer and change its capacity if neccessary 
  * @buf: buffer structure
@@ -165,7 +156,6 @@ int ul_dbuff_cpy(ul_dbuff_t *buf, const void *b, int n)
     return buf->len;
 }
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_cat - appends bytes at end of buffer and change its capacity if neccessary 
  * @buf: buffer structure
@@ -181,18 +171,17 @@ int ul_dbuff_cat(ul_dbuff_t *buf, const void *b, int n)
     if(b == NULL) return 0;
     if(new_len == ul_dbuff_set_len(buf, new_len)) {
         memcpy(buf->data + old_len, b, n);
+    } else {
+       #ifdef UL_DBUFF_LOGGING
+        ul_logdeb("ul_dbuff_cat: set_len(%lu) error, old_len == %lu\n, act len == %lu\n", new_len, old_len, buf->len);
+       #endif
     }
-    #ifdef UL_DBUFF_LOGGING
-    else {
-        vca_log("dbuff", LOG_DEB, "ul_dbuff_cat: set_len(%lu) error, old_len == %lu\n, act len == %lu\n", new_len, old_len, buf->len);
-    }
-    #endif
     return buf->len;
 }
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_strcat - appends str at dhe end of buffer and change its capacity if neccessary 
+ * @buf: buffer structure
  * @str: string to append
  *
  * Returns number length of buffer (including terminating '\0')
@@ -201,26 +190,26 @@ inline int ul_dbuff_strcat(ul_dbuff_t *buf, const char *str)
 {
     /*
     #ifdef UL_DBUFF_LOGGING
-    if(buf->len > 0) vca_log("dbuff", LOG_DEB, "ul_dbuff_strcat: '%s' + '%s'\n", buf->data, str);
-    else vca_log("dbuff", LOG_DEB, "ul_dbuff_strcat: '' + %s\n", str);
+    if(buf->len > 0) ul_logdeb("ul_dbuff_strcat: '%s' + '%s'\n", buf->data, str);
+    else ul_logdeb("ul_dbuff_strcat: '' + %s\n", str);
     #endif
     */
     if(str == NULL) return 0;
     if(buf->len > 0 && buf->data[buf->len-1] == '\0') { 
         /* #ifdef UL_DBUFF_LOGGING
-        vca_log("dbuff", LOG_DEB, "ul_dbuff_strcat: terminating zero found at %ld, after '%c'\n", buf->len-1, buf->data[buf->len - 2]);
+        ul_logdeb("ul_dbuff_strcat: terminating zero found at %ld, after '%c'\n", buf->len-1, buf->data[buf->len - 2]);
         #endif
         */
         ul_dbuff_set_len(buf, buf->len - 1);
     }
     #ifdef UL_DBUFF_LOGGING
     if(buf->len > 0 && buf->data[buf->len-1] != '\0') {
-        vca_log("dbuff", LOG_ERR, "ul_dbuff_strcat: terminating zero not found at %ld, found '%c'\n", buf->len-1, buf->data[buf->len-1]);
+        ul_logerr("ul_dbuff_strcat: terminating zero not found at %ld, found '%c'\n", buf->len-1, buf->data[buf->len-1]);
     }
     #endif
     /* #ifdef UL_DBUFF_LOGGING
     ul_dbuff_cat(buf, str, strlen(str) + 1);
-    vca_log("dbuff", LOG_DEB, "ul_dbuff_strcat: returning '%s'\n", buf->data);
+    ul_logdeb("ul_dbuff_strcat: returning '%s'\n", buf->data);
     return buf->len;
     #else
     return ul_dbuff_cat(buf, str, strlen(str) + 1);
@@ -228,9 +217,9 @@ inline int ul_dbuff_strcat(ul_dbuff_t *buf, const char *str)
     return ul_dbuff_cat(buf, str, strlen(str) + 1);
 }
  
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_strcpy - copy str to the buffer and change its capacity if neccessary 
+ * @buf: buffer structure
  * @str: string to copy
  *
  * Returns number length of buffer (including terminating '\0')
@@ -241,20 +230,18 @@ inline int ul_dbuff_strcpy(ul_dbuff_t *buf, const char *str)
     return ul_dbuff_strcat(buf, str);
 }
  
-//-----------------------------------------------------------------
 /**
-* ul_dbuff_append_byte - appends byte at dhe end of buffer and change its capacity if neccessary 
-* @buf: buffer structure
-* @b: appended byte
-*
+ * ul_dbuff_append_byte - appends byte at dhe end of buffer and change its capacity if neccessary 
+ * @buf: buffer structure
+ * @b: appended byte
+ *
  * Returns number length of buffer (including terminating '\0')
-*/
+ */
 inline int ul_dbuff_append_byte(ul_dbuff_t *buf, unsigned char b)
 {
     return ul_dbuff_cat(buf, &b, 1);
 }
  
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_ltrim - remove all white space characters from the left 
  * @buf: buffer structure
@@ -273,12 +260,12 @@ int ul_dbuff_ltrim(ul_dbuff_t *buf)
        return buf->len;
 }
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_rtrim - remove all white space characters from the right
- * if buffer is terminated by '\0', than is also terminated after rtrim
  * @buf: buffer structure
  *
+ * if buffer is terminated by '\0', than is also terminated after rtrim
+ *
  * Return: new length of buffer
  */
 int ul_dbuff_rtrim(ul_dbuff_t *buf)
@@ -299,7 +286,6 @@ int ul_dbuff_rtrim(ul_dbuff_t *buf)
        return buf->len;
 }
 
-//-----------------------------------------------------------------
 /**
  * ul_dbuff_trim - remove all white space characters from the right and from the left 
  * @buf: buffer structure
@@ -311,7 +297,7 @@ int ul_dbuff_trim(ul_dbuff_t *buf)
     ul_dbuff_rtrim(buf);
     return ul_dbuff_ltrim(buf);
 }
-//-----------------------------------------------------------------
+
 /**
  * ul_dbuff_cpos - searches string for char 
  * @buf:   searched dbuff
@@ -338,7 +324,7 @@ int ul_dbuff_cpos(const ul_dbuff_t *buf, unsigned char what, unsigned char quote
     if(ret >= buf->len) ret = -1;
     return ret;
 }
-//-----------------------------------------------------------------
+
 /**
  * ul_str_cpos - searches string for char 
  * @str:   zero terminated string
@@ -364,7 +350,7 @@ int ul_str_cpos(const unsigned char *str, unsigned char what, unsigned char quot
     if(!str[ret]) ret = -1;
     return ret;
 }
-//-----------------------------------------------------------------
+
 /**
  * ul_str_pos - searches string for substring
  * @str:   zero terminated string
@@ -391,14 +377,15 @@ int ul_str_pos(const unsigned char *str, const unsigned char *what, unsigned cha
     }
     return -1;
 }
-//-----------------------------------------------------------------
+
 /**
- * ul_str_ncpy - copyes string to the buffer<br>
- *               Standard strncpy function have some disadvatages 
- *               (ie. do not append term. zero if copied string doesn't fit in to buffer, fills whole rest of buffer with zeros)
+ * ul_str_ncpy - copies string to the buffer
  * @to:         buffer where to copy str
  * @from:       zero terminated string
- * @buff_size:  size of the <code>to</code> buffer (including terminating zero)
+ * @buff_size:  size of the @to buffer (including terminating zero)
+ *
+ * Standard strncpy function have some disadvatages 
+ * (ie. do not append term. zero if copied string doesn't fit in to buffer, fills whole rest of buffer with zeros)
  *
  * Returns strlen(to) or negative value in case of error
  */
@@ -411,28 +398,7 @@ int ul_str_ncpy(unsigned char *to, const unsigned char *from, int buff_size)
     to[i] = '\0';
     return i;
 }
-//-----------------------------------------------------------------
-/**
- * ul_dbuff_log_hex - writes content of dbuff to log
- * @buf: buffer structure
- * @log_level: logging level
- */
-void ul_dbuff_log_hex(ul_dbuff_t *buf, int log_level)
-{
-#ifdef UL_DBUFF_LOGGING
-    vca_log("dbuff", log_level, "[");
-    int i;
-    log_level |= VCA_LOGL_CONT;
-    for(i=0; i<buf->len; i++) {
-        char c = buf->data[i];
-        if(i > 0) vca_log("dbuff", log_level, " ");
-        if(c > ' ') vca_log("dbuff", log_level, " %c", c);
-        else        vca_log("dbuff", log_level, "%02x", c);
-    }
-    vca_log("dbuff", log_level, "]\n");
-#endif
-}
-//-----------------------------------------------------------------
+
 /**
  * ul_dbuff_cut_pos - cut first @n bytes from @fromdb and copies it to @todb.
  * If @n is greater than fromdb.len whole @fromdb is copied to @todb.
@@ -453,14 +419,14 @@ void ul_dbuff_cut_pos(ul_dbuff_t *fromdb, ul_dbuff_t *todb, int n)
     memmove(fromdb->data, fromdb->data + n, fromdb->len - n);
     ul_dbuff_set_len(fromdb, newlen);
 }
-//-----------------------------------------------------------------
+
 /**
  * ul_dbuff_cut - cuts bytes before delimiter + delimiter char from @fromdb and copies tham to the @todb
  * If @fromdb doesn't contain delimiter @todb is trimmed to zero length.
  * @fromdb: buffer to cut from
  * @todb:   buffer to copy to
  * @delimiter: delimiter char
- * quote:   quoted delimiters are ignored, @quote can be '\0', than it is ignored.
+ * @quote:   quoted delimiters are ignored, @quote can be '\0', than it is ignored.
  */
 void ul_dbuff_cut_delimited(ul_dbuff_t *fromdb, ul_dbuff_t *todb, char delimiter, char quote)
 {
@@ -468,7 +434,7 @@ void ul_dbuff_cut_delimited(ul_dbuff_t *fromdb, ul_dbuff_t *todb, char delimiter
     if(pos < 0) pos = -1;
     ul_dbuff_cut_pos(fromdb, todb, pos+1);
 }
-//-----------------------------------------------------------------
+
 /**
  * ul_dbuff_cut_token - cuts not whitespaces from %fromdb to %todb.
  *                      Leading whitespaces are ignored. Cut string is trimmed.
@@ -479,12 +445,27 @@ void ul_dbuff_cut_token(ul_dbuff_t *fromdb, ul_dbuff_t *todb)
 {
     const unsigned char *pc = (unsigned char*)fromdb->data;
     int pos;
-    // skip leading white spaces
+    /* skip leading white spaces */
     for(pos=0; pc[pos]>0 && pc[pos]<=' ' && pos<fromdb->len; pos++);
-    // skip token
+    /* skip token */
     for(; pc[pos]>' ' && pos<fromdb->len; pos++);
     ul_dbuff_cut_pos(fromdb, todb, pos);
     ul_dbuff_trim(todb);
 }
-//-----------------------------------------------------------------
 
+/**
+ * ul_dbuff_export - Copies data from %srcdb to the buffer %dest.
+ * @srcdb: source dbuff
+ * @dest: destination buffer
+ * @maxlen: maximum number of bytes to copy
+ * 
+ * Returns: the number of bytes copied.
+ */
+int ul_dbuff_export(ul_dbuff_t *srcdb, void *dest, int maxlen)
+{
+    int len;
+    if (!dest) return 0;
+    len = srcdb->len < maxlen ? srcdb->len : maxlen;
+    memcpy(dest, srcdb->data, len);
+    return len;
+}