libyui  3.3.2
YPath.cc
1 /*
2  Copyright (c) 2012 Björn Esser
3 
4  Permission is hereby granted, free of charge, to any person obtaining
5  a copy of this software and associated documentation files (the
6  "Software"), to deal in the Software without restriction, including
7  without limitation the rights to use, copy, modify, merge, publish,
8  distribute, sublicense, and/or sell
9  copies of the Software, and to permit persons to whom the Software is
10  furnished to do so, subject to the following conditions:
11 
12  The above copyright notice and this permission notice shall be
13  included in all copies or substantial portions of the Software.
14 
15  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18  SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
19  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
21  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 
24 
25 /*-/
26 
27  File: YPath.cc
28 
29  Author: Björn Esser <bjoern.esser@gmail.com>
30 
31 /-*/
32 
33 #include <stdio.h>
34 #include <string.h>
35 #include <sstream>
36 #include <sys/types.h>
37 #include <dirent.h>
38 #include <vector>
39 
40 #include "YPath.h"
41 #include "YSettings.h"
42 #include "Libyui_config.h"
43 
44 #define YUILogComponent "ui"
45 #include "YUILog.h"
46 
47 YPath::YPath ( const std::string & directory, const std::string & filename )
48 {
49  yuiMilestone () << "Given filename: " << filename << std::endl;
50 
51  bool isThemeDir = ! directory.compare ( THEMEDIR );
52  std::string progSubDir = YSettings::progDir ();
53  std::string fullname = "";
54  std::string themeSubDir = "/current";
55  size_t splitPos = fullPath.rfind( "/" );
56  bool hasProgSubDir = progSubDir.compare ( "" );
57  bool hasSubDirPrepend = ( splitPos != std::string::npos );
58  std::string filenameNoPrepend = filename.substr ( splitPos + 1, std::string::npos );
59  std::string subDirPrepend = "";
60  std::vector<std::string> dirList;
61 
62  if ( hasSubDirPrepend )
63  subDirPrepend = filename.substr ( 0, splitPos );
64 
65  yuiMilestone () << "Preferring subdir: " << progSubDir << std::endl;
66  yuiMilestone () << "Subdir given with filename: " << subDirPrepend << std::endl;
67  yuiMilestone () << "Looking for: " << filenameNoPrepend << std::endl;
68 
69  if ( hasSubDirPrepend ) // prefer subdir prepended to filename
70  {
71  if ( isThemeDir ) // prefer /current inside THEMEDIR
72  {
73  if ( hasProgSubDir )
74  dirList.push_back ( directory + "/" + progSubDir + themeSubDir + "/" + subDirPrepend );
75 
76  dirList.push_back ( directory + themeSubDir + "/" + subDirPrepend );
77  }
78  if ( hasProgSubDir )
79  dirList.push_back ( directory + "/" + progSubDir + "/" + subDirPrepend );
80 
81  dirList.push_back ( directory + "/" + subDirPrepend );
82  }
83 
84  if ( isThemeDir ) // prefer /current inside THEMEDIR
85  {
86  if ( hasProgSubDir )
87  dirList.push_back ( directory + "/" + progSubDir + themeSubDir );
88 
89  dirList.push_back ( directory + themeSubDir );
90  }
91 
92  // the "usual" lookup
93  if ( hasProgSubDir )
94  dirList.push_back ( directory + "/" + progSubDir );
95 
96  dirList.push_back ( directory );
97 
98  for ( std::vector<std::string>::const_iterator x = dirList.begin () ; x != dirList.end () && fullPath.compare ( "" ) == 0 ; ++x )
99  {
100  std::vector<std::string> fileList = lsDir( *x );
101 
102  for ( std::vector<std::string>::const_iterator i = fileList.begin () ; i != fileList.end () && fullPath.compare ( "" ) == 0 ; ++i )
103  {
104  if ( *i != "." && *i != ".." ) // filter out parent and curdir
105  {
106  fullname = directory + "/" + *i;
107  if ( *i == filenameNoPrepend )
108  fullPath = fullname;
109  else
110  {
111  fullPath = lookRecursive ( fullname, filenameNoPrepend );
112  }
113  }
114  }
115  }
116 
117  if( fullPath.compare ( "" ) != 0 )
118  yuiMilestone() << "Found " << filenameNoPrepend << " in " << dir() << std::endl;
119  else
120  {
121  yuiMilestone() << "Could NOT find " << filename << " by looking recursive inside " << directory << std::endl;
122  fullPath = filename;
123  }
124 }
125 
127 {
128 }
129 
130 std::vector<std::string> YPath::lsDir( const std::string & directory )
131 {
132  std::vector<std::string> fileList;
133  DIR * dir;
134  struct dirent * ent;
135 
136  if ( ( dir = opendir( directory.c_str () ) ) != NULL )
137  {
138  yuiMilestone() << "Looking in " << directory << std::endl;
139 
140  while ( ( ent = readdir ( dir ) ) != NULL )
141  fileList.push_back ( ent -> d_name );
142 
143  closedir ( dir );
144  }
145 
146  return fileList;
147 }
148 
149 std::string YPath::lookRecursive( const std::string & directory, const std::string & filename )
150 {
151  std::vector<std::string> fileList = lsDir( directory );
152  std::string file = "";
153  std::string fullname;
154 
155  for ( std::vector<std::string>::const_iterator i = fileList.begin() ; i != fileList.end() && file.compare ( "" ) == 0 ; ++i )
156  {
157  if ( *i != "." && *i != ".." ) // filter out parent and curdir
158  {
159  fullname = directory + "/" + ( *i );
160  if ( *i == filename )
161  file = fullname;
162  else
163  {
164  file = lookRecursive ( fullname, filename );
165  }
166  }
167  }
168  return file;
169 }
170 
171 std::string YPath::path()
172 {
173  return fullPath;
174 }
175 
176 std::string YPath::dir()
177 {
178  return fullPath.substr ( 0, fullPath.rfind( "/" ) );
179 }
std::string path()
Returns the full path of the file if found; if not found just the filename given in constructor...
Definition: YPath.cc:171
~YPath()
Destructor.
Definition: YPath.cc:126
std::string dir()
Returns the directory where the file is found; if not found just the subdir part (if there&#39;s any) of ...
Definition: YPath.cc:176
YPath(const std::string &directory, const std::string &filename)
Constructor.
Definition: YPath.cc:47
static std::string progDir()
Returns the value of your program&#39;s subdir.
Definition: YSettings.cc:72