aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2008-05-12 17:23:29 +0400
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2008-05-12 17:23:29 +0400
commit2da7593c289cb3480ced048c8068ebc3a3119fd8 (patch)
tree417cfb5c943d8fd17b15ff6df4ed84581861e345
parent70c78281e3957bdbaa1204e40f2419a4a1918eb7 (diff)
downloadrspamd-2da7593c289cb3480ced048c8068ebc3a3119fd8.tar.gz
rspamd-2da7593c289cb3480ced048c8068ebc3a3119fd8.zip
* Add fixed string library
-rwxr-xr-xconfigure4
-rw-r--r--fstring.c236
-rw-r--r--fstring.h77
3 files changed, 315 insertions, 2 deletions
diff --git a/configure b/configure
index e967bb075..ca1271608 100755
--- a/configure
+++ b/configure
@@ -17,7 +17,7 @@ LEX_SRC="cfg_file.l"
YACC_OUTPUT="cfg_yacc.c"
LEX_OUTPUT="cfg_lex.c"
-SOURCES="upstream.c cfg_utils.c memcached.c main.c util.c worker.c ${LEX_OUTPUT} ${YACC_OUTPUT}"
+SOURCES="upstream.c cfg_utils.c memcached.c main.c util.c worker.c fstring.c ${LEX_OUTPUT} ${YACC_OUTPUT}"
CFLAGS="$CFLAGS -W -Wall -Wpointer-arith -Wno-unused-parameter"
CFLAGS="$CFLAGS -Wno-unused-function -Wunused-variable -Wno-sign-compare"
@@ -25,7 +25,7 @@ CFLAGS="$CFLAGS -Wunused-value -ggdb -I${LOCALBASE}/include"
CFLAGS="$CFLAGS -DRVERSION=\\\"${VERSION}\\\" -DHASH_COMPAT"
LDFLAGS="$LDFLAGS -L/usr/lib -L${LOCALBASE}/lib"
OPT_FLAGS="-O -pipe -fno-omit-frame-pointer"
-DEPS="cfg_file.h memcached.h util.h main.h upstream.h ${LEX_OUTPUT} ${YACC_OUTPUT}"
+DEPS="cfg_file.h memcached.h util.h main.h upstream.h fstring.h ${LEX_OUTPUT} ${YACC_OUTPUT}"
EXEC=rspamd
USER=postfix
GROUP=postfix
diff --git a/fstring.c b/fstring.c
new file mode 100644
index 000000000..cecf531de
--- /dev/null
+++ b/fstring.c
@@ -0,0 +1,236 @@
+#include <stdlib.h>
+
+#include "fstring.h"
+
+/*
+ * Search first occurence of character in string
+ */
+ssize_t
+fstrchr (f_str_t *src, char c)
+{
+ register ssize_t cur = 0;
+
+ while (cur < src->len) {
+ if (*(src->begin + cur) == c) {
+ return cur;
+ }
+ cur ++;
+ }
+
+ return -1;
+}
+
+/*
+ * Search last occurence of character in string
+ */
+ssize_t
+fstrrchr (f_str_t *src, char c)
+{
+ register ssize_t cur = src->len;
+
+ while (cur > 0) {
+ if (*(src->begin + cur) == c) {
+ return cur;
+ }
+ cur --;
+ }
+
+ return -1;
+}
+
+/*
+ * Search for pattern in orig
+ */
+ssize_t
+fstrstr (f_str_t *orig, f_str_t *pattern)
+{
+ register ssize_t cur = 0, pcur = 0;
+
+ if (pattern->len > orig->len) {
+ return -1;
+ }
+
+ while (cur < orig->len) {
+ if (*(orig->begin + cur) == *pattern->begin) {
+ while (cur < orig->len && pcur < pattern->len) {
+ if (*(orig->begin + cur) != *(pattern->begin + pcur)) {
+ pcur = 0;
+ break;
+ }
+ cur ++;
+ pcur ++;
+ }
+ return cur - pattern->len;
+ }
+ cur ++;
+ }
+
+ return -1;
+
+}
+
+/*
+ * Split string by tokens
+ * word contains parsed word
+ *
+ * Return: -1 - no new words can be extracted
+ * 1 - word was extracted and there are more words
+ * 0 - last word extracted
+ */
+int
+fstrtok (f_str_t *text, const char *sep, f_tok_t *state)
+{
+ register size_t cur;
+ const char *csep = sep;
+
+ if (state->pos >= text->len) {
+ return -1;
+ }
+
+ cur = state->pos;
+
+ while (cur < text->len) {
+ while (*csep) {
+ if (*(text->begin + cur) == *csep) {
+ state->word.begin = (text->begin + state->pos);
+ state->word.len = cur - state->pos;
+ state->pos = cur + 1;
+ return 1;
+ }
+ csep ++;
+ }
+ csep = sep;
+ cur ++;
+ }
+
+ /* Last word */
+ state->word.begin = (text->begin + state->pos);
+ state->word.len = cur - state->pos;
+ state->pos = cur;
+
+ return 0;
+}
+
+/*
+ * Copy one string into other
+ */
+size_t
+fstrcpy (f_str_t *dest, f_str_t *src)
+{
+ register size_t cur = 0;
+
+ if (dest->size < src->len) {
+ return 0;
+ }
+
+ while (cur < src->len && cur < dest->size) {
+ *(dest->begin + cur) = *(src->begin + cur);
+ cur ++;
+ }
+
+ return cur;
+}
+
+/*
+ * Concatenate two strings
+ */
+size_t
+fstrcat (f_str_t *dest, f_str_t *src)
+{
+ register size_t cur = src->len;
+
+ if (dest->size < src->len + dest->len) {
+ return 0;
+ }
+
+ while (cur < src->len && cur < dest->size) {
+ *(dest->begin + cur) = *(src->begin + cur);
+ cur ++;
+ }
+
+ dest->len += src->len;
+
+ return cur;
+
+}
+
+/*
+ * Push one character to fstr
+ */
+int
+fstrpush (f_str_t *dest, char c)
+{
+ if (dest->size < dest->len) {
+ /* Need to reallocate string */
+ return 0;
+ }
+
+ *(dest->begin + dest->len) = c;
+ dest->len ++;
+ return 1;
+}
+
+/*
+ * Allocate memory for f_str_t
+ */
+f_str_t*
+fstralloc (size_t len)
+{
+ f_str_t *res = malloc (sizeof (f_str_t));
+
+ if (res == NULL) {
+ return NULL;
+ }
+ res->begin = malloc (len);
+ if (res->begin == NULL) {
+ free (res);
+ return NULL;
+ }
+
+ res->size = len;
+ return res;
+}
+
+/*
+ * Truncate string to its len
+ */
+f_str_t*
+fstrtruncate (f_str_t *orig)
+{
+ f_str_t *res;
+
+ if (orig == NULL || orig->len == 0 || orig->size <= orig->len) {
+ return orig;
+ }
+
+ res = fstralloc (orig->len);
+ if (res == NULL) {
+ return NULL;
+ }
+ fstrcpy (res, orig);
+ fstrfree (orig);
+
+ return res;
+}
+
+/*
+ * Enlarge string to new size
+ */
+f_str_t*
+fstrgrow (f_str_t *orig, size_t newlen)
+{
+ f_str_t *res;
+
+ if (orig == NULL || orig->len == 0 || orig->size >= newlen) {
+ return orig;
+ }
+
+ res = fstralloc (newlen);
+ if (res == NULL) {
+ return NULL;
+ }
+ fstrcpy (res, orig);
+ fstrfree (orig);
+
+ return res;
+}
diff --git a/fstring.h b/fstring.h
new file mode 100644
index 000000000..30895ccc5
--- /dev/null
+++ b/fstring.h
@@ -0,0 +1,77 @@
+/*
+ * Functions for handling with fixed size strings
+ */
+
+#ifndef FSTRING_H
+#define FSTRING_H
+
+#include <sys/types.h>
+
+typedef struct f_str_s {
+ char *begin;
+ size_t len;
+ size_t size;
+} f_str_t;
+
+typedef struct f_tok_s {
+ f_str_t word;
+ size_t pos;
+} f_tok_t;
+
+/*
+ * Search first occurence of character in string
+ */
+ssize_t fstrchr (f_str_t *src, char c);
+
+/*
+ * Search last occurence of character in string
+ */
+ssize_t fstrrchr (f_str_t *src, char c);
+
+/*
+ * Search for pattern in orig
+ */
+ssize_t fstrstr (f_str_t *orig, f_str_t *pattern);
+
+/*
+ * Split string by tokens
+ * word contains parsed word
+ */
+int fstrtok (f_str_t *text, const char *sep, f_tok_t *state);
+
+/*
+ * Copy one string into other
+ */
+size_t fstrcpy (f_str_t *dest, f_str_t *src);
+
+/*
+ * Concatenate two strings
+ */
+size_t fstrcat (f_str_t *dest, f_str_t *src);
+
+/*
+ * Push one character to fstr
+ */
+int fstrpush (f_str_t *dest, char c);
+
+/*
+ * Allocate memory for f_str_t
+ */
+f_str_t* fstralloc (size_t len);
+
+/*
+ * Truncate string to its len
+ */
+f_str_t* fstrtruncate (f_str_t *orig);
+
+/*
+ * Enlarge string to new size
+ */
+f_str_t* fstrgrow (f_str_t *orig, size_t newlen);
+
+/*
+ * Free memory for f_str_t
+ */
+#define fstrfree(x) free((x)->begin); free((x))
+
+#endif