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/core-module.h"
10
11#include <iomanip>
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 = "/usr/share/dict/";
29
31 cmd.Usage("SystemPath examples.\n");
32
33 cmd.AddValue("path", "Path to demonstrate SystemPath functions.", path);
34 cmd.Parse(argc, argv);
35
36 // Show initial values:
37 std::cout << std::endl;
38 std::cout << cmd.GetName() << ":" << std::endl;
39
40 std::cout << "FindSelfDirectory: " << FindSelfDirectory() << std::endl;
41
42 std::cout << "Demonstration path: " << path << std::endl;
43 std::cout << "Exists? " << (Exists(path) ? "yes" : "no") << std::endl;
44
45 auto foo = Append(path, "foo");
46 std::cout << "Append 'foo': " << foo << std::endl;
47 std::cout << "Exists? " << (Exists(foo) ? "yes" : "no") << std::endl;
48
49 std::cout << "Split path:\n";
50 auto items = Split(path);
51 for (const auto& item : items)
52 {
53 std::cout << " '" << item << "'\n";
54 }
55 std::cout << std::endl;
56
57 std::cout << "Successive Joins: \n";
58 for (auto it = items.begin(); it != items.end(); ++it)
59 {
60 auto partial = Join(items.begin(), it);
61 std::cout << " '" << partial << "'\n";
62 }
63
64 std::cout << "Files in the directory: \n";
65 auto files = ReadFiles(path);
66 for (const auto& item : files)
67 {
68 std::cout << " '" << item << "'\n";
69 }
70
71 return 0;
72}
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 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 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.