LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
FileUtils.cpp
Go to the documentation of this file.
1// Copyright 2015 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 * @file FileUtils.h
8 * @author Gael de Chalendar
9 * @date Tue Jul 7 2015
10 * copyright Copyright (C) 2015 by CEA LIST
11 ***********************************************************************/
12
13#include "FileUtils.h"
14
15#include <QFileInfo>
16#include <QDir>
17#include <QStandardPaths>
18
19namespace Lima {
20namespace Common {
21namespace Misc {
22
23uint64_t countLines(std::istream& file)
24{
25 uint64_t result = 0;
26 std::streampos initialPosition = file.tellg();
27 int c = file.get();
28 while (c != -1)
29 {
30 while (c != -1 && c != '\n')
31 {
32 c = file.get();
33 }
34 result = result + 1;
35 c = file.get();
36 }
37 file.clear();
38 file.seekg(initialPosition, std::ios_base::beg);
39 return result;
40}
41
42uint64_t countLines(QFile& file)
43{
44 uint64_t result = 0;
45 qint64 initialPosition = file.pos();
46 char c = '\0';
47 while (!file.atEnd())
48 {
49 while (!file.atEnd() && c != '\n')
50 {
51 file.getChar(&c);
52 }
53 result = result + 1;
54 file.getChar(&c);
55 }
56 file.seek(initialPosition);
57 return result;
58}
59
60// Appends paths from source to target if following conditions are met:
61// - path isn't empty
62// - path isn't appended previously
63// - path exists
64void appendNonEmptyDirs(QStringList& target, const QStringList& source)
65{
66 for (const auto& path: source)
67 {
68 if ( !path.isEmpty() && !target.contains(path) && QDir(path).exists() )
69 {
70 target << path;
71 }
72 }
73}
74
75QString getEnvVar(const QString& name)
76{
77 return QString::fromUtf8(qgetenv((name.toUpper()).toStdString().c_str()).constData());
78}
79
80QString getDataHome()
81{
82 QString dataHome = QString::fromUtf8(qgetenv("XDG_DATA_HOME"));
83 if (dataHome.isEmpty())
84 {
85 dataHome = QString::fromUtf8(qgetenv("HOME"));
86 if (!dataHome.isEmpty())
87 {
88 dataHome += "/.local/share/";
89 }
90 }
91 return dataHome;
92}
93
94// Appends path in case it exists. Otherwise appends alt_path
95void appendFirstOrSecond(QStringList& target, const QString& path, const QString& alt_path)
96{
97 if ( QDir( path ).exists() )
98 {
99 appendNonEmptyDirs(target, QStringList(path));
100 }
101 else
102 {
103 appendNonEmptyDirs(target, QStringList(alt_path));
104 }
105}
106
107// Calls appendFirstOrSecond for given project
108void appendFirstOrSecondForProject(QStringList& target,
109 const QString& project,
110 const QString& path_prefix,
111 const QString& path_postfix = QString(""))
112{
113 appendFirstOrSecond(target,
114 getEnvVar(project + "_DIST") + path_prefix + project + path_postfix,
115 QString::fromUtf8("/usr/") + path_prefix + project + path_postfix);
116}
117
118void buildDirectoriesListForProject(QStringList& target,
119 const QString& project,
120 const QString& env_postfix, // "_CONF" or "_RESOURCES"
121 const QString& path_prefix, // "/share/config/" or "/share/apps"
122 const QString& path_postfix = QString("") // "" or "/resources
123 )
124{
125 QStringList dirs;
126 QString var = getEnvVar(project + env_postfix);
127 if ( !var.isEmpty() )
128 {
129 dirs << var.split(LIMA_PATH_SEPARATOR);
130 }
131
132 appendNonEmptyDirs(target, dirs);
133
134 if ( dirs.isEmpty() )
135 {
136 appendFirstOrSecondForProject(target, project, path_prefix, path_postfix);
137 }
138
139}
140
141QStringList buildConfigurationDirectoriesList(const QStringList& projects,
142 const QStringList& paths)
143{
144 // qDebug() << "buildConfigurationDirectoriesList" << projects << paths;
145 QStringList configDirs;
146
147 // 1. Add the paths explicitly given
148 appendNonEmptyDirs(configDirs, paths);
149
150 // 2. Add dirs from LIMA_CONF if no project is given
151 if (projects.empty())
152 {
153 QString var = getEnvVar(QString::fromStdString("lima") + "_CONF");
154 if ( !var.isEmpty() )
155 {
156 configDirs << var.split(LIMA_PATH_SEPARATOR);
157 }
158 }
159 else
160 {
161 // 3. Add dirs from each PROJECT_CONF
162 for (const auto& project: projects)
163 {
164 QString var = getEnvVar(project.toUpper() + "_CONF");
165 if ( !var.isEmpty() )
166 {
167 configDirs << var.split(LIMA_PATH_SEPARATOR);
168 }
169 }
170 }
171 // 4. Add conf dir in XDG_DATA_HOME or ~/.local/share/ after LIMA_CONF but before /usr
172 QString dataHome = getDataHome();
173 appendNonEmptyDirs(configDirs, QStringList(dataHome + "/lima/config/"));
174
175 // 5. Then add the *_CONF for each given project and complete if necessary with *_DIST/… or /usr/…
176 if ( configDirs.isEmpty() )
177 {
178 for (const auto& project: projects)
179 {
180 appendFirstOrSecondForProject(configDirs, project, "/share/config/", "");
181 }
182 }
183
184 QString var = getEnvVar(QString::fromStdString("LIMA_SHOW_CONFIG_PATH"));
185 if ( !var.isEmpty() )
186 {
187 qDebug() << "LIMA Configuration directories list built is:" << configDirs;
188 }
189
190 return configDirs;
191}
192
193QStringList buildResourcesDirectoriesList(const QStringList& projects,
194 const QStringList& paths)
195{
196 // qDebug() << "buildResourcesDirectoriesList" << projects << paths;
197 QStringList resourcesDirs;
198 QString dataHome = getDataHome();
199
200 appendNonEmptyDirs(resourcesDirs, QStringList(dataHome + "/lima/resources/"));
201
202 for (const auto& project: projects)
203 {
204 buildDirectoriesListForProject(resourcesDirs, project, "_RESOURCES",
205 "/share/apps/", "/resources");
206 }
207
208 appendNonEmptyDirs(resourcesDirs, paths);
209
210 QString var = getEnvVar(QString::fromStdString("LIMA_SHOW_CONFIG_PATH"));
211 if ( !var.isEmpty() )
212 {
213 qDebug() << "LIMA Resources directories list built is:" << resourcesDirs;
214 }
216 LINFO << "LIMA Resources directories list built is:" << resourcesDirs;
217 return resourcesDirs;
218}
219
220QString findFileInPaths(const QString& paths, const QString& fileName, const QChar& separator)
221{
222 auto pathsList = paths.split(separator);
223 auto filesList =fileName.split(separator);
224 for (const auto& path: pathsList)
225 {
226 for (const auto& file: filesList)
227 {
228 if (QFileInfo::exists(path + "/" + file))
229 {
230 #ifndef WIN32 // Windows do not support circular dependency between qslog and tools libraries
231 {
233 LDEBUG << "File found:" << path + "/" + file;
234 }
235 #endif
236 return path + "/" + file;
237 }
238 }
239 }
241 LINFO << "findFileInPaths no '" << fileName << "' found in '" << paths
242 << "' separated by '" << separator << "'";
243 return QString();
244}
245
246
247} // end namespace
248} // end namespace
249} // end namespace
#define FILESREPORTLOGINIT
Definition FileUtils.h:28
#define LIMA_PATH_SEPARATOR
Definition FileUtils.h:25
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
void buildDirectoriesListForProject(QStringList &target, const QString &project, const QString &env_postfix, const QString &path_prefix, const QString &path_postfix=QString(""))
void appendFirstOrSecondForProject(QStringList &target, const QString &project, const QString &path_prefix, const QString &path_postfix=QString(""))
QString getEnvVar(const QString &name)
Definition FileUtils.cpp:75
QStringList buildResourcesDirectoriesList(const QStringList &projects, const QStringList &paths)
Build a list of resources directories from a list of project names and a list of paths.
QString getDataHome()
Definition FileUtils.cpp:80
void appendFirstOrSecond(QStringList &target, const QString &path, const QString &alt_path)
Definition FileUtils.cpp:95
uint64_t countLines(std::istream &file)
Count the number of lines in the given file from the current position.
Definition FileUtils.cpp:23
void appendNonEmptyDirs(QStringList &target, const QStringList &source)
Definition FileUtils.cpp:64
QString findFileInPaths(const QString &paths, const QString &fileName, const QChar &separator)
Find the given file in the given paths.
QStringList buildConfigurationDirectoriesList(const QStringList &projects, const QStringList &paths)
Build a list of configuration directories from a list of project names and a list of paths.
NAUTITIA.