include/EDoc/utils.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 by Brendon Costa
00004 
00005    This library is free software; you can redistribute it and/or modify 
00006    it under the terms of the "LGPL Like" License defined in the file COPYING 
00007    that should have been distributed along with this source.
00008 
00009    This library is distributed in the hope that it will be useful, 
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013    You should have received a copy of the "LGPL Like" License 
00014    along with this library; see the file COPYING. if not, it can be 
00015    obtained from the EDoc++ website: 
00016    http://edoc.sourceforge.net/license.html
00017 
00018 *******************************************************************************/
00019 #ifndef EDOC_UTILS_H
00020 #define EDOC_UTILS_H
00021 
00022 #include <string>
00023 #include <vector>
00024 #include <list>
00025 
00026 /** \file utils.h This file provides some useful utilities that make using 
00027  * the STL containers a little simpler throughout the EDoc library.
00028  */
00029  
00030 //==============================================================================
00031 /** \brief A macro that can be used to loop for each element of a stl container.
00032  *
00033  * May be used like:
00034  * \code
00035  * typedef std::map<int, Blah> BlahMap;
00036  * BlahMap mymap;
00037  * ...
00038  * EDOC_FOREACH(BlahMap, it, mymap)
00039  * {
00040  *    ... do stuff with it->second ...
00041  * }
00042  * \endcode
00043  *
00044  * Important to note is that the typedef above is necessary in the EDOC_FOREACH
00045  * otherwise the macro will assume the comma in the map type is a sperator for
00046  * the macro. It is not necessary however for use with std::list and std::vector
00047  */
00048 #define EDOC_FOREACH(ContainerType, IteratorName, ContainerObject) \
00049    for(ContainerType::iterator IteratorName = (ContainerObject).begin(); \
00050       IteratorName != (ContainerObject).end(); ++IteratorName)
00051 
00052 //==============================================================================
00053 /** \brief A macro that can be used to loop for each element of a const stl
00054  * container.
00055  */
00056 #define EDOC_FOREACH_CONST(ContainerType, IteratorName, ContainerObject) \
00057    for(ContainerType::const_iterator IteratorName = (ContainerObject).begin(); \
00058       IteratorName != (ContainerObject).end(); ++IteratorName)
00059 
00060 //==============================================================================
00061 /** \brief A macro for marking function parameters as unused.
00062  *
00063  * This is used to eliminate the GCC warning for unused parameters when
00064  * compiled with that option.
00065  */
00066 #define EDOC_UNUSED(Param)
00067 
00068 //==============================================================================
00069 
00070 namespace EDoc
00071 {
00072    class Type;
00073 
00074    //---------------------------------------------------------------------------
00075    /** \brief used in a number of places within the Print() functions to provide
00076     * indentation.
00077     */
00078    static char const* const INDENT_STR = "\t";
00079 
00080    //---------------------------------------------------------------------------
00081    /** \brief Returns the extension of the given file name.
00082     *
00083     * I.e.: Thing.txt would return "txt"
00084     */
00085    std::string GetFileExtension(std::string name);
00086 
00087    //---------------------------------------------------------------------------
00088    /** \brief Returns both the extension and the base name of the given 
00089     * file name.
00090     *
00091     * I.e.: Thing.txt would return "Thing" and "txt"
00092     */
00093    std::pair<std::string, std::string> SplitFileExtension(std::string name);
00094 
00095    //---------------------------------------------------------------------------
00096    /** \brief Creates a new temporary file.
00097     *
00098     * Depending upon implementation this function may use the given base string
00099     * to construct the temporary file name. 
00100     */
00101    std::string CreateTempFilename(std::string base="EDoctmp");
00102 
00103    //---------------------------------------------------------------------------
00104    /** \brief Returns true if the given STL container contains an item.
00105     */
00106    template <typename T>
00107    inline bool ListContains(const T& cont, typename T::const_reference item)
00108    {
00109       EDOC_FOREACH_CONST(typename T, it, cont)
00110       {
00111          if (*it == item)
00112          {
00113             return true;
00114          }
00115       }
00116       
00117       return false;
00118    }
00119 
00120    //---------------------------------------------------------------------------
00121    /** \brief Returns an iterator for the item that matches.
00122     *
00123     * Most containers in STL have a .find() function. But apparently not
00124     * std::vector.
00125     */
00126    template <typename T>
00127    inline typename T::iterator VecFind(T& cont, typename T::const_reference item)
00128    {
00129       EDOC_FOREACH(typename T, it, cont)
00130       {
00131          if ((*it) == item)
00132          {
00133             return it;
00134          }
00135       }
00136       return cont.end();
00137    }
00138 
00139    //---------------------------------------------------------------------------
00140    /** \brief Pushes item to the back of the STL container if it does not 
00141     * already exist in the container.
00142     */
00143    template <typename T>
00144    inline void PushBackUnique(T& cont, typename T::const_reference item)
00145    {
00146       if (ListContains(cont, item))
00147       {
00148          return;
00149       }
00150       cont.push_back(item);
00151    }
00152 
00153    //---------------------------------------------------------------------------
00154    /** \brief Appends items from one STL container to the back of a seccond 
00155     * STL container if the items are not already in the second container.
00156     */
00157    template<typename T>
00158    inline void AppendUnique(T& cont1, const T& cont2)
00159    {
00160       EDOC_FOREACH_CONST(typename T, it, cont2)
00161       {
00162          PushBackUnique(cont1, *it);
00163       }
00164    }
00165 
00166    //---------------------------------------------------------------------------
00167    /** \brief Appends contents of STL container cont2 to the end of cont1.
00168     */
00169    template<typename T>
00170    inline T& Append(T& cont1, const T& cont2)
00171    {
00172       cont1.insert(cont1.end(), cont2.begin(), cont2.end());
00173       return cont1;
00174    }
00175 
00176    //---------------------------------------------------------------------------
00177    /** \brief Erases all items from the given container.
00178     */
00179    template<typename T>
00180    inline void Erase(T& cont)
00181    {
00182       cont.erase(cont.begin(), cont.end());
00183    }
00184 
00185    //---------------------------------------------------------------------------
00186    /** \brief Splits the given string into a vector of strings using sep as a
00187     * seperator.
00188     */
00189    std::vector<std::string> Split(std::string str, char sep = ' ');
00190 
00191    //---------------------------------------------------------------------------
00192    /** \brief Executes the given command and returns its exit status.
00193     */
00194    int RunProgram(std::string command_in);
00195 
00196    //---------------------------------------------------------------------------
00197    /** \brief Executes the given command and returns its exit status.
00198     */
00199    int RunProgramRedirect(std::string& out, std::string& err, std::string command);
00200 
00201    //---------------------------------------------------------------------------
00202    /** \brief Creates a directory structure if it does not exist.
00203     *
00204     * This is like mkdir -p
00205     */
00206    void MkDirs(std::string dir);
00207 
00208    //---------------------------------------------------------------------------
00209    /** \brief Copies source file to destination file.
00210     */
00211    void CopyFile(std::string source, std::string destination);
00212 
00213    //---------------------------------------------------------------------------
00214    /** \brief Returns true if the specified filename exists as a file.
00215     */
00216    bool FileExists(std::string filename);
00217 
00218    //---------------------------------------------------------------------------
00219    /** \brief Trims all whitespace from the beginning and end of the string.
00220     */
00221    std::string TrimWhitespace(const std::string& str);
00222 
00223    //---------------------------------------------------------------------------
00224    /** \brief Creates a temporary file on the system using the given template.
00225     */
00226    std::string MakeTemporaryFile(std::string prefix="", std::string suffix="", std::string directory="/tmp");
00227 
00228    //---------------------------------------------------------------------------
00229 
00230 }
00231 
00232 #endif // EDOC_UTILS_H

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1