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


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26