log: use strtok_r
PATCH 1/1 log: use strtok_r
Signed-off-by Paolo Bonzini <[email protected]>
--- include/sysemu/os-win32.h | 1 + util/log.c | 30 ++++++++---------------------- util/oslib-win32.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 22 deletions(-)
include/sysemu/os-win32.h +1 -0
@@ -80,6 +80,7 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);struct tm *localtime_r (const time_t *timep,struct tm *result);#endif /* CONFIG_LOCALTIME_R */ +char *strtok_r (char *str,const char *delim,char **saveptr);static inline void os_setup_signal_handling (void ) {}static inline void os_daemonize (void ) {}
util/log.c +8 -22
@@ -140,40 +140,29 @@ const QEMULogItem qemu_log_items[] = { { 0, NULL, NULL }, }; -static int cmp1 (const char *s1,int n,const char *s2) -{ -if (strlen (s2) != n) { -return 0 ; - } -return memcmp (s1, s2, n) ==0 ; -} -/* takes a comma separated list of log masks. Return 0 if error. */ int qemu_str_to_log_mask (const char *str) {const QEMULogItem *item;int mask; -const char *p, *p1; +char *copy, *p, *p1; - p = str; + copy =strdup (str); mask =0 ; -for (;;) { - p1 =strchr (p,',' ); -if (!p1) { - p1 = p +strlen (p); - } -if (cmp1 (p,p1-p,"all" )) { +for (p =strtok_r (copy,"," , &p1); p; + p =strtok_r (NULL ,"," , &p1)) { +if (!strcmp (p,"all" )) {for (item = qemu_log_items; item->mask !=0 ; item++) { mask |= item->mask; } #ifdef CONFIG_TRACE_LOG - }else if (strncmp (p,"trace:" ,6 ) ==0 && p +6 != p1) { + }else if (strncmp (p,"trace:" ,6 ) ==0 && p[6 ] !=0 ) {trace_enable_events (p +6 ); mask |=LOG_TRACE ;#endif }else {for (item = qemu_log_items; item->mask !=0 ; item++) { -if (cmp1 (p, p1 - p, item->name)) { +if (!strcmp (p, item->name)) { goto found; } }
@@ -181,11 +170,8 @@ int qemu_str_to_log_mask(const char *str) found: mask |= item->mask; } -if (*p1 !=',' ) { -break ; - } - p = p1 +1 ; } +g_free (copy);return mask; }
util/oslib-win32.c +35 -0
@@ -504,3 +504,38 @@ pid_t qemu_fork(Error **errp) "cannot fork child process");return -1 ; } + +/* +* public domain strtok_r() by Charlie Gordon +* +* from comp.lang.c 9/14/2007 +* +* http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684 +* +* (Declaration that it's public domain): +* http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c +*/ +char *strtok_r (char *str,const char *delim,char **saveptr) +{ +char *ret; + +if (!str) { + str = *saveptr; + } + +/* Ignore delimiters at beginning of string. */ + str +=strspn (str, delim); +if (!*str) { +/* Ignore delimiters at end of string too. */ +return NULL ; + } + + ret = str; + str +=strcspn (str, delim); +if (*str) { + *str++ ='\0' ; + } + + *saveptr = str; +return ret; +}
Signature · 1 line
2.5.0
Sent by mistake, sorry.
Paolo
Quoted 128 lines · Paolo Bonzini
Signed-off-by Paolo Bonzini <[email protected]>--- include/sysemu/os-win32.h | 1 + util/log.c | 30 ++++++++---------------------- util/oslib-win32.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 22 deletions(-)include/sysemu/os-win32.h +1
-0@@ -80,6 +80,7 @@ struct tm *gmtime_r(const time_t *timep, struct tm *result);struct tm *localtime_r (const time_t *timep,struct tm *result);#endif /* CONFIG_LOCALTIME_R */ +char *strtok_r (char *str,const char *delim,char **saveptr);static inline void os_setup_signal_handling (void ) {}static inline void os_daemonize (void ) {}util/log.c +8
-22@@ -140,40 +140,29 @@ const QEMULogItem qemu_log_items[] = { { 0, NULL, NULL }, }; -static int cmp1 (const char *s1,int n,const char *s2) -{ -if (strlen (s2) != n) { -return 0 ; - } -return memcmp (s1, s2, n) ==0 ; -} -/* takes a comma separated list of log masks. Return 0 if error. */ int qemu_str_to_log_mask (const char *str) {const QEMULogItem *item;int mask; -const char *p, *p1; +char *copy, *p, *p1; - p = str; + copy =strdup (str); mask =0 ; -for (;;) { - p1 =strchr (p,',' ); -if (!p1) { - p1 = p +strlen (p); - } -if (cmp1 (p,p1-p,"all" )) { +for (p =strtok_r (copy,"," , &p1); p; + p =strtok_r (NULL ,"," , &p1)) { +if (!strcmp (p,"all" )) {for (item = qemu_log_items; item->mask !=0 ; item++) { mask |= item->mask; } #ifdef CONFIG_TRACE_LOG - }else if (strncmp (p,"trace:" ,6 ) ==0 && p +6 != p1) { + }else if (strncmp (p,"trace:" ,6 ) ==0 && p[6 ] !=0 ) {trace_enable_events (p +6 ); mask |=LOG_TRACE ;#endif }else {for (item = qemu_log_items; item->mask !=0 ; item++) { -if (cmp1 (p, p1 - p, item->name)) { +if (!strcmp (p, item->name)) { goto found; } }@@ -181,11 +170,8 @@ int qemu_str_to_log_mask(const char *str) found: mask |= item->mask; } -if (*p1 !=',' ) { -break ; - } - p = p1 +1 ; } +g_free (copy);return mask; }util/oslib-win32.c +35
-0@@ -504,3 +504,38 @@ pid_t qemu_fork(Error **errp) "cannot fork child process");return -1 ; } + +/* +* public domain strtok_r() by Charlie Gordon +* +* from comp.lang.c 9/14/2007 +* +* http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684 +* +* (Declaration that it's public domain): +* http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c +*/ +char *strtok_r (char *str,const char *delim,char **saveptr) +{ +char *ret; + +if (!str) { + str = *saveptr; + } + +/* Ignore delimiters at beginning of string. */ + str +=strspn (str, delim); +if (!*str) { +/* Ignore delimiters at end of string too. */ +return NULL ; + } + + ret = str; + str +=strcspn (str, delim); +if (*str) { + *str++ ='\0' ; + } + + *saveptr = str; +return ret; +}