From: R. Steve McKown Date: Fri, 28 Sep 2012 19:04:21 +0000 (-0600) Subject: stringutils.c collects funcs from several places X-Git-Tag: 0.5~20 X-Git-Url: https://oss.titaniummirror.com/gitweb?p=repo_shell.git;a=commitdiff_plain;h=cb5d643dcf8bc721ae41a605bdd7bd8facc16c8d stringutils.c collects funcs from several places --- diff --git a/Makefile b/Makefile index eb4728c..d9d1847 100644 --- a/Makefile +++ b/Makefile @@ -15,13 +15,13 @@ PROGRAMS = $(MAIN) mystrtok_test acl_test all: $(MAIN) -repo_shell: repo_shell.c inih/ini.c git_acl.c mystrtok.c stra.c utility.c match.c version.c +repo_shell: repo_shell.c inih/ini.c git_acl.c stringutils.c stra.c utility.c version.c $(CC) -I inih $^ -o $@ -mystrtok_test: mystrtok_test.c mystrtok.c utility.c +mystrtok_test: mystrtok_test.c stringutils.c utility.c $(CC) -g $^ -o $@ -acl_test: acl_test.c git_acl.c inih/ini.c mystrtok.c stra.c utility.c match.c +acl_test: acl_test.c git_acl.c inih/ini.c stringutils.c stra.c utility.c $(CC) -I inih $^ -o $@ $(INFILES):%: %.in diff --git a/git_acl.c b/git_acl.c index 4bb75ee..796f5e9 100644 --- a/git_acl.c +++ b/git_acl.c @@ -16,19 +16,12 @@ */ #include -//#include -//#include -//#include -//#include -//#include -//#include -//#include +#include #include #include "ini.h" #include "utility.h" -#include "mystrtok.h" +#include "stringutils.h" #include "stra.h" -#include "match.h" #include "git_acl.h" enum { @@ -115,23 +108,6 @@ static acl_clear(acl_t *acl) stra_destroy(&acl->userids); } -static bool str_has_word(const char* string, const char* word) -{ - char *_s = xstrdup(string); - char *s = _s; - char *p = my_strtok(&s, " \t\n"); - - while (p) { - if (match(p, word)) { - free(_s); - return true; - } - p = my_strtok(&s, " \t\n"); - } - free(_s); - return false; -} - static int acl_handler(void* user, const char* section, const char* name, const char* value) { diff --git a/git_acl.h b/git_acl.h index 47c6aa8..b1be165 100644 --- a/git_acl.h +++ b/git_acl.h @@ -18,19 +18,6 @@ #ifndef GIT_ACL_H #define GIT_ACL_H -#include -#include -#include -#include -#include -#include -#include -#include -#include "ini.h" -#include "utility.h" -#include "mystrtok.h" -#include "stra.h" - typedef enum { PERMS_NOTFOUND = 0, PERMS_NONE, diff --git a/match.c b/match.c deleted file mode 100644 index 32938a1..0000000 --- a/match.c +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright © 2012, Titanium Mirror, Inc. - * All Rights Reserved. - * - * This document is the proprietary and confidential property of - * Titanium Mirror, Inc. All use, distribution, reproduction or re-distribution - * is disallowed without the prior express written consent of - * Titanium Mirror, Inc. - */ - -/* - * Match allowing wildcard ('*') - * - * @author R. Steve McKown - */ - -#include -#include - -bool match(const char* pattern, const char* string) -{ - char* p; - - if (!pattern || !string) - return false; - p = index(pattern, '*'); - if (p == pattern) - return true; - else if (p) - return (strncmp(pattern, string, p - pattern) == 0); - else - return (strcmp(pattern, string) == 0); -} diff --git a/match.h b/match.h deleted file mode 100644 index 756d6c6..0000000 --- a/match.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright © 2012, Titanium Mirror, Inc. - * All Rights Reserved. - * - * This document is the proprietary and confidential property of - * Titanium Mirror, Inc. All use, distribution, reproduction or re-distribution - * is disallowed without the prior express written consent of - * Titanium Mirror, Inc. - */ - -/* - * Match allowing wildcard ('*') - * - * @author R. Steve McKown - */ - -#ifndef MATCH_H -#define MATCH_H - -#include - -bool match(const char* pattern, const char* string); - -#endif /* end of include guard: MATCH_H */ diff --git a/mystrtok.c b/mystrtok.c deleted file mode 100644 index f075683..0000000 --- a/mystrtok.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright © 2012, Titanium Mirror, Inc. - * All Rights Reserved. - * - * This document is the proprietary and confidential property of - * Titanium Mirror, Inc. All use, distribution, reproduction or re-distribution - * is disallowed without the prior express written consent of - * Titanium Mirror, Inc. - */ - -/* - * A custom strtok. This version skips any number and any combination of - * delimiter characters between tokens. - * - * @author R. Steve McKown - */ - -#include -#include - -char *my_strtok(char **begin, char* delims) -{ - char *p; - char *q; - - if (!begin) - return NULL; - if (!delims || !*delims || !*begin) - return (*begin = NULL); - - q = *begin; - while (*q && index(delims, *q)) q++; - if (!*q) - return (*begin = NULL); - p = q; - while (*q && !index(delims, *q)) q++; - if (*q) - *q++ = 0; - *begin = q; - return p; -} diff --git a/mystrtok.h b/mystrtok.h deleted file mode 100644 index 6968fa8..0000000 --- a/mystrtok.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright © 2012, Titanium Mirror, Inc. - * All Rights Reserved. - * - * This document is the proprietary and confidential property of - * Titanium Mirror, Inc. All use, distribution, reproduction or re-distribution - * is disallowed without the prior express written consent of - * Titanium Mirror, Inc. - */ - -/* - * A custom strtok. This version skips any number and any combination of - * delimiter characters between tokens. - * - * @author R. Steve McKown - */ - -#ifndef MYSTRTOK_H -#define MYSTRTOK_H - -char *my_strtok(char **begin, char* delims); - -#endif /* end of include guard: MYSTRTOK_H */ diff --git a/mystrtok_test.c b/mystrtok_test.c index 5a8756e..fa68589 100644 --- a/mystrtok_test.c +++ b/mystrtok_test.c @@ -1,6 +1,6 @@ #include #include "utility.h" -#include "mystrtok.h" +#include "stringutils.h" int main(int argc, char **argv) { diff --git a/repo_shell.c b/repo_shell.c index 86e27b2..7ef610d 100644 --- a/repo_shell.c +++ b/repo_shell.c @@ -12,6 +12,7 @@ #include "utility.h" #include "version.h" #include "git_acl.h" +#include "stringutils.h" #define CFG_FILE "/etc/repo_shell.cfg" #define SHELL "/bin/bash" diff --git a/stra.c b/stra.c index 232419a..21bceac 100644 --- a/stra.c +++ b/stra.c @@ -16,8 +16,8 @@ #include #include "utility.h" +#include "stringutils.h" #include "stra.h" -#include "match.h" void stra_init(stra_t *stra, size_t size) { diff --git a/stringutils.c b/stringutils.c new file mode 100644 index 0000000..8249d86 --- /dev/null +++ b/stringutils.c @@ -0,0 +1,77 @@ +/* + * Copyright © 2012, Titanium Mirror, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of + * Titanium Mirror, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of + * Titanium Mirror, Inc. + */ + +/* + * A custom strtok. This version skips any number and any combination of + * delimiter characters between tokens. + * + * @author R. Steve McKown + */ + +#include +#include +#include +#include +#include +#include "utility.h" + +bool match(const char* pattern, const char* string) +{ + char* p; + + if (!pattern || !string) + return false; + p = index(pattern, '*'); + if (p == pattern) + return true; + else if (p) + return (strncmp(pattern, string, p - pattern) == 0); + else + return (strcmp(pattern, string) == 0); +} + +char *my_strtok(char **begin, char* delims) +{ + char *p; + char *q; + + if (!begin) + return NULL; + if (!delims || !*delims || !*begin) + return (*begin = NULL); + + q = *begin; + while (*q && index(delims, *q)) q++; + if (!*q) + return (*begin = NULL); + p = q; + while (*q && !index(delims, *q)) q++; + if (*q) + *q++ = 0; + *begin = q; + return p; +} + +bool str_has_word(const char* string, const char* word) +{ + char *_s = xstrdup(string); + char *s = _s; + char *p = my_strtok(&s, " \t\n"); + + while (p) { + if (match(p, word)) { + free(_s); + return true; + } + p = my_strtok(&s, " \t\n"); + } + free(_s); + return false; +} diff --git a/stringutils.h b/stringutils.h new file mode 100644 index 0000000..16731dd --- /dev/null +++ b/stringutils.h @@ -0,0 +1,43 @@ +/* + * Copyright © 2012, Titanium Mirror, Inc. + * All Rights Reserved. + * + * This document is the proprietary and confidential property of + * Titanium Mirror, Inc. All use, distribution, reproduction or re-distribution + * is disallowed without the prior express written consent of + * Titanium Mirror, Inc. + */ + +/* + * A custom strtok. This version skips any number and any combination of + * delimiter characters between tokens. + * + * @author R. Steve McKown + */ + +#ifndef MYSTRTOK_H +#define MYSTRTOK_H + +#include + +/* Return tokens separated by any combination of contiguous characters drawn + * from the contents of delims. begin is the address of the pointer containing + * the original string on first call, and is updated during each call to skip + * the token already returned. Returns NULL when no more tokens are available + * in the string. The string pointed to by **begin is altered, as is true also + * with the generic strtok(). + */ +char *my_strtok(char **begin, char* delims); + +/* Returns true if the word is contained within string. Words are delimited by + * any whitespace characters. + */ +bool str_has_word(const char* string, const char* word); + +/* Returns true if the pattern matches string. Only the asterisk character is + * used as a wildcard, meaning zero or more characters of any value. The + * wildcard, if present, may only be the last character in the pattern string. + */ +bool match(const char* pattern, const char* string); + +#endif /* end of include guard: MYSTRTOK_H */