otbSystem.cxx 6.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Thomas Feuvrier's avatar
Thomas Feuvrier committed
20 21 22


#include "otbSystem.h"
23 24
#include <string> // strdup
#include <cstdlib>
25

26
#if (defined(WIN32) || defined(WIN32CE)) && !defined(__CYGWIN__) && !defined(__MINGW32__)
27

28 29 30
/*=====================================================================
                   WIN32 / MSVC++ implementation
 *====================================================================*/
31 32 33 34 35
#ifndef WIN32CE
#  include <io.h>
#else
#  include <wce_io.h>
#endif
36 37 38 39
#else
/*=====================================================================
                      POSIX (Unix) implementation
 *====================================================================*/
40 41
#include <sys/types.h>
#include <dirent.h>
42 43
#endif

Thomas Feuvrier's avatar
Thomas Feuvrier committed
44 45 46 47 48
namespace otb
{

//GetRootName from uiig library.
std::string
OTB Bot's avatar
STYLE  
OTB Bot committed
49
System::GetRootName(const std::string& filename)
Thomas Feuvrier's avatar
Thomas Feuvrier committed
50
{
51
  const std::string fileExt = itksys::SystemTools::GetFilenameLastExtension(filename);
Thomas Feuvrier's avatar
Thomas Feuvrier committed
52 53 54

  // Create a base filename
  // i.e Image.ent --> Image
OTB Bot's avatar
STYLE  
OTB Bot committed
55 56 57
  if (fileExt.length() > 0)
    {
    const std::string::size_type it = filename.find_last_of(fileExt);
58
    std::string                  baseName(filename, 0, it - fileExt.length() + 1);
OTB Bot's avatar
STYLE  
OTB Bot committed
59 60
    return (baseName);
    }
Thomas Feuvrier's avatar
Thomas Feuvrier committed
61
  //Default to return same as input when the extension is nothing (Analyze)
OTB Bot's avatar
STYLE  
OTB Bot committed
62
  return (filename);
Thomas Feuvrier's avatar
Thomas Feuvrier committed
63 64
}

65 66

#if (defined(WIN32) || defined(WIN32CE)) && !defined(__CYGWIN__) && !defined(__MINGW32__)
67 68 69 70 71

/*=====================================================================
                   WIN32 / MSVC++ implementation
 *====================================================================*/

72
std::vector<std::string> System::Readdir(const std::string&  pszPath)
73
{
OTB Bot's avatar
STYLE  
OTB Bot committed
74 75 76 77 78
  struct _finddata_t       c_file;
  long                     hFile;
  std::vector<std::string> listFileFind;
  std::string              pszFileSpec;
  std::string              path(pszPath);
79

OTB Bot's avatar
STYLE  
OTB Bot committed
80
  if (pszPath.empty() == true) path = ".";
81

82
  pszFileSpec = path + "\\*.*";
83

OTB Bot's avatar
STYLE  
OTB Bot committed
84
  if ((hFile = _findfirst(pszFileSpec.c_str(), &c_file)) != -1L)
85
    {
OTB Bot's avatar
STYLE  
OTB Bot committed
86 87
    do
      {
88
      listFileFind.push_back(c_file.name);
OTB Bot's avatar
STYLE  
OTB Bot committed
89 90
      }
    while (_findnext(hFile, &c_file) == 0);
91

OTB Bot's avatar
STYLE  
OTB Bot committed
92 93
    _findclose(hFile);
    }
94

95
  return listFileFind;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
}

#else

/*=====================================================================
                      POSIX (Unix) implementation
 *====================================================================*/

/**
 * Read names in a directory.
 *
 * This function abstracts access to directory contains.  It returns a
 * list of strings containing the names of files, and directories in this
 * directory.  The resulting string list becomes the responsibility of the
 * application and should be freed with CSLDestroy() when no longer needed.
 *
 * Note that no error is issued via CPLError() if the directory path is
 * invalid, though NULL is returned.
 *
 * @param pszPath the relative, or absolute path of a directory to read.
 * @return The list of entries in the directory, or NULL if the directory
 * doesn't exist.
 */

120
std::vector<std::string> System::Readdir(const std::string& pszPath)
121
{
OTB Bot's avatar
STYLE  
OTB Bot committed
122 123 124 125
  DIR *                    hDir;
  std::vector<std::string> listFileFind;
  struct dirent *          psDirEntry;
  std::string              path(pszPath);
126

OTB Bot's avatar
STYLE  
OTB Bot committed
127
  if (pszPath.empty() == true) path = ".";
128

129
  if ((hDir = opendir(path.c_str())) != nullptr)
130
    {
131
    while ((psDirEntry = readdir(hDir)) != nullptr)
OTB Bot's avatar
STYLE  
OTB Bot committed
132
      {
133
      listFileFind.push_back(psDirEntry->d_name);
OTB Bot's avatar
STYLE  
OTB Bot committed
134
      }
135

OTB Bot's avatar
STYLE  
OTB Bot committed
136 137
    closedir(hDir);
    }
138
  return listFileFind;
139 140
}

141 142 143
#endif


144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/** From an hdf subset name such as:
 * SUBDATASET_7_NAME=HDF4_EOS:EOS_GRID:"file/MOD13Q1.A2010001.h17v05.005.2010028003734.hdf":MODIS_Grid_16DAY...
 * it will set key to SUBDATASET_7_NAME
 * and name to HDF4_EOS:EOS_GRID:"file/MOD13Q1.A2010001.h17v05.005.2010028003734.hdf":MODIS_Grid_16DAY...
 */
bool System::ParseHdfSubsetName(const std::string& id, std::string& key, std::string& name)
{
  std::size_t pos = id.find("=");
  if (pos == std::string::npos) return false;
  key = id.substr(0, pos);
  name = id.substr(pos+1, id.size() - pos - 1);
  return true;
}

/** Parse and hdf filename indicating the dataset number
 * if id is filelocation/file.hdf:10
 * it will return filelocation/file.hdf in file
 * and 10 in datasetNum
 */
bool System::ParseHdfFileName(const std::string& id, std::string& file, unsigned int& datasetNum)
{
  std::size_t pos = id.rfind(":");
  if (pos == std::string::npos) return false;
  std::string datasetNumString(id.substr(pos+1, id.size() - pos - 1));
  datasetNum = atoi(datasetNumString.c_str());
  // a value of 0 could be the sign that the conversion failed, we need to check that the character was really 0
  if (datasetNum == 0)
    {
      if (datasetNumString.compare("0") != 0)
        {
        return false;
        }
    }
  file = id.substr(0, pos);
  return true;
}

181 182 183 184 185
/** Parse a filename indicating an additional information
 * if id is filelocation/file.***:10
 * it will return filelocation/file.*** in file
 * and 10 in addNum additional
 */
186
bool System::ParseFileNameForAdditionalInfo(const std::string& id, std::string& file, unsigned int& addNum)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
  std::size_t pos = id.rfind(":");
  if (pos == std::string::npos) return false;

  std::string addNumString(id.substr(pos+1, id.size() - pos - 1));
  addNum = atoi(addNumString.c_str());
  // a value of 0 could be the sign that the conversion failed, we need to check that the character was really 0
  if (addNum == 0)
    {
      if (addNumString.compare("0") != 0)
        {
        return false;
        }
    }
  file = id.substr(0, pos);
  return true;
}

Thomas Feuvrier's avatar
Thomas Feuvrier committed
205
}