Revision: 44679
http://brlcad.svn.sourceforge.net/brlcad/?rev=44679&view=rev
Author: davidloman
Date: 2011-05-25 12:27:06 +0000 (Wed, 25 May 2011)
Log Message:
-----------
Pull out commonly used, or otherwise generically useful functions into new
StringUtils class.
Added Paths:
-----------
geomcore/trunk/include/StringUtils.h
geomcore/trunk/src/utility/StringUtils.cxx
Added: geomcore/trunk/include/StringUtils.h
===================================================================
--- geomcore/trunk/include/StringUtils.h (rev 0)
+++ geomcore/trunk/include/StringUtils.h 2011-05-25 12:27:06 UTC (rev
44679)
@@ -0,0 +1,111 @@
+/* S T R I N G U T I L S . C X X
+ * BRL-CAD
+ *
+ * Copyright (c) 2011 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * 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 file; see the file named COPYING for more
+ * information.
+ */
+/** @file StringUtils.cxx
+ *
+ */
+#ifndef __STRINGUTILS_H__
+#define __STRINGUTILS_H__
+
+#include <string>
+#include <list>
+
+#define PATH_DELIM "/"
+#define DOUBLE_PATH_DELIM "//"
+
+class StringUtils {
+public:
+ virtual ~StringUtils();
+
+ //TODO swap out with bu_file_type()??
+ /**
+ * Checks the supplied path for existence and whether its a file or
directory.
+ * return values:
+ * 0 == does not exist
+ * 1 == exists and is a Directory
+ * 2 == exists and is a File
+ */
+ static const int isFileOrDir(const std::string path);
+
+ /**
+ * Splits the provided path 'path' into a std::list of path steps.
+ * Steps are determined by scanning for PATH_DELIM and splitting at each
instance.
+ *
+ * Returns the number of segments added to 'steps'
+ */
+ static const size_t splitPath(
+ const std::string path,
+ std::list<std::string>* steps);
+
+ /**
+ * Splits the provided string 'original' into a std::list of string segments.
+ * Segments are determined by scanning for 'delimiter' and splitting at each
instance.
+ *
+ * Returns the number of segments added to 'segments'
+ */
+ static const size_t splitString(
+ const std::string original,
+ std::list<std::string>* segments,
+ const std::string delimiter);
+
+ /**
+ * Combines the left and right paths into combined using PATH_DELIM
+ */
+ static void combinePaths(
+ std::string* combined,
+ const std::string* left,
+ const std::string* right);
+
+ /**
+ * Removes duplicate PATH_DELIM entries.
+ */
+ static void cleanString(std::string* out);
+
+ /**
+ * Walks 'path' and finds the first step that is a file. 'path' is then
split at that point
+ * into lPath and rPath respectively. If a valid int* is provided, fn will
set 'totalSteps'
+ * equal to the number of steps that are in 'path' Note that both rPath and
lPath will be
+ * reinitialized to empty strings in this fn.
+ *
+ * Return value is the step at which 'path' was split.
+ * If a path with no delimiters is passed in, this will return 0;
+ * If a NULL lPath or rPath is passed in, this will return -1;
+ */
+ static const int splitPathAtFile(
+ const std::string path,
+ std::string* lPath,
+ std::string* rPath,
+ int* totalSteps);
+
+
+private:
+ StringUtils();
+
+};
+
+#endif /* __STRINGUTILS_H__ */
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
Property changes on: geomcore/trunk/include/StringUtils.h
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Revision Date Author
Added: svn:eol-style
+ native
Added: geomcore/trunk/src/utility/StringUtils.cxx
===================================================================
--- geomcore/trunk/src/utility/StringUtils.cxx (rev 0)
+++ geomcore/trunk/src/utility/StringUtils.cxx 2011-05-25 12:27:06 UTC (rev
44679)
@@ -0,0 +1,175 @@
+/* S T R I N G U T I L S . C X X
+ * BRL-CAD
+ *
+ * Copyright (c) 2011 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * 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 file; see the file named COPYING for more
+ * information.
+ */
+/** @file StringUtils.cxx
+ *
+ */
+
+#include "StringUtils.h"
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <stdio.h>
+
+const int
+StringUtils::isFileOrDir(const std::string path)
+{
+ struct stat st_buf;
+
+ /* Check existence */
+ if ((stat(path.c_str(), &st_buf)) != 0)
+ return 0;
+
+ /* Check if dir */
+ if (S_ISDIR (st_buf.st_mode))
+ return 1;
+
+ /* Check if file */
+ if (S_ISREG (st_buf.st_mode))
+ return 2;
+
+ /* Shouldn't ever get here. */
+ return -1;
+}
+
+const size_t
+StringUtils::splitPath(const std::string path, std::list<std::string>* steps)
+{
+ return StringUtils::splitString(path, steps, PATH_DELIM);
+}
+
+const size_t
+StringUtils::splitString(const std::string original, std::list<std::string>*
segments, const std::string delimiter)
+{
+ std::string sub = "";
+ std::string copy = original;
+ size_t endPos = 0;
+ size_t cnt = 0;
+
+ do {
+ endPos = copy.find_first_of(delimiter);
+ if (endPos == std::string::npos){
+ if (copy.length() <=0)
+ break;
+ else
+ endPos = copy.length();
+ }
+ sub = copy.substr(0, endPos);
+
+ if (sub.length() > 0) {
+ segments->push_back(sub);
+ ++cnt;
+ }
+ copy = copy.erase(0, endPos+1);
+
+ } while (endPos != std::string::npos);
+
+ return cnt;
+}
+
+void
+StringUtils::combinePaths(
+ std::string* combined,
+ const std::string* left,
+ const std::string* right
+ )
+{
+ *combined = *left + PATH_DELIM + *right;
+ StringUtils::cleanString(combined);
+}
+
+void
+StringUtils::cleanString(std::string* out)
+{
+ int pos = out->find(DOUBLE_PATH_DELIM);
+ while (pos != -1)
+ {
+ out->replace(pos,2,PATH_DELIM);
+ pos = out->find(DOUBLE_PATH_DELIM);
+ }
+}
+
+const int
+StringUtils::splitPathAtFile(const std::string path, std::string* lPath,
std::string* rPath, int* totalSteps)
+{
+ std::string pathStep = "";
+ int step = 0;
+ int ford = 0;
+
+ if (lPath == NULL || rPath == NULL)
+ return -1;
+
+ (*lPath) = "";
+ (*rPath) = "";
+
+ std::list<std::string> strStack;
+ StringUtils::splitPath(path, &strStack);
+
+ if (totalSteps != NULL)
+ *totalSteps = strStack.size();
+
+ if (strStack.size() == 0)
+ return 0;
+
+ std::list<std::string>::const_iterator it = strStack.begin();
+
+ /* build FS string */
+ for (; it != strStack.end(); ++it)
+ {
+ pathStep = (std::string)*it;
+ *lPath += pathStep;
+ ford = StringUtils::isFileOrDir(lPath->c_str());
+
+ /* 2 == FILE, 1 == DIR, 0 == NOTEXIST */
+ if (ford == 2) {
+ ++step;
+ break;
+ } else if (ford == 1) {
+ ++step;
+ *lPath += PATH_DELIM;
+ continue;
+ } else if (ford == 0) {
+ return step * -1; /* Failed */
+ } else {
+ return step * -1; /* Failed */
+ }
+ }
+
+ ++it;
+
+ /* build FS string */
+ for (; it != strStack.end(); ++it)
+ {
+ pathStep = (std::string)*it;
+ if (it != strStack.end())
+ *rPath += PATH_DELIM;
+ *rPath += pathStep;
+
+ }
+ return step;
+}
+
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
Property changes on: geomcore/trunk/src/utility/StringUtils.cxx
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Revision Date Author
Added: svn:eol-style
+ native
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery,
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now.
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits