Add underscores() helper to replace - with _

This commit is contained in:
Lucas De Marchi 2011-11-30 02:14:33 -02:00
parent 81cf2060e0
commit 8185fc91e2
2 changed files with 33 additions and 0 deletions

View File

@ -63,5 +63,6 @@ int kmod_parse_config(struct kmod_ctx *ctx, struct kmod_config *config);
void kmod_free_config(struct kmod_ctx *ctx, struct kmod_config *config);
char *getline_wrapped(FILE *fp, unsigned int *linenum);
char *underscores(struct kmod_ctx *ctx, char *s);
#endif

View File

@ -83,3 +83,35 @@ char *getline_wrapped(FILE *fp, unsigned int *linenum)
}
}
}
/*
* Replace dashes with underscores.
* Dashes inside character range patterns (e.g. [0-9]) are left unchanged.
*/
char *underscores(struct kmod_ctx *ctx, char *s)
{
unsigned int i;
if (!s)
return NULL;
for (i = 0; s[i]; i++) {
switch (s[i]) {
case '-':
s[i] = '_';
break;
case ']':
INFO(ctx, "Unmatched bracket in %s\n", s);
break;
case '[':
i += strcspn(&s[i], "]");
if (!s[i])
INFO(ctx, "Unmatched bracket in %s\n", s);
break;
}
}
return s;
}