**************************************************************************/
/*
- * Path manipulation.
+ * String manipulation.
*/
-#ifndef _OS_PATH_HPP_
-#define _OS_PATH_HPP_
+#ifndef _OS_STRING_HPP_
+#define _OS_STRING_HPP_
#include <assert.h>
namespace os {
-class Path {
+/**
+ * Vector based zero-terminate string, suitable for passing strings or paths
+ * to/from OS calls.
+ */
+class String {
protected:
typedef std::vector<char> Buffer;
+
+ /**
+ * The buffer's last element is always the '\0' character, therefore the
+ * buffer must never be empty.
+ */
Buffer buffer;
Buffer::iterator find(char c) {
Buffer::iterator it = buffer.begin();
- /* Why do we make these functions fail on empty paths? */
assert(it != buffer.end());
while (it != buffer.end()) {
if (*it == c) {
Buffer::iterator rfind(char c) {
Buffer::iterator it = buffer.end();
- assert(it != buffer.begin());
- --it; // skip null
while (it != buffer.begin()) {
--it;
if (*it == c) {
return buffer.end();
}
- Path(size_t size) :
+ String(size_t size) :
buffer(size) {
}
}
public:
- Path() {
+
+ /*
+ * Constructors
+ */
+
+ String() {
buffer.push_back(0);
}
- Path(const char *s) :
+ String(const char *s) :
buffer(s, s + strlen(s) + 1)
{}
+ String(const String &other) :
+ buffer(other.buffer)
+ {}
+
template <class InputIterator>
- Path(InputIterator first, InputIterator last) :
+ String(InputIterator first, InputIterator last) :
buffer(first, last)
{
buffer.push_back(0);
}
+ /**
+ * From a printf-like format string
+ */
+ static String
+ format(const char *format, ...)
+#ifdef __GNUC__
+ __attribute__ ((format (printf, 1, 2)))
+#endif
+ {
+
+ va_list args;
+
+ va_start(args, format);
+
+ int length;
+ va_list args_copy;
+ va_copy(args_copy, args);
+#ifdef _WIN32
+ /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
+ * if the number of characters to write is greater than count.
+ */
+ length = _vscprintf(format, args_copy);
+#else
+ char dummy;
+ length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
+#endif
+ va_end(args_copy);
+
+ assert(length >= 0);
+ size_t size = length + 1;
+
+ String path(size);
+
+ va_start(args, format);
+ vsnprintf(path.buf(), size, format, args);
+ va_end(args);
+
+ return path;
+ }
+
+ /*
+ * Conversion to ordinary C strings.
+ */
+
+ const char *str(void) const {
+ assert(buffer.back() == 0);
+ return &buffer[0];
+ }
+
+ operator const char *(void) const {
+ return str();
+ }
+
+ /*
+ * Iterators
+ */
+
+ typedef Buffer::const_iterator const_iterator;
+ typedef Buffer::iterator iterator;
+
+ const_iterator begin(void) const {
+ return buffer.begin();
+ }
+
+ iterator begin(void) {
+ return buffer.begin();
+ }
+
+ const_iterator end(void) const {
+ const_iterator it = buffer.end();
+ assert(it != buffer.begin());
+ --it; // skip null
+ return it;
+ }
+
+ iterator end(void) {
+ iterator it = buffer.end();
+ assert(it != buffer.begin());
+ --it; // skip null
+ return it;
+ }
+
+ /*
+ * Operations
+ */
+
+ void insert(iterator position, char c) {
+ buffer.insert(position, c);
+ }
+
+ template <class InputIterator>
+ void insert(iterator position, InputIterator first, InputIterator last) {
+ buffer.insert(position, first, last);
+ }
+
+ void insert(iterator position, const char *s) {
+ assert(s);
+ insert(position, s, s + strlen(s));
+ }
+
+ void insert(iterator position, const String & other) {
+ insert(position, other.begin(), other.end());
+ }
+
+ void append(char c) {
+ insert(end(), c);
+ }
+
+ template <class InputIterator>
+ void append(InputIterator first, InputIterator last) {
+ insert(end(), first, last);
+ }
+
+ void append(const char *s) {
+ insert(end(), s);
+ }
+
+ void append(const String & other) {
+ insert(end(), other);
+ }
+
char *buf(size_t size) {
buffer.resize(size);
return &buffer[0];
}
+ size_t length(void) const {
+ size_t size = buffer.size();
+ assert(size > 0);
+ assert(buffer[size - 1] == 0);
+ return size - 1;
+ }
+
+ void truncate(size_t length) {
+ assert(length < buffer.size());
+ buffer[length] = 0;
+ buffer.resize(length + 1);
+ }
+
+ void truncate(void) {
+ truncate(strlen(str()));
+ }
+
+
+ /*
+ * String manipulation
+ */
+
+ bool
+ exists(void) const;
+
void trimDirectory(void) {
- Buffer::iterator sep = rfind(OS_DIR_SEP);
+ iterator sep = rfind(OS_DIR_SEP);
if (sep != buffer.end()) {
buffer.erase(buffer.begin(), sep + 1);
}
* 3. A path of just the root directory is unchaged.
*/
void trimFilename(void) {
- Buffer::iterator first = find(OS_DIR_SEP);
- Buffer::iterator last = rfind(OS_DIR_SEP);
+ iterator first = find(OS_DIR_SEP);
+ iterator last = rfind(OS_DIR_SEP);
if (last == buffer.end()) {
return;
}
if (last == first) {
- buffer.erase(first + 1, buffer.end() - 1);
+ buffer.erase(first + 1, end());
} else {
- buffer.erase(last, buffer.end() - 1);
+ buffer.erase(last, end());
}
}
void trimExtension(void) {
- Buffer::iterator dot = rfind('.');
+ iterator dot = rfind('.');
if (dot != buffer.end()) {
- buffer.erase(dot, buffer.end() - 1);
+ buffer.erase(dot, end());
}
}
- size_t length(void) const {
- size_t size = buffer.size();
- assert(size > 0);
- assert(buffer[size - 1] == 0);
- return size - 1;
- }
-
- void truncate(size_t length) {
- assert(length < buffer.size());
- buffer[length] = 0;
- buffer.resize(length + 1);
- }
-
- void truncate(void) {
- truncate(strlen(str()));
- }
-
- const char *str(void) const {
- assert(buffer[buffer.size() - 1] == 0);
- return &buffer[0];
- }
-
- operator const char *(void) const {
- return str();
- }
-
- void join(const Path & other) {
- size_t len = length();
- if (len > 0 && buffer[len - 1] != OS_DIR_SEP) {
- buffer.insert(buffer.begin() + len++, OS_DIR_SEP);
+ void join(const String & other) {
+ if (length() && end()[-1] != OS_DIR_SEP) {
+ append(OS_DIR_SEP);
}
- buffer.insert(buffer.begin() + len, other.buffer.begin(), other.buffer.end() - 1);
- }
-
- /**
- * Create a path from a printf-like format string
- */
- static Path
- format(const char *format, ...)
-#ifdef __GNUC__
- __attribute__ ((format (printf, 1, 2)))
-#endif
- {
-
- va_list args;
-
- va_start(args, format);
-
- int length;
- va_list args_copy;
- va_copy(args_copy, args);
-#ifdef _WIN32
- /* We need to use _vcsprintf to calculate the length as vsnprintf returns -1
- * if the number of characters to write is greater than count.
- */
- length = _vscprintf(format, args_copy);
-#else
- char dummy;
- length = vsnprintf(&dummy, sizeof dummy, format, args_copy);
-#endif
- va_end(args_copy);
-
- assert(length >= 0);
- size_t size = length + 1;
-
- Path path(size);
-
- va_start(args, format);
- vsnprintf(path.buf(), size, format, args);
- va_end(args);
-
- return path;
+ append(other.begin(), other.end());
}
-
- bool exists(void) const;
};
-Path getProcessName();
-Path getCurrentDir();
+String getProcessName();
+String getCurrentDir();
} /* namespace os */
-#endif /* _OS_PATH_HPP_ */
+#endif /* _OS_STRING_HPP_ */