]> git.donarmstrong.com Git - flightcrew.git/blob - src/FlightCrew/Misc/Utilities.h
Imported Upstream version 0.7.2+dfsg
[flightcrew.git] / src / FlightCrew / Misc / Utilities.h
1 /************************************************************************\r
2 **\r
3 **  Copyright (C) 2010  Strahinja Markovic\r
4 **\r
5 **  This file is part of FlightCrew.\r
6 **\r
7 **  FlightCrew is free software: you can redistribute it and/or modify\r
8 **  it under the terms of the GNU Lesser General Public License as published\r
9 **  by the Free Software Foundation, either version 3 of the License, or\r
10 **  (at your option) any later version.\r
11 **\r
12 **  FlightCrew is distributed in the hope that it will be useful,\r
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 **  GNU Lesser General Public License for more details.\r
16 **\r
17 **  You should have received a copy of the GNU Lesser General Public License\r
18 **  along with FlightCrew.  If not, see <http://www.gnu.org/licenses/>.\r
19 **\r
20 *************************************************************************/\r
21 \r
22 #pragma once\r
23 #ifndef UTILITIES_H\r
24 #define UTILITIES_H\r
25 \r
26 #include <vector>\r
27 #include <string>\r
28 #include <algorithm>\r
29 #include <boost/unordered/unordered_set_fwd.hpp>\r
30 #include "Misc/BoostFilesystemUse.h"\r
31 #include "XercesHUse.h"\r
32 #include "Result.h"\r
33 \r
34 namespace FlightCrew\r
35 {\r
36 \r
37 namespace Util\r
38 {\r
39     std::string ReadUnicodFile( const fs::path &filepath );\r
40 \r
41     std::string GetFirstNumChars( const std::string &string, unsigned int num_chars );\r
42 \r
43     std::string GetFirstNumCharsFromFile( const fs::path &filepath, unsigned int num_chars );\r
44 \r
45     int LineOfCharIndex( const std::string &string, unsigned int char_index );\r
46 \r
47     boost::shared_ptr< xc::DOMDocument > LoadXmlDocument( const fs::path &filepath );\r
48 \r
49     boost::shared_ptr< xc::DOMDocument > LoadXhtmlDocument( const fs::path &filepath );\r
50 \r
51     std::string UrlDecode( const std::string &encoded_url );\r
52 \r
53     std::string GetUrlFragment( const std::string &decoded_url );\r
54 \r
55     std::string UrlWithoutFragment( const std::string &decoded_url );\r
56 \r
57     std::string UrlWithoutFileScheme( const std::string &decoded_url );\r
58 \r
59     fs::path NormalizePath( const fs::path &filepath );\r
60 \r
61     fs::path Utf8PathToBoostPath( const std::string &utf8_path );\r
62 \r
63     std::string BoostPathToUtf8Path( const fs::path &filepath );\r
64 \r
65     std::vector< Result > AddPathToResults( const std::vector< Result > &results, const fs::path &filepath );\r
66 \r
67     template< typename T >\r
68     void RemoveDuplicates( std::vector<T> &vector )\r
69     {\r
70         std::sort( vector.begin(), vector.end() );\r
71         vector.erase( std::unique( vector.begin(), vector.end() ), vector.end() );\r
72     }\r
73 \r
74     template< typename T >\r
75     bool Contains( const std::vector<T> &vector, const T &value )\r
76     {\r
77         return std::find( vector.begin(), vector.end(), value ) != vector.end(); \r
78     }\r
79 \r
80     template< typename T >\r
81     std::vector<T>& Extend( std::vector<T> &base_vector, const std::vector <T> &extension_vector ) \r
82     {\r
83             base_vector.insert( base_vector.end(), extension_vector.begin(), extension_vector.end() );\r
84             return base_vector;\r
85     }\r
86 \r
87     template< typename T >\r
88     std::vector<T>& SortedInPlace( std::vector<T> &vector ) \r
89     {\r
90         std::sort( vector.begin(), vector.end() );\r
91         return vector;\r
92     }\r
93 \r
94     // The STL algos for set union and intersection\r
95     // only work for sorted ranges, which boost::unordered_sets aren't.\r
96     template< typename T >\r
97     boost::unordered_set< T > SetIntersection( \r
98         const boost::unordered_set< T > &first,\r
99         const boost::unordered_set< T > &second )\r
100     {\r
101         boost::unordered_set< T > intersection;\r
102 \r
103         // We will iterate over the smaller set\r
104         // and check presence in the larger one\r
105         // for the sake of performance.\r
106         if ( second.size() < first.size() )\r
107         \r
108             return SetIntersection( second, first );\r
109 \r
110         for ( typename boost::unordered_set< T > ::const_iterator it = first.begin();\r
111             it != first.end();\r
112             ++it )\r
113         {\r
114             if ( second.find( *it ) != second.end() )\r
115                     \r
116                 intersection.insert( *it );\r
117         }\r
118 \r
119         return intersection;\r
120     }\r
121 \r
122     template< typename T >\r
123     boost::unordered_set< T > SetUnion( \r
124         const boost::unordered_set< T > &first,\r
125         const boost::unordered_set< T > &second )\r
126     {\r
127         boost::unordered_set< T > union_set;\r
128         union_set.insert( first .begin(), first .end() );\r
129         union_set.insert( second.begin(), second.end() );\r
130 \r
131         return union_set;\r
132     }\r
133 \r
134     template< typename T >\r
135     boost::unordered_set< T > SetSubtraction(\r
136         const boost::unordered_set< T > &first,\r
137         const boost::unordered_set< T > &second )\r
138     {\r
139         boost::unordered_set< T > subtracted;\r
140 \r
141         for ( typename boost::unordered_set< T >::const_iterator it = first.begin();\r
142             it != first.end();\r
143             ++it )\r
144         {\r
145             if ( second.find( *it ) == second.end() )\r
146                     \r
147                 subtracted.insert( *it );\r
148         }\r
149 \r
150         return subtracted;\r
151     }\r
152 \r
153 } // namespace Util\r
154 \r
155 } // namespace FlightCrew\r
156 \r
157 #endif // UTILITIES_H\r