UFile.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "rtabmap/utilite/UFile.h"
21 
22 #include <fstream>
23 #include "rtabmap/utilite/UStl.h"
24 
25 bool UFile::exists(const std::string &filePath)
26 {
27  bool fileExists = false;
28  std::ifstream in(filePath.c_str(), std::ios::in);
29  if (in.is_open())
30  {
31  fileExists = true;
32  in.close();
33  }
34  return fileExists;
35 }
36 
37 long UFile::length(const std::string &filePath)
38 {
39  long fileSize = 0;
40  FILE* fp = 0;
41 #ifdef _MSC_VER
42  fopen_s(&fp, filePath.c_str(), "rb");
43 #else
44  fp = fopen(filePath.c_str(), "rb");
45 #endif
46  if(fp == NULL)
47  {
48  return 0;
49  }
50 
51  fseek(fp , 0 , SEEK_END);
52  fileSize = ftell(fp);
53  fclose(fp);
54 
55  return fileSize;
56 }
57 
58 int UFile::erase(const std::string &filePath)
59 {
60  return std::remove(filePath.c_str());
61 }
62 
63 int UFile::rename(const std::string &oldFilePath,
64  const std::string &newFilePath)
65 {
66  return std::rename(oldFilePath.c_str(), newFilePath.c_str());
67 }
68 
69 std::string UFile::getName(const std::string & filePath)
70 {
71  std::string fullPath = filePath;
72  std::string name;
73  for(int i=(int)fullPath.size()-1; i>=0; --i)
74  {
75  if(fullPath[i] == '/' || fullPath[i] == '\\')
76  {
77  break;
78  }
79  else
80  {
81  name.insert(name.begin(), fullPath[i]);
82  }
83  }
84  return name;
85 }
86 
87 std::string UFile::getExtension(const std::string &filePath)
88 {
89  std::list<std::string> list = uSplit(filePath, '.');
90  if(list.size())
91  {
92  return list.back();
93  }
94  return "";
95 }
96 
97 void UFile::copy(const std::string & from, const std::string & to)
98 {
99  std::ifstream src(from.c_str());
100  std::ofstream dst(to.c_str());
101 
102  dst << src.rdbuf();
103 }
UFile::rename
static int rename(const std::string &oldFilePath, const std::string &newFilePath)
Definition: UFile.cpp:63
UFile::getName
std::string getName()
Definition: UFile.h:135
name
UFile::erase
static int erase(const std::string &filePath)
Definition: UFile.cpp:58
list
UFile::length
long length()
Definition: UFile.h:110
list::size
size_t size() const
UFile::getExtension
std::string getExtension()
Definition: UFile.h:140
uSplit
std::list< std::string > uSplit(const std::string &str, char separator=' ')
Definition: UStl.h:564
UFile::copy
static void copy(const std::string &from, const std::string &to)
Definition: UFile.cpp:97
UStl.h
Wrappers of STL for convenient functions.
NULL
#define NULL
UFile.h
dst
char * dst
Definition: lz4.h:354
UFile::exists
bool exists()
Definition: UFile.h:104
i
int i


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:23