OSSP CVS Repository

ossp - Difference in ossp-pkg/l2/l2_ut_level.c versions 1.4 and 1.5
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/l2/l2_ut_level.c 1.4 -> 1.5

--- l2_ut_level.c        2001/10/09 14:03:04     1.4
+++ l2_ut_level.c        2001/10/09 15:34:55     1.5
@@ -48,36 +48,37 @@
     { 0,                 NULL       }
 };
 
-l2_result_t l2_util_l2s(char *string, size_t maxlen, l2_level_t level)
+l2_result_t l2_util_l2s(char *string, size_t maxlen, int sep, unsigned int levelmask)
 {
-    int len, i, j, l;
+    char hexbuf[2+(sizeof(unsigned int)*2)+1];
+    int len;
+    int i;
+    int l;
 
+    len = maxlen;
     string[0] = '\0';
-    len = i = j = l = 0;
-    for (i = 0; l2s_table[i].level != NULL; i++) { /* loop through levels    */
-        if (level & l2s_table[i].level) {
-            if (j != 0)
-                return L2_ERR_ARG; /* level has matched more than one string */
-            else
-                j = i; /* index the given level to its corresponding string  */
+    for (i = 0; l2s_table[i].level != NULL; i++) {
+        if (levelmask & l2s_table[i].level) {
+            levelmask &= ~(l2s_table[i].level);
+            l = strlen(l2s_table[i].string) + 1;
+            if (len < l)
+                return L2_ERR_MEM;
+            sprintf(string+(maxlen-len), "%s%c", l2s_table[i].string, sep);
+            len -= l;
         }
-
-        l = strlen(l2s_table[i].string);
-        if (l > len) 
-            len = l; /* len is the size of the largest level string          */
     }
-
-    if (len + 1 > maxlen)
-        return L2_ERR_MEM;
-    else
-        sprintf(string, "%s", l2s_table[j].string);
-
-    for (i = 0; string[i] != '\0'; i++) /* process string to dynamically pad */
-    ;                                   /* with spaces in order to line up   */
-    while (i < len){                    /* small level string text with the  */
-        string[i++] = ' ';              /* larger ones                       */
+    if (levelmask != 0) {
+        sprintf(hexbuf, "0x%x", levelmask);
+        l = strlen(hexbuf) + 1;
+        if (len < l)
+            return L2_ERR_MEM;
+        sprintf(string+(maxlen-len), "%s%c", hexbuf, sep);
+        len -= l;
     }
-    string[i] = '\0';                   /* don't forget to put back the EOL  */
+    /* remove trailing comma */
+    if ((maxlen-len) > 0)
+        string[(maxlen-len)-1] = '\0'; 
+
     return L2_OK;
 }
 
@@ -105,26 +106,40 @@
     return 0;
 }
 
-l2_result_t l2_util_s2l(const char *string, size_t maxlen, l2_level_t *level)
+l2_result_t l2_util_s2l(const char *string, size_t maxlen, int sep, unsigned int *levelmask)
 {
+    const char *cpB;
+    const char *cpE;
     int bFound;
     int i;
 
-    *level = 0;
-    bFound = 0;
-    for (i = 0; l2s_table[i].level != NULL; i++) {
-        if (strcasecmp(string, l2s_table[i].string) == 0) {
-            *level = l2s_table[i].level;
-            bFound = 1;
+    *levelmask = 0;
+    cpE = string;
+    while (1) {
+        cpB = cpE;
+        if (cpB >= (string+maxlen))
+            break;
+        if ((int)(*cpB) == sep)
+            cpB++;
+        for (cpE = cpB; cpE < (string+maxlen) && (int)(*cpE) != sep; cpE++)
+            ;
+        if (cpE > (string+maxlen))
             break;
+        bFound = 0;
+        for (i = 0; l2s_table[i].level != NULL; i++) {
+            if (strncasecmp(cpB, l2s_table[i].string, cpE-cpB) == 0) {
+                *levelmask |= l2s_table[i].level;
+                bFound = 1;
+                break;
+            }
         }
-    }
-    if (!bFound) {
-        if (strncasecmp(string, "0x", 2) == 0 && myishexnumber((int)(*(string+2)))) {
-            *level = hexval(string+2, string + strlen(string));
+        if (!bFound) {
+            if ((cpE > cpB+2) && strncasecmp(cpB, "0x", 2) == 0 && myishexnumber((int)(*(cpB+2)))) {
+                *levelmask |= hexval(cpB+2, cpE);
+            }
+            else
+                return L2_ERR_ARG;
         }
-        else
-            return L2_ERR_ARG;
     }
     return L2_OK;
 }

CVSTrac 2.0.1