A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
system-path-examples.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#include "ns3/command-line.h"
10#include "ns3/system-path.h"
11
12#include <iostream>
13#include <string>
14
15/**
16 * @file
17 * @ingroup core-examples
18 * @ingroup systempath
19 * Example program illustrating use of ns3::SystemPath
20 */
21
22using namespace ns3;
23using namespace ns3::SystemPath;
24
25int
26main(int argc, char* argv[])
27{
28 std::string path = FindSelfDirectory();
29
30 CommandLine cmd(__FILE__);
31 cmd.Usage("SystemPath examples.\n");
32
33 cmd.AddValue("path", "Path to demonstrate SystemPath functions.", path);
34 cmd.Parse(argc, argv);
35
36 // Print system path information
37 std::cout << "Example name : " << cmd.GetName() << "\n";
38 std::cout << "FindSelf : " << FindSelf() << "\n";
39 std::cout << "FindSelfDirectory : " << FindSelfDirectory() << "\n";
40 std::cout << "Temporary directory name : " << MakeTemporaryDirectoryName() << "\n";
41 std::cout << "\n";
42
43 std::cout << "Path : " << path << "\n";
44 std::cout << "Exists : " << std::boolalpha << Exists(path) << "\n";
45 std::cout << "Valid system path : " << CreateValidSystemPath(path) << "\n";
46 std::cout << "\n";
47
48 auto pathFoo = Append(path, "foo");
49 std::cout << "Append 'foo' to path : " << pathFoo << "\n";
50 std::cout << "Exists : " << std::boolalpha << Exists(pathFoo) << "\n";
51 std::cout << "\n";
52
53 std::cout << "Split path:\n";
54 auto items = Split(path);
55 for (const auto& item : items)
56 {
57 std::cout << " '" << item << "'\n";
58 }
59 std::cout << "\n";
60
61 std::cout << "Successive joins of the split path:\n";
62 for (auto it = items.begin(); it != items.end(); ++it)
63 {
64 auto partial = Join(items.begin(), it);
65 std::cout << " '" << partial << "'\n";
66 }
67 std::cout << "\n";
68
69 std::cout << "Files in the path directory:\n";
70 auto files = ReadFiles(path);
71 for (const auto& item : files)
72 {
73 std::cout << " '" << item << "'\n";
74 }
75
76 return 0;
77}
Parse command-line arguments.
std::list< std::string > ReadFiles(std::string path)
Get the list of files located in a file system directory.
bool Exists(const std::string path)
Check if a path exists.
std::list< std::string > Split(std::string path)
Split a file system path into directories according to the local path separator.
std::string FindSelf()
Get the file system path to the current executable.
std::string MakeTemporaryDirectoryName()
Get the name of a temporary directory.
std::string Append(std::string left, std::string right)
Join two file system path elements.
std::string Join(std::list< std::string >::const_iterator begin, std::list< std::string >::const_iterator end)
Join a list of file system path directories into a single file system path.
std::string CreateValidSystemPath(const std::string path)
Replace incompatible characters in a path, to get a path compatible with different file systems.
std::string FindSelfDirectory()
Get the file system path to the current executable.
Namespace for various file and directory path functions.
Every class exported by the ns3 library is enclosed in the ns3 namespace.