Main Page · Modules · All Classes · Class Hierarchy
MSDefs.cpp
1 /*
2  * This file is part of the AiBO+ project
3  *
4  * Copyright (C) 2005-2016 Csaba Kertész (csaba.kertesz@gmail.com)
5  *
6  * AiBO+ is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * AiBO+ is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
19  *
20  */
21 
22 #include "MSDefs.hpp"
23 
24 #include <qdir.h>
25 #include <qfile.h>
26 #include <QtGlobal>
27 
28 #include <MCDefs.hpp>
29 #include <MCLog.hpp>
30 
31 static QString BasePath;
32 
33 void MSSetBasePath(const QString& path)
34 {
35  if (path.isEmpty())
36  {
37  MC_WARNING("Empty path is not a valid base path.");
38  return;
39  }
40  BasePath = path;
41  MCCreatePath(path);
42 }
43 
44 
45 QString MSGetBasePath()
46 {
47  if (BasePath.isEmpty())
48  {
49  MSSetBasePath(QDir::homePath());
50  }
51  return BasePath;
52 }
53 
54 
55 QString MSGetMlBasePath()
56 {
58 }
59 
60 
62 {
63  return MSGetMlBasePath()+"models"+MC_DIR_SEPARATOR;
64 }
65 
66 
68 {
69  return MSGetMlBasePath()+"samples"+MC_DIR_SEPARATOR;
70 }
71 
72 
73 int MSDirectorySize(const QString& directory)
74 {
75  if (directory.isEmpty())
76  return 0;
77 
78  int Size = 0;
79  QFileInfo FileInfo(directory);
80 
81  if (FileInfo.isDir())
82  {
83  QDir Dir(directory);
84  QFileInfoList FileIntoList = Dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden |
85  QDir::NoSymLinks | QDir::NoDotAndDotDot);
86 
87  for (int i = 0; i < FileIntoList.size(); ++i)
88  {
89  QFileInfo FileInfo = FileIntoList.at(i);
90 
91  if (FileInfo.isDir())
92  Size += MSDirectorySize(FileInfo.absoluteFilePath());
93  else
94  Size += FileInfo.size();
95  }
96  }
97  return Size;
98 }
99 
100 
101 QStringList MSGetSubDirectories(const QString& directory)
102 {
103  if (directory.isEmpty())
104  return QStringList();
105 
106  QFileInfo FileInfo(directory);
107  QStringList Dirs;
108 
109  if (!FileInfo.isDir())
110  return Dirs;
111 
112  QDir Dir(directory);
113  QFileInfoList DirList = Dir.entryInfoList(QDir::Dirs | QDir::Hidden |
114  QDir::NoSymLinks | QDir::NoDotAndDotDot);
115 
116  Dirs.push_back(FileInfo.absoluteFilePath());
117  for (int i = 0; i < DirList.size(); ++i)
118  {
119  QFileInfo DirInfo = DirList.at(i);
120 
121  Dirs += MSGetSubDirectories(DirInfo.absoluteFilePath()+MC_DIR_SEPARATOR);
122  }
123  return Dirs;
124 }
125 
126 
127 QStringList MSGetSubDirectoryContents(const QString& directory)
128 {
129  if (directory.isEmpty())
130  return QStringList();
131 
132  QFileInfo FileInfo(directory);
133  QStringList Files;
134 
135  if (!FileInfo.isDir())
136  return Files;
137 
138  QDir Dir(directory);
139  QFileInfoList FileIntoList = Dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden |
140  QDir::NoSymLinks | QDir::NoDotAndDotDot);
141 
142  for (int i = 0; i < FileIntoList.size(); ++i)
143  {
144  QFileInfo FileInfo = FileIntoList.at(i);
145 
146  if (FileInfo.isDir())
147  Files += MSGetSubDirectoryContents(FileInfo.absoluteFilePath());
148  else
149  Files.push_back(FileInfo.absoluteFilePath());
150  }
151  return Files;
152 }
bool MCCreatePath(const QString &path)
Create a new path if it does not exist.
Definition: MCDefs.cpp:344
#define MC_WARNING(...)
Warning macro.
Definition: MCLog.hpp:43
QStringList MSGetSubDirectories(const QString &directory)
Get all subdirectories recursively.
Definition: MSDefs.cpp:101
QStringList MSGetSubDirectoryContents(const QString &directory)
Get all files from a directory recursively.
Definition: MSDefs.cpp:127
#define MC_DIR_SEPARATOR
Directory separator macro.
Definition: MCDefs.hpp:578
void MSSetBasePath(const QString &path)
Set the base path.
Definition: MSDefs.cpp:33
int MSDirectorySize(const QString &directory)
Calculate directory size.
Definition: MSDefs.cpp:73
QString MSGetBasePath()
Get the base path.
Definition: MSDefs.cpp:45
QString MSGetMlModelsBasePath()
Get the machine learning models base path.
Definition: MSDefs.cpp:61
QString MSGetMlBasePath()
Get the machine learning base path.
Definition: MSDefs.cpp:55
QString MSGetMlSamplesBasePath()
Get the machine learning samples base path.
Definition: MSDefs.cpp:67