fw4spl
Os.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 
8 #include <boost/filesystem.hpp>
9 
10 #include "fwCore/base.hpp"
11 #include "fwTools/Os.hpp"
12 
13 
14 namespace fwTools
15 {
16 
17 namespace os
18 {
19 
20 std::string getEnv(const std::string &name, bool *ok)
21 {
22  char *value = std::getenv(name.c_str());
23  bool exists = (value != NULL);
24  if(ok != NULL)
25  {
26  *ok = exists;
27  }
28  return std::string(exists ? value : "");
29 }
30 
31 //------------------------------------------------------------------------------
32 
33 std::string getEnv(const std::string &name, const std::string &defaultValue)
34 {
35  bool ok = false;
36  std::string value = getEnv(name, &ok);
37  return ok ? value : defaultValue;
38 }
39 
40 //------------------------------------------------------------------------------
41 
42 std::string getUserDataDir( std::string company, std::string appName, bool createDirectory )
43 {
44  std::string dataDir;
45 #ifdef WIN32
46  char *appData = std::getenv("APPDATA");
47  dataDir = ::fwTools::os::getEnv("APPDATA");
48 #else
49  bool hasXdgConfigHome = false;
50  bool hasHome = false;
51  std::string xdgConfigHome = ::fwTools::os::getEnv("XDG_CONFIG_HOME", &hasXdgConfigHome);
52  std::string home = ::fwTools::os::getEnv("HOME", &hasHome);
53  dataDir = hasXdgConfigHome ? xdgConfigHome : (hasHome ? std::string(home) + "/.config" : "");
54 #endif
55 
56  if ( !company.empty() )
57  {
58  dataDir += "/" + company;
59  }
60 
61 
62  if ( !appName.empty() )
63  {
64  dataDir += "/" + appName;
65  }
66 
67  if ( !dataDir.empty() )
68  {
69  if (boost::filesystem::exists(dataDir))
70  {
71  if ( !boost::filesystem::is_directory(dataDir) )
72  {
73  OSLM_ERROR( dataDir << " already exists and is not a directory." );
74  dataDir = "";
75  }
76  }
77  else if (createDirectory)
78  {
79  OSLM_INFO("Creating application data directory: "<< dataDir);
80  boost::filesystem::create_directories(dataDir);
81  }
82  }
83 
84  return dataDir;
85 }
86 
87 } // namespace os
88 
89 } // namespace fwTools
90 
FWTOOLS_API std::string getUserDataDir(std::string company="", std::string appName="", bool createDirectory=false)
Return the users&#39;s application data directory.
Definition: Os.cpp:42
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
FWTOOLS_API std::string getEnv(const std::string &name, bool *ok=NULL)
Returns a environment variable value.
Definition: Os.cpp:20