--- l2_ut_level.c 2001/09/13 12:19:45 1.3
+++ l2_ut_level.c 2001/10/09 14:03:04 1.4
@@ -48,37 +48,36 @@
{ 0, NULL }
};
-l2_result_t l2_util_l2s(char *string, size_t maxlen, int sep, unsigned int levelmask)
+l2_result_t l2_util_l2s(char *string, size_t maxlen, l2_level_t level)
{
- char hexbuf[2+(sizeof(unsigned int)*2)+1];
- int len;
- int i;
- int l;
+ int len, i, j, l;
- len = maxlen;
string[0] = '\0';
- 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;
+ 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 */
}
+
+ l = strlen(l2s_table[i].string);
+ if (l > len)
+ len = l; /* len is the size of the largest level string */
}
- 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;
- }
- /* remove trailing comma */
- if ((maxlen-len) > 0)
- string[(maxlen-len)-1] = '\0';
+ 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 */
+ }
+ string[i] = '\0'; /* don't forget to put back the EOL */
return L2_OK;
}
@@ -106,40 +105,26 @@
return 0;
}
-l2_result_t l2_util_s2l(const char *string, size_t maxlen, int sep, unsigned int *levelmask)
+l2_result_t l2_util_s2l(const char *string, size_t maxlen, l2_level_t *level)
{
- const char *cpB;
- const char *cpE;
int bFound;
int i;
- *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))
+ *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;
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 ((cpE > cpB+2) && strncasecmp(cpB, "0x", 2) == 0 && myishexnumber((int)(*(cpB+2)))) {
- *levelmask |= hexval(cpB+2, cpE);
- }
- else
- return L2_ERR_ARG;
+ }
+ if (!bFound) {
+ if (strncasecmp(string, "0x", 2) == 0 && myishexnumber((int)(*(string+2)))) {
+ *level = hexval(string+2, string + strlen(string));
}
+ else
+ return L2_ERR_ARG;
}
return L2_OK;
}
|