Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /** Following functions investigate the directory manipulation capabilities in
11  * MRPT. Functions are called from the main function in the end of the script.
12  * See each function for the corresponding usage.
13  * See http://reference.mrpt.org/stable/group__filesystem.html on the
14  * documentation of the functions
15  */
16 
17 #include <mrpt/poses/CPose2D.h>
18 #include <mrpt/poses/CPosePDF.h>
19 #include <mrpt/system/filesystem.h>
21 #include <mrpt/system/datetime.h>
22 
23 #include <string>
24 #include <sstream>
25 #include <iostream>
26 
27 using namespace mrpt;
28 using namespace mrpt::poses;
29 using namespace mrpt::io;
30 using namespace mrpt::system;
31 using namespace std;
32 
33 /** Create a directory
34  * Open and write some content in a file inside the directory
35  * If directory exists delete it altogether.
36  */
37 void setupDirContents()
38 {
40 
41  string dir_name = "dir_a";
42  string file_name = "file_b";
43  CPose2D a_pose(1, 2, DEG2RAD(40));
44 
45  if (!directoryExists(dir_name))
46  {
47  cout << "Creating directory... " << endl;
48  createDirectory(dir_name);
49  f.open(dir_name + "/" + file_name);
50  if (f.fileOpenCorrectly())
51  { // checking for errors...
52  cout << "file was opened correctly" << endl;
53  // CSerializable form (binary)
54  f.printf("some random text ...\n");
55  f.printf("some more random text.\n");
56  f.printf("CPose2D: %s", a_pose.asString().c_str());
57  f.close();
58  }
59  else
60  {
61  cout << "file was NOT opened successfully" << endl;
62  return;
63  }
64  }
65  else
66  {
67  cout << "directory " << dir_name << " exists. " << endl;
68  cout << "removing directory altogether... " << endl;
70  dir_name,
71  /* deleteDirectoryAsWell = */ true);
72  }
73 }
74 
75 /** Initialize a directory along with some dummy content.
76  * Rename the directory and filenames inside it to
77  * ${PREVIOUS_FNAME}_renamed_datetime
78  */
79 void renameDirContents()
80 {
81  string dir_name = "dir_b";
83  string fname = "file";
84  stringstream ss_tmp;
85 
86  // get the current datetime as a string - add it to the renamed fnames
87  TTimeStamp cur_time(getCurrentTime());
88  string cur_time_str = dateTimeToString(cur_time);
89  string cur_time_validstr(fileNameStripInvalidChars(cur_time_str));
90  string string_to_add = "_renamed_" + cur_time_validstr;
91 
92  bool success; // flag for specifying whether an operation was successfully
93  // completed
94 
95  if (!directoryExists(dir_name))
96  {
97  cout << "directory " << dir_name << " doesn't exist. " << endl;
98  cout << "Creating it.. " << endl;
99  success = createDirectory(dir_name);
100  if (!success)
101  {
103  "There was an error creating the directory: " + dir_name)
104  }
105  }
106 
107  // build the initial directory contents
108  for (int i = 0; i < 10; i++)
109  {
110  ss_tmp.str("");
111  ss_tmp << dir_name << "/" << fname << i;
112  f.open(ss_tmp.str());
113  f.printf("dummy text in file...");
114  f.close();
115  }
116 
117  // rename all the contents (of depth 1)
118  for (int i = 0; i < 10; i++)
119  {
120  ss_tmp.str("");
121  ss_tmp << dir_name << "/" << fname << i;
122  success = renameFile(
123  ss_tmp.str(),
124  /*new_name = */ ss_tmp.str() + string_to_add);
125  }
126 
127  // finally rename the directory itself
128  cout << "Renaming directory " << dir_name << " to: " << dir_name
129  << string_to_add << endl;
130  string* err_msg = nullptr; // flag for catching the error msg if any..
131  success = renameFile(dir_name, dir_name + string_to_add, err_msg);
132  if (success)
133  {
134  cout << "Directory renaming was successful!" << endl;
135  }
136  else
137  {
138  THROW_EXCEPTION("Error while trying to rename directory: " + dir_name);
139  }
140 }
141 
142 //
143 // MAIN
144 //
145 int main()
146 {
147  char c;
148  try
149  {
150  cout << "Running setupDirContents fun..." << endl;
151  cout << "------------------------------" << endl;
153  cout << "Press a key to continue..." << endl;
154  c = getchar();
155 
156  cout << "Running RenameDirContents fun..." << endl;
157  cout << "------------------------------" << endl;
159 
160  return 0;
161  }
162  catch (std::exception& e)
163  {
164  std::cout << "MRPT exception caught: " << e.what() << std::endl;
165  return -1;
166  }
167  catch (...)
168  {
169  printf("Untyped exception!!");
170  return -1;
171  }
172 }
filesystem.h
mrpt::io::CFileOutputStream::close
void close()
Close the stream.
Definition: CFileOutputStream.cpp:43
mrpt::system::dateTimeToString
std::string dateTimeToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (UTC time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:248
mrpt::system::renameFile
bool renameFile(const std::string &oldFileName, const std::string &newFileName, std::string *error_msg=nullptr)
Renames a file - If the target path is different and the filesystem allows it, it will be moved to th...
Definition: filesystem.cpp:308
mrpt::io::CFileOutputStream::open
bool open(const std::string &fileName, bool append=false)
Open the given file for write.
Definition: CFileOutputStream.cpp:31
mrpt::io
Definition: img/CImage.h:22
mrpt::system::directoryExists
bool directoryExists(const std::string &fileName)
Test if a given directory exists (it fails if the given path refers to an existing file).
Definition: filesystem.cpp:136
c
const GLubyte * c
Definition: glext.h:6313
CFileOutputStream.h
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CPose2D.h
renameDirContents
void renameDirContents()
Initialize a directory along with some dummy content.
Definition: vision_stereo_rectify/test.cpp:79
mrpt::system::TTimeStamp
uint64_t TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:31
mrpt::system::fileNameStripInvalidChars
std::string fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars='_')
Replace invalid filename chars by underscores ('_') or any other user-given char.
Definition: filesystem.cpp:328
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
CPosePDF.h
mrpt::poses::CPose2D
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:40
mrpt::io::CFileOutputStream::fileOpenCorrectly
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
Definition: CFileOutputStream.cpp:112
mrpt::system::deleteFilesInDirectory
bool deleteFilesInDirectory(const std::string &s, bool deleteDirectoryAsWell=false)
Delete all the files in a given directory (nothing done if directory does not exists,...
Definition: filesystem.cpp:215
mrpt::io::CFileOutputStream
This CStream derived class allow using a file as a write-only, binary stream.
Definition: io/CFileOutputStream.h:24
mrpt::system::getCurrentTime
mrpt::system::TTimeStamp getCurrentTime()
Returns the current (UTC) system time.
Definition: datetime.cpp:74
mrpt::io::CStream::printf
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Definition: CStream.cpp:30
mrpt::system::createDirectory
bool createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:158
setupDirContents
void setupDirContents()
Create a directory Open and write some content in a file inside the directory If directory exists del...
Definition: vision_stereo_rectify/test.cpp:37
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
datetime.h
mrpt::DEG2RAD
double DEG2RAD(const double x)
Degrees to radians.
Definition: core/include/mrpt/core/bits_math.h:42



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST