diff options
author | Patrick Williams <iawillia@us.ibm.com> | 2012-08-07 15:42:05 -0500 |
---|---|---|
committer | A. Patrick Williams III <iawillia@us.ibm.com> | 2012-08-15 12:27:55 -0500 |
commit | eedc0bb29e002418c53a13a96030a980a4f6b26b (patch) | |
tree | d09470895bb63ff41dc531049d095408d7bc93bc /src | |
parent | a346e4fa854de69dcd6a0ebfddf4c5530a175504 (diff) | |
download | talos-hostboot-eedc0bb29e002418c53a13a96030a980a4f6b26b.tar.gz talos-hostboot-eedc0bb29e002418c53a13a96030a980a4f6b26b.zip |
Additional needed utilities for PRD port.
Implement assorted utilities and STL templates for the
port of existing PRD code from FSP:
* strcat / strncat
* std::generate_n
* std::transform
* std::back_inserter
RTC: 44531
Change-Id: I9992f78222cbc2f74da15eea74c9b2cad602ad59
Reviewed-on: http://gfw160.austin.ibm.com:8080/gerrit/1501
Tested-by: Jenkins Server
Reviewed-by: Douglas R. Gilbert <dgilbert@us.ibm.com>
Reviewed-by: Christopher T. Phan <cphan@us.ibm.com>
Reviewed-by: A. Patrick Williams III <iawillia@us.ibm.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/include/algorithm | 115 | ||||
-rw-r--r-- | src/include/iterator | 125 | ||||
-rwxr-xr-x | src/include/string.h | 48 | ||||
-rw-r--r-- | src/lib/string.C | 83 | ||||
-rw-r--r-- | src/usr/testcore/lib/string.H | 91 |
5 files changed, 373 insertions, 89 deletions
diff --git a/src/include/algorithm b/src/include/algorithm index 7ee1106e6..b0d4d41c1 100644 --- a/src/include/algorithm +++ b/src/include/algorithm @@ -1,25 +1,26 @@ -// IBM_PROLOG_BEGIN_TAG -// This is an automatically generated prolog. -// -// $Source: src/include/algorithm $ -// -// IBM CONFIDENTIAL -// -// COPYRIGHT International Business Machines Corp. 2011 -// -// p1 -// -// Object Code Only (OCO) source materials -// Licensed Internal Code Source Materials -// IBM HostBoot Licensed Internal Code -// -// The source code for this program is not published or other- -// wise divested of its trade secrets, irrespective of what has -// been deposited with the U.S. Copyright Office. -// -// Origin: 30 -// -// IBM_PROLOG_END +/* IBM_PROLOG_BEGIN_TAG + * This is an automatically generated prolog. + * + * $Source: src/include/algorithm $ + * + * IBM CONFIDENTIAL + * + * COPYRIGHT International Business Machines Corp. 2011-2012 + * + * p1 + * + * Object Code Only (OCO) source materials + * Licensed Internal Code Source Materials + * IBM HostBoot Licensed Internal Code + * + * The source code for this program is not published or other- + * wise divested of its trade secrets, irrespective of what has + * been deposited with the U.S. Copyright Office. + * + * Origin: 30 + * + * IBM_PROLOG_END_TAG + */ #ifndef ALGORITHM #define ALGORITHM @@ -125,6 +126,24 @@ namespace std } /** + * Fill a sequence with a generated value + * @param[in] first OutputIterator to the first position in the sequence. + * @param[in] n number of elements in the sequence + * @param[in] gen functor to create values used to fill the sequence. + */ + template <typename OutputIterator, typename Size, typename Generator> + OutputIterator generate_n(OutputIterator first, Size n, Generator gen) + { + for(; n>0; --n) + { + *first = gen(); + ++first; + } + + return first; + } + + /** * Return the lesser of two arguments * @param[in] a object reference * @param[in] b object reference @@ -681,6 +700,58 @@ namespace std Util::__Util_QSort_Impl::sort(first, last, pred); } + /** Transform one sequence into another. + * + * Executes an operator against all elements in [first, last) and writes + * the result to another sequence. + * + * @param first - Beginning of the input range. + * @param last - Ending of the input range. + * @param result - Beginning of the output range. + * @param op - The transformation operator. + */ + template <typename InputIterator, typename OutputIterator, + typename UnaryFunction> + OutputIterator transform(InputIterator first, InputIterator last, + OutputIterator result, UnaryFunction op) + { + while (first != last) + { + *result = op(*first); + ++result; + ++first; + } + return result; + } + + /** Transform two sequences into another. + * + * Executes an operator against all elements in [first1, last1) along + * with the peer from [first2, ...) and writes the result to + * another sequence. + * + * @param first1 - Beginning of the first input range. + * @param last1 - Ending of the first input range. + * @param first2 - Beginning of the second input range. + * @param result - Beginning of the output range. + * @param op - The transformation operator. + */ + template <typename InputIterator1, typename InputIterator2, + typename OutputIterator, typename BinaryFunction> + OutputIterator transform(InputIterator1 first1, InputIterator1 last1, + InputIterator2 first2, OutputIterator result, + BinaryFunction op) + { + while (first1 != last1) + { + *result = op(*first1, *first2); + ++result; + ++first1; ++first2; + } + return result; + } + + }; #endif diff --git a/src/include/iterator b/src/include/iterator index dbe4b2baa..4a61f261e 100644 --- a/src/include/iterator +++ b/src/include/iterator @@ -1,26 +1,26 @@ -// IBM_PROLOG_BEGIN_TAG -// This is an automatically generated prolog. -// -// $Source: src/include/iterator $ -// -// IBM CONFIDENTIAL -// -// COPYRIGHT International Business Machines Corp. 2012 -// -// p1 -// -// Object Code Only (OCO) source materials -// Licensed Internal Code Source Materials -// IBM HostBoot Licensed Internal Code -// -// The source code for this program is not published or other- -// wise divested of its trade secrets, irrespective of what has -// been deposited with the U.S. Copyright Office. -// -// Origin: 30 -// -// IBM_PROLOG_END - +/* IBM_PROLOG_BEGIN_TAG + * This is an automatically generated prolog. + * + * $Source: src/include/iterator $ + * + * IBM CONFIDENTIAL + * + * COPYRIGHT International Business Machines Corp. 2012 + * + * p1 + * + * Object Code Only (OCO) source materials + * Licensed Internal Code Source Materials + * IBM HostBoot Licensed Internal Code + * + * The source code for this program is not published or other- + * wise divested of its trade secrets, irrespective of what has + * been deposited with the U.S. Copyright Office. + * + * Origin: 30 + * + * IBM_PROLOG_END_TAG + */ #ifndef __STL_ITERATOR #define __STL_ITERATOR @@ -100,6 +100,85 @@ typename iterator_traits<InputIterator>::difference_type (first, last); } +/** A OutputIterator which operates by push_back onto a container. + * + * See public std::back_insert_iterator documentation. + */ +template <typename BackInsertionSequence> +class back_insert_iterator +{ + public: + // Common iterator typedefs. + typedef typename BackInsertionSequence::value_type value_type; + typedef typename BackInsertionSequence::difference_type difference_type; + typedef typename BackInsertionSequence::pointer pointer; + typedef typename BackInsertionSequence::reference reference; + + /** Default constructor from a container reference. */ + back_insert_iterator(BackInsertionSequence& s) : sequence(s) {}; + /** Copy constructor. Reuses container reference. */ + back_insert_iterator(const back_insert_iterator& i) + : sequence(i.sequence) {}; + + /** Assignment (copy) operator. */ + back_insert_iterator& operator=(const back_insert_iterator& i) + { + sequence = i.sequence; + return *this; + } + + /** Dereference operator. + * + * This is used to make the standard pattern '*i = x' work on + * an iterator. Since we need to 'push_back' into the + * container we don't actually return anything except ourself, + * which allows the operator= to be called. + */ + back_insert_iterator& operator*() { return *this; } + + /** Assignment operator. + * + * This is the second part of the standard pattern '*i = x'. + * + * Adds the value to the container by calling push_back. + * + * @param[in] v - The value to insert to the container. + */ + back_insert_iterator& operator=(const value_type& v) + { + sequence.push_back(v); + return *this; + } + + /** Preincrement operator - no-op */ + back_insert_iterator& operator++() { return *this; }; + /** Postincrement operator - no-op */ + back_insert_iterator& operator++(int unused) { return *this; }; + + private: + /** The container to insert into. */ + BackInsertionSequence& sequence; +}; + +/** Create a back_insert_iterator from a container. + * + * Utility function to allow back_insert_iterators to be created without + * needing to specify the underlying container type. + * + * Example: Reverse copy elements from one vector into a new vector. + * copy(v.rbegin(), v.rend(), back_inserter(v2)); + * + * @param[in] s - Sequence to create an iterator for. + * + * @return The back_insert_iterator. + */ +template <typename BackInsertionSequence> +back_insert_iterator<BackInsertionSequence> + back_inserter(BackInsertionSequence& s) +{ + return back_insert_iterator<BackInsertionSequence>(s); +} + }; // namespace std. #endif diff --git a/src/include/string.h b/src/include/string.h index 529d161b3..50acecac1 100755 --- a/src/include/string.h +++ b/src/include/string.h @@ -1,25 +1,26 @@ -// IBM_PROLOG_BEGIN_TAG -// This is an automatically generated prolog. -// -// $Source: src/include/string.h $ -// -// IBM CONFIDENTIAL -// -// COPYRIGHT International Business Machines Corp. 2010 - 2011 -// -// p1 -// -// Object Code Only (OCO) source materials -// Licensed Internal Code Source Materials -// IBM HostBoot Licensed Internal Code -// -// The source code for this program is not published or other- -// wise divested of its trade secrets, irrespective of what has -// been deposited with the U.S. Copyright Office. -// -// Origin: 30 -// -// IBM_PROLOG_END +/* IBM_PROLOG_BEGIN_TAG + * This is an automatically generated prolog. + * + * $Source: src/include/string.h $ + * + * IBM CONFIDENTIAL + * + * COPYRIGHT International Business Machines Corp. 2010-2012 + * + * p1 + * + * Object Code Only (OCO) source materials + * Licensed Internal Code Source Materials + * IBM HostBoot Licensed Internal Code + * + * The source code for this program is not published or other- + * wise divested of its trade secrets, irrespective of what has + * been deposited with the U.S. Copyright Office. + * + * Origin: 30 + * + * IBM_PROLOG_END_TAG + */ #ifndef __STRING_H #define __STRING_H @@ -41,6 +42,9 @@ extern "C" int strcmp(const char* s1, const char* s2); size_t strlen(const char* s1); + char* strcat(char* d, const char* s); + char* strncat(char* d, const char* s, size_t n); + char* strchr(const char* s, int c); #ifdef __cplusplus diff --git a/src/lib/string.C b/src/lib/string.C index 0e1095c63..18cfec530 100644 --- a/src/lib/string.C +++ b/src/lib/string.C @@ -1,25 +1,26 @@ -// IBM_PROLOG_BEGIN_TAG -// This is an automatically generated prolog. -// -// $Source: src/lib/string.C $ -// -// IBM CONFIDENTIAL -// -// COPYRIGHT International Business Machines Corp. 2011 -// -// p1 -// -// Object Code Only (OCO) source materials -// Licensed Internal Code Source Materials -// IBM HostBoot Licensed Internal Code -// -// The source code for this program is not published or other- -// wise divested of its trade secrets, irrespective of what has -// been deposited with the U.S. Copyright Office. -// -// Origin: 30 -// -// IBM_PROLOG_END +/* IBM_PROLOG_BEGIN_TAG + * This is an automatically generated prolog. + * + * $Source: src/lib/string.C $ + * + * IBM CONFIDENTIAL + * + * COPYRIGHT International Business Machines Corp. 2011-2012 + * + * p1 + * + * Object Code Only (OCO) source materials + * Licensed Internal Code Source Materials + * IBM HostBoot Licensed Internal Code + * + * The source code for this program is not published or other- + * wise divested of its trade secrets, irrespective of what has + * been deposited with the U.S. Copyright Office. + * + * Origin: 30 + * + * IBM_PROLOG_END_TAG + */ #include <string.h> extern "C" void *memset(void *vdest, int ch, size_t len) @@ -201,6 +202,44 @@ extern "C" size_t strlen(const char* a) return length; } +extern "C" char* strcat(char* d, const char* s) +{ + char* _d = d; + while(*_d) + { + _d++; + } + + while(*s) + { + *_d = *s; + _d++; s++; + } + *_d = '\0'; + + return d; +} + +extern "C" char* strncat(char* d, const char* s, size_t n) +{ + char* _d = d; + while(*_d) + { + _d++; + } + + while((*s) && (0 != n)) + { + *_d = *s; + _d++; s++; + n--; + } + *_d = '\0'; + + return d; +} + + extern "C" char* strchr(const char* s, int c) { while((*s != '\0') && (*s != c)) diff --git a/src/usr/testcore/lib/string.H b/src/usr/testcore/lib/string.H new file mode 100644 index 000000000..fb6906166 --- /dev/null +++ b/src/usr/testcore/lib/string.H @@ -0,0 +1,91 @@ +/* IBM_PROLOG_BEGIN_TAG + * This is an automatically generated prolog. + * + * $Source: src/usr/testcore/lib/string.H $ + * + * IBM CONFIDENTIAL + * + * COPYRIGHT International Business Machines Corp. 2012 + * + * p1 + * + * Object Code Only (OCO) source materials + * Licensed Internal Code Source Materials + * IBM HostBoot Licensed Internal Code + * + * The source code for this program is not published or other- + * wise divested of its trade secrets, irrespective of what has + * been deposited with the U.S. Copyright Office. + * + * Origin: 30 + * + * IBM_PROLOG_END_TAG + */ +#ifndef __TESTCORE_LIB_STRING_H +#define __TESTCORE_LIB_STRING_H + +#include <string.h> + +/** @file string.H + * @brief Test cases for the string.h system library header. + */ + +class StringTest : public CxxTest::TestSuite +{ + private: + static const char * string1; + static const char * string2; + + public: + /** + * Tests for strcat. + */ + void testStrcat() + { + char a[64]; + + size_t len1 = strlen(string1); + + strcpy(a, string1); + strcat(a, string2); + + if (0 != memcmp(a, string1, len1)) + { + TS_FAIL("strcat: String1 contents not copied properly."); + } + if (0 != strcmp(&a[len1], string2)) + { + TS_FAIL("strcat: String2 contents not cat'd properly."); + } + } + + /** + * Tests for strncat. + */ + void testStrncat() + { + char a[64]; + memset(a, '\0', sizeof(a)); + + size_t len1 = strlen(string1); + + strcpy(a, string1); + strncat(a, string2, 7); + + if (0 != memcmp(a, string1, len1)) + { + TS_FAIL("strncat: String1 contents not copied properly."); + } + if (0 != strcmp(&a[len1], "Another")) + { + TS_FAIL("strncat: String2 contents not cat'd properly."); + } + + } +}; + +const char * StringTest::string1 = "This is a string."; +const char * StringTest::string2 = "Another string am I."; + + +#endif |