File: test.cpp

package info (click to toggle)
mrpt 1%3A2.5.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,448 kB
  • sloc: cpp: 551,662; ansic: 38,702; xml: 3,914; python: 2,547; sh: 404; makefile: 237
file content (164 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* +------------------------------------------------------------------------+
   |                     Mobile Robot Programming Toolkit (MRPT)            |
   |                          https://www.mrpt.org/                         |
   |                                                                        |
   | Copyright (c) 2005-2023, Individual contributors, see AUTHORS file     |
   | See: https://www.mrpt.org/Authors - All rights reserved.               |
   | Released under BSD License. See: https://www.mrpt.org/License          |
   +------------------------------------------------------------------------+ */

/** Following functions investigate the directory manipulation capabilities in
 * MRPT. Functions are called from the main function in the end of the script.
 * See each function for the corresponding usage.
 * See https://reference.mrpt.org/stable/group__filesystem.html on the
 * documentation of the functions
 */

#include <mrpt/io/CFileOutputStream.h>
#include <mrpt/poses/CPose2D.h>
#include <mrpt/poses/CPosePDF.h>
#include <mrpt/system/datetime.h>
#include <mrpt/system/filesystem.h>

#include <iostream>
#include <sstream>
#include <string>

using namespace mrpt;
using namespace mrpt::poses;
using namespace mrpt::io;
using namespace mrpt::system;
using namespace std;

/** Create a directory
 * Open and write some content in a file inside the directory
 * If directory exists delete it altogether.
 */
void setupDirContents()
{
	CFileOutputStream f;

	string dir_name = "dir_a";
	string file_name = "file_b";
	CPose2D a_pose(1, 2, 40.0_deg);

	if (!directoryExists(dir_name))
	{
		cout << "Creating directory... " << endl;
		createDirectory(dir_name);
		f.open(dir_name + "/" + file_name, OpenMode::TRUNCATE);
		if (f.fileOpenCorrectly())
		{  // checking for errors...
			cout << "file was opened correctly" << endl;
			// CSerializable form (binary)
			f.printf("some random text ...\n");
			f.printf("some more random text.\n");
			f.printf("CPose2D: %s", a_pose.asString().c_str());
			f.close();
		}
		else
		{
			cout << "file was NOT opened successfully" << endl;
			return;
		}
	}
	else
	{
		cout << "directory " << dir_name << " exists. " << endl;
		cout << "removing directory altogether... " << endl;
		deleteFilesInDirectory(
			dir_name,
			/* deleteDirectoryAsWell = */ true);
	}
}

/** Initialize a directory along with some dummy content.
 * Rename the directory and filenames inside it to
 * ${PREVIOUS_FNAME}_renamed_datetime
 */
void renameDirContents()
{
	string dir_name = "dir_b";
	CFileOutputStream f;
	string fname = "file";
	stringstream ss_tmp;

	// get the current datetime as a string - add it to the renamed fnames
	TTimeStamp cur_time(getCurrentTime());
	string cur_time_str = dateTimeToString(cur_time);
	string cur_time_validstr(fileNameStripInvalidChars(cur_time_str));
	string string_to_add = "_renamed_" + cur_time_validstr;

	bool success;  // flag for specifying whether an operation was successfully
	// completed

	if (!directoryExists(dir_name))
	{
		cout << "directory " << dir_name << " doesn't exist. " << endl;
		cout << "Creating it.. " << endl;
		success = createDirectory(dir_name);
		if (!success)
		{
			THROW_EXCEPTION(
				"There was an error creating the directory: " + dir_name);
		}
	}

	// build the initial directory contents
	for (int i = 0; i < 10; i++)
	{
		ss_tmp.str("");
		ss_tmp << dir_name << "/" << fname << i;
		f.open(ss_tmp.str(), OpenMode::TRUNCATE);
		f.printf("dummy text in file...");
		f.close();
	}

	// rename all the contents (of depth 1)
	for (int i = 0; i < 10; i++)
	{
		ss_tmp.str("");
		ss_tmp << dir_name << "/" << fname << i;
		success = renameFile(
			ss_tmp.str(),
			/*new_name = */ ss_tmp.str() + string_to_add);
	}

	// finally rename the directory itself
	cout << "Renaming directory " << dir_name << " to: " << dir_name
		 << string_to_add << endl;
	string* err_msg = nullptr;	// flag for catching the error msg if any..
	success = renameFile(dir_name, dir_name + string_to_add, err_msg);
	if (success) { cout << "Directory renaming was successful!" << endl; }
	else
	{
		THROW_EXCEPTION("Error while trying to rename directory: " + dir_name);
	}
}

//
// MAIN
//
int main()
{
	try
	{
		cout << "Running setupDirContents fun..." << endl;
		cout << "------------------------------" << endl;
		setupDirContents();
		cout << "Press a key to continue..." << endl;
		// char c=
		getchar();

		cout << "Running RenameDirContents fun..." << endl;
		cout << "------------------------------" << endl;
		renameDirContents();

		return 0;
	}
	catch (const std::exception& e)
	{
		std::cerr << "MRPT error: " << mrpt::exception_to_str(e) << std::endl;
		return -1;
	}
}