From 602a04c4b58f14f8458f8de0b1a7a89ba51874ea Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 11 Dec 2012 04:07:06 +0100 Subject: [PATCH] factor out fequently used functions for ordinal numbers --- src/include/limits.h | 43 +++++++++++++++++++++++++++++++ src/lib/symbol-impl.cpp | 3 ++- src/lib/util.hpp | 57 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 95 insertions(+), 8 deletions(-) create mode 100644 src/include/limits.h diff --git a/src/include/limits.h b/src/include/limits.h new file mode 100644 index 000000000..9dd58f00a --- /dev/null +++ b/src/include/limits.h @@ -0,0 +1,43 @@ +/* + LIMITS.h - hard wired safety limits + + Copyright (C) Lumiera.org + 2012, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +/** @file limits.h + ** hard wired safety limits. + ** This tiny header defines some hard limits for indexing, counting, searching + ** and similar ordinal operations. These limits are a protection against ordinal + ** and index numbers insanely out-of dimension, like e.g. a symbolic ID with more + ** than 1000 characters. Whenever an actual allocation is based on such ordinal values, + ** a tighter and more specific limitation will be enforced on a case by case base + ** + ** @see symbol-impl.cpp + ** @see util::uNum + */ + + +#ifndef LUMIERA_LIMITS_H +#define LUMIERA_LIMITS_H + + +#define LUMIERA_IDSTRING_MAX_RELEVANT 1000 +#define LUMIERA_MAX_ORDINAL_NUMBER 1000 + +#endif /*LUMIERA_LIMITS_H*/ diff --git a/src/lib/symbol-impl.cpp b/src/lib/symbol-impl.cpp index 7edabf2a8..b9aade749 100644 --- a/src/lib/symbol-impl.cpp +++ b/src/lib/symbol-impl.cpp @@ -35,6 +35,7 @@ #include "lib/symbol.hpp" +#include "include/limits.h" extern "C" { #include "lib/safeclib.h" } @@ -50,7 +51,7 @@ using boost::hash_combine; namespace lib { - const size_t STRING_MAX_RELEVANT = 1000; + const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT; /** equality on Symbol values is defined diff --git a/src/lib/util.hpp b/src/lib/util.hpp index a629de195..55a04c443 100644 --- a/src/lib/util.hpp +++ b/src/lib/util.hpp @@ -24,6 +24,8 @@ #ifndef LIB_UTIL_H #define LIB_UTIL_H +#include "include/limits.h" + #include #include #include @@ -56,6 +58,54 @@ namespace util { return n1 < n2? N1(n2) : n1; } + /** cut a numeric value to be >=0 */ + template + inline NUM + noneg (NUM val) + { + return (0 + inline NUM + limited (NB lowerBound, NUM val, NB upperBound) + { + return min ( max (val, lowerBound) + , upperBound); + } + + /** positive integral number from textual representation + * @return always a number, 0 in case of unparseable text, + * limited to 0 <= num <= LUMIERA_MAX_ORDINAL_NUMBER */ + inline uint + uNum (const char* pCStr) + { + if (!pCStr) return 0; + int parsedNumber(std::atoi (pCStr)); + return limited (0, parsedNumber, LUMIERA_MAX_ORDINAL_NUMBER); + } + + inline int + sNum (const char* pCStr) + { + if (!pCStr) return 0; + int parsedNumber(std::atoi (pCStr)); + return limited (-LUMIERA_MAX_ORDINAL_NUMBER, parsedNumber, LUMIERA_MAX_ORDINAL_NUMBER); + } + + inline uint + uNum (string const& spec) + { + return uNum (spec.c_str()); + } + + inline int + sNum (string const& spec) + { + return sNum (spec.c_str()); + } + /* ======== generic empty check ========= */ @@ -90,13 +140,6 @@ namespace util { } - /** cut a numeric value to be >=0 */ - template - inline NUM - noneg (NUM val) - { - return (0