From 72c39e3522c5781d1e7dc8abad77d96141c5d49b Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Thu, 1 May 2008 16:36:27 +0000 Subject: Import beginnings of a CSS parsing library. Currently comprises a lexer. svn path=/trunk/libcss/; revision=4112 --- src/utils/Makefile | 46 +++++++++++++++++++++++++ src/utils/errors.c | 80 ++++++++++++++++++++++++++++++++++++++++++++ src/utils/parserutilserror.h | 29 ++++++++++++++++ src/utils/utils.h | 28 ++++++++++++++++ 4 files changed, 183 insertions(+) create mode 100644 src/utils/Makefile create mode 100644 src/utils/errors.c create mode 100644 src/utils/parserutilserror.h create mode 100644 src/utils/utils.h (limited to 'src/utils') diff --git a/src/utils/Makefile b/src/utils/Makefile new file mode 100644 index 0000000..912590c --- /dev/null +++ b/src/utils/Makefile @@ -0,0 +1,46 @@ +# Child makefile fragment +# +# Toolchain is provided by top-level makefile +# +# Variables provided by top-level makefile +# +# COMPONENT The name of the component +# EXPORT The location of the export directory +# TOP The location of the source tree root +# RELEASEDIR The place to put release objects +# DEBUGDIR The place to put debug objects +# +# do_include Canned command sequence to include a child makefile +# +# Variables provided by parent makefile: +# +# DIR The name of the directory we're in, relative to $(TOP) +# +# Variables we can manipulate: +# +# ITEMS_CLEAN The list of items to remove for "make clean" +# ITEMS_DISTCLEAN The list of items to remove for "make distclean" +# TARGET_TESTS The list of target names to run for "make test" +# +# SOURCES The list of sources to build for $(COMPONENT) +# +# Plus anything from the toolchain + +# Push parent directory onto the directory stack +sp := $(sp).x +dirstack_$(sp) := $(d) +d := $(DIR) + +# Sources +SRCS_$(d) := errors.c + +# Append to sources for component +SOURCES += $(addprefix $(d), $(SRCS_$(d))) + +# Now include any children we may have +MAKE_INCLUDES := $(wildcard $(d)*/Makefile) +$(eval $(foreach INC, $(MAKE_INCLUDES), $(call do_include,$(INC)))) + +# Finally, pop off the directory stack +d := $(dirstack_$(sp)) +sp := $(basename $(sp)) diff --git a/src/utils/errors.c b/src/utils/errors.c new file mode 100644 index 0000000..ac7dd42 --- /dev/null +++ b/src/utils/errors.c @@ -0,0 +1,80 @@ +/* + * This file is part of LibCSS. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#include + +#include + +/** + * Convert a LibCSS error code to a string + * + * \param error The error code to convert + * \return Pointer to string representation of error, or NULL if unknown. + */ +const char *css_error_to_string(css_error error) +{ + const char *result = NULL; + + switch (error) { + case CSS_OK: + result = "No error"; + break; + case CSS_NOMEM: + result = "Insufficient memory"; + break; + case CSS_BADPARM: + result = "Bad parameter"; + break; + case CSS_INVALID: + result = "Invalid input"; + break; + case CSS_FILENOTFOUND: + result = "File not found"; + break; + case CSS_NEEDDATA: + result = "Insufficient data"; + break; + case CSS_BADCHARSET: + result = "BOM and @charset mismatch"; + break; + case CSS_EOF: + result = "EOF encountered"; + break; + } + + return result; +} + +/** + * Convert a string representation of an error name to a LibCSS error code + * + * \param str String containing error name + * \param len Length of string (bytes) + * \return LibCSS error code, or CSS_OK if unknown + */ +css_error css_error_from_string(const char *str, size_t len) +{ + if (strncmp(str, "CSS_OK", len) == 0) { + return CSS_OK; + } else if (strncmp(str, "CSS_NOMEM", len) == 0) { + return CSS_NOMEM; + } else if (strncmp(str, "CSS_BADPARM", len) == 0) { + return CSS_BADPARM; + } else if (strncmp(str, "CSS_INVALID", len) == 0) { + return CSS_INVALID; + } else if (strncmp(str, "CSS_FILENOTFOUND", len) == 0) { + return CSS_FILENOTFOUND; + } else if (strncmp(str, "CSS_NEEDDATA", len) == 0) { + return CSS_NEEDDATA; + } else if (strncmp(str, "CSS_BADCHARSET", len) == 0) { + return CSS_BADCHARSET; + } else if (strncmp(str, "CSS_EOF", len) == 0) { + return CSS_EOF; + } + + return CSS_OK; +} diff --git a/src/utils/parserutilserror.h b/src/utils/parserutilserror.h new file mode 100644 index 0000000..d2cffb6 --- /dev/null +++ b/src/utils/parserutilserror.h @@ -0,0 +1,29 @@ +/* + * This file is part of LibCSS. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#ifndef css_utils_parserutilserror_h_ +#define css_utils_parserutilserror_h_ + +#include + +#include + +/** + * Convert a ParserUtils error into a LibCSS error + * + * \param error The ParserUtils error to convert + * \return The corresponding LibCSS error + */ +static inline css_error css_error_from_parserutils_error( + parserutils_error error) +{ + /* Currently, there's a 1:1 mapping, so we've nothing to do */ + return (css_error) error; +} + +#endif + diff --git a/src/utils/utils.h b/src/utils/utils.h new file mode 100644 index 0000000..ac19e59 --- /dev/null +++ b/src/utils/utils.h @@ -0,0 +1,28 @@ +/* + * This file is part of LibCSS. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2007 John-Mark Bell + */ + +#ifndef css_utils_h_ +#define css_utils_h_ + +#ifndef max +#define max(a,b) ((a)>(b)?(a):(b)) +#endif + +#ifndef min +#define min(a,b) ((a)<(b)?(a):(b)) +#endif + +#ifndef SLEN +/* Calculate length of a string constant */ +#define SLEN(s) (sizeof((s)) - 1) /* -1 for '\0' */ +#endif + +#ifndef UNUSED +#define UNUSED(x) ((x)=(x)) +#endif + +#endif -- cgit v1.2.3