A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
//
23
namespace
ns3
24
{
25
26
AsciiFile::AsciiFile
()
27
:
m_file
()
28
{
29
FatalImpl::RegisterStream
(&
m_file
);
30
}
31
32
AsciiFile::~AsciiFile
()
33
{
34
FatalImpl::UnregisterStream
(&
m_file
);
35
Close
();
36
}
37
38
bool
39
AsciiFile::Fail
()
const
40
{
41
return
m_file
.fail();
42
}
43
44
bool
45
AsciiFile::Eof
()
const
46
{
47
return
m_file
.eof();
48
}
49
50
void
51
AsciiFile::Close
()
52
{
53
m_file
.close();
54
}
55
56
void
57
AsciiFile::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
65
void
66
AsciiFile::Read
(std::string& line)
67
{
68
NS_ASSERT
(
m_file
.good());
69
70
// Read the next line.
71
getline(
m_file
, line);
72
}
73
74
bool
75
AsciiFile::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
ascii-file.h
assert.h
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::AsciiFile::Eof
bool Eof() const
Definition
ascii-file.cc:45
ns3::AsciiFile::Close
void Close()
Close the underlying file.
Definition
ascii-file.cc:51
ns3::AsciiFile::Fail
bool Fail() const
Definition
ascii-file.cc:39
ns3::AsciiFile::AsciiFile
AsciiFile()
Definition
ascii-file.cc:26
ns3::AsciiFile::m_file
std::fstream m_file
output file
Definition
ascii-file.h:75
ns3::AsciiFile::~AsciiFile
~AsciiFile()
Definition
ascii-file.cc:32
ns3::AsciiFile::Open
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
ns3::AsciiFile::Read
void Read(std::string &line)
Read next line from file.
Definition
ascii-file.cc:66
ns3::AsciiFile::Diff
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
fatal-impl.h
ns3::FatalImpl::RegisterStream(), ns3::FatalImpl::UnregisterStream(), and ns3::FatalImpl::FlushStream...
NS_ASSERT
#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
ns3::FatalImpl::UnregisterStream
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
Definition
fatal-impl.cc:132
ns3::FatalImpl::RegisterStream
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
Definition
fatal-impl.cc:125
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
model
ascii-file.cc
Generated on
for ns-3 by
1.15.0