-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #586 from nomis52/master
Refactor string functions, fix many defects.
- Loading branch information
Showing
41 changed files
with
654 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* Format.cpp | ||
* Formatting functions for basic types. | ||
* Copyright (C) 2005 Simon Newton | ||
*/ | ||
|
||
#include "ola/strings/Format.h" | ||
|
||
#include <iomanip> | ||
#include <sstream> | ||
#include <string> | ||
|
||
namespace ola { | ||
namespace strings { | ||
|
||
using std::endl; | ||
using std::ostringstream; | ||
using std::string; | ||
|
||
string IntToString(int i) { | ||
ostringstream str; | ||
str << i; | ||
return str.str(); | ||
} | ||
|
||
string IntToString(unsigned int i) { | ||
ostringstream str; | ||
str << i; | ||
return str.str(); | ||
} | ||
|
||
void FormatData(std::ostream *out, | ||
const uint8_t *data, | ||
unsigned int length, | ||
unsigned int indent, | ||
unsigned int byte_per_line) { | ||
ostringstream raw, ascii; | ||
raw << std::hex; | ||
for (unsigned int i = 0; i != length; i++) { | ||
raw << std::setfill('0') << std::setw(2) | ||
<< static_cast<unsigned int>(data[i]) << " "; | ||
if (isprint(data[i])) { | ||
ascii << data[i]; | ||
} else { | ||
ascii << "."; | ||
} | ||
|
||
if (i % byte_per_line == byte_per_line - 1) { | ||
*out << string(indent, ' ') << raw.str() << " " << ascii.str() << endl; | ||
raw.str(""); | ||
ascii.str(""); | ||
} | ||
} | ||
if (length % byte_per_line != 0) { | ||
// pad if needed | ||
raw << string(3 * (byte_per_line - (length % byte_per_line)), ' '); | ||
*out << string(indent, ' ') << raw.str() << " " << ascii.str() << endl; | ||
} | ||
} | ||
} // namespace strings | ||
} // namespace ola |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# LIBRARIES | ||
################################################## | ||
common_libolacommon_la_SOURCES += \ | ||
common/strings/Format.cpp \ | ||
common/strings/Utils.cpp | ||
|
||
# TESTS | ||
################################################ | ||
test_programs += common/strings/UtilsTester | ||
|
||
common_strings_UtilsTester_SOURCES = \ | ||
common/strings/UtilsTest.cpp | ||
common_strings_UtilsTester_CXXFLAGS = $(COMMON_TESTING_FLAGS) | ||
common_strings_UtilsTester_LDADD = $(COMMON_TESTING_LIBS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* Utils.cpp | ||
* Miscellaneous string functions. | ||
* Copyright (C) 2014 Simon Newton | ||
*/ | ||
|
||
#include "ola/strings/Utils.h" | ||
|
||
#include <string.h> | ||
#include <string> | ||
|
||
namespace ola { | ||
namespace strings { | ||
|
||
using std::string; | ||
|
||
void CopyToFixedLengthBuffer(const std::string &input, | ||
char *buffer, | ||
unsigned int size) { | ||
// buffer may not be NULL terminated. | ||
// coverity(BUFFER_SIZE_WARNING) | ||
strncpy(buffer, input.c_str(), size); | ||
} | ||
} // namespace strings | ||
} // namespace ola |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* UtilsTest.cpp | ||
* Unittest for strings Util functions. | ||
* Copyright (C) 2014 Simon Newton | ||
*/ | ||
|
||
#include <cppunit/extensions/HelperMacros.h> | ||
#include <string> | ||
|
||
#include "ola/base/Array.h" | ||
#include "ola/strings/Utils.h" | ||
#include "ola/testing/TestUtils.h" | ||
|
||
using ola::strings::CopyToFixedLengthBuffer; | ||
using std::string; | ||
|
||
class UtilsTest: public CppUnit::TestFixture { | ||
CPPUNIT_TEST_SUITE(UtilsTest); | ||
CPPUNIT_TEST(testCopyToFixedLengthBuffer); | ||
CPPUNIT_TEST_SUITE_END(); | ||
|
||
public: | ||
void testCopyToFixedLengthBuffer(); | ||
}; | ||
|
||
|
||
CPPUNIT_TEST_SUITE_REGISTRATION(UtilsTest); | ||
|
||
/* | ||
* Test the CopyToFixedLengthBuffer function | ||
*/ | ||
void UtilsTest::testCopyToFixedLengthBuffer() { | ||
char buffer[6]; | ||
const string short_input("foo"); | ||
const string input("foobar"); | ||
const string oversized_input("foobarbaz"); | ||
|
||
const char short_output[] = {'f', 'o', 'o', 0, 0, 0}; | ||
CopyToFixedLengthBuffer(short_input, buffer, arraysize(buffer)); | ||
OLA_ASSERT_DATA_EQUALS(short_output, arraysize(short_output), | ||
buffer, arraysize(buffer)); | ||
|
||
const char output[] = {'f', 'o', 'o', 'b', 'a', 'r'}; | ||
CopyToFixedLengthBuffer(input, buffer, arraysize(buffer)); | ||
OLA_ASSERT_DATA_EQUALS(output, arraysize(output), | ||
buffer, arraysize(buffer)); | ||
|
||
const char oversized_output[] = {'f', 'o', 'o', 'b', 'a', 'r'}; | ||
CopyToFixedLengthBuffer(oversized_input, buffer, arraysize(buffer)); | ||
OLA_ASSERT_DATA_EQUALS(oversized_output, arraysize(oversized_output), | ||
buffer, arraysize(buffer)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.