A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ascii-file.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mitch Watrous (watrous@u.washington.edu)
7 *
8 * This file is based on pcap-file.cc by Craig Dowell (craigdo@ee.washington.edu)
9 */
10
11#include "ascii-file.h"
12
13#include "assert.h"
14#include "fatal-impl.h"
15
16#include <iostream>
17#include <string>
18
19//
20// This file is used as part of the ns-3 test framework, so please refrain from
21// adding any ns-3 specific constructs such as Packet to this file.
22//
23namespace ns3
24{
25
31
37
38bool
40{
41 return m_file.fail();
42}
43
44bool
46{
47 return m_file.eof();
48}
49
50void
52{
53 m_file.close();
54}
55
56void
57AsciiFile::Open(const std::string& filename, std::ios::openmode mode)
58{
59 NS_ASSERT((mode & std::ios::app) == 0);
60 NS_ASSERT(!m_file.fail());
61
62 m_file.open(filename, mode);
63}
64
65void
66AsciiFile::Read(std::string& line)
67{
68 NS_ASSERT(m_file.good());
69
70 // Read the next line.
71 getline(m_file, line);
72}
73
74bool
75AsciiFile::Diff(const std::string& f1, const std::string& f2, uint64_t& lineNumber)
76{
77 AsciiFile ascii1;
78 AsciiFile ascii2;
79 ascii1.Open(f1, std::ios::in);
80 ascii2.Open(f2, std::ios::in);
81 bool bad = ascii1.Fail() || ascii2.Fail();
82 if (bad)
83 {
84 return true;
85 }
86
87 std::string line1;
88 std::string line2;
89 lineNumber = 0;
90 bool diff = false;
91
92 while (!ascii1.Eof() && !ascii2.Eof())
93 {
94 ascii1.Read(line1);
95 ascii2.Read(line2);
96
97 lineNumber++;
98
99 bool same = ascii1.Fail() == ascii2.Fail();
100 if (!same)
101 {
102 diff = true;
103 break;
104 }
105 if (ascii1.Eof())
106 {
107 break;
108 }
109
110 if (line1 != line2)
111 {
112 diff = true; // Lines do not match
113 break;
114 }
115 }
116
117 return diff;
118}
119
120} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
bool Eof() const
Definition ascii-file.cc:45
void Close()
Close the underlying file.
Definition ascii-file.cc:51
bool Fail() const
Definition ascii-file.cc:39
std::fstream m_file
output file
Definition ascii-file.h:75
void Open(const std::string &filename, std::ios::openmode mode)
Create a new ascii file or open an existing ascii file.
Definition ascii-file.cc:57
void Read(std::string &line)
Read next line from file.
Definition ascii-file.cc:66
static bool Diff(const std::string &f1, const std::string &f2, uint64_t &lineNumber)
Compare two ASCII files line-by-line.
Definition ascii-file.cc:75
ns3::FatalImpl::RegisterStream(), ns3::FatalImpl::UnregisterStream(), and ns3::FatalImpl::FlushStream...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
Every class exported by the ns3 library is enclosed in the ns3 namespace.