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-error.h
"
15
#include "
fatal-impl.h
"
16
17
#include <iostream>
18
#include <string>
19
20
//
21
// This file is used as part of the ns-3 test framework, so please refrain from
22
// adding any ns-3 specific constructs such as Packet to this file.
23
//
24
namespace
ns3
25
{
26
27
AsciiFile::AsciiFile
()
28
: m_file()
29
{
30
FatalImpl::RegisterStream
(&
m_file
);
31
}
32
33
AsciiFile::~AsciiFile
()
34
{
35
FatalImpl::UnregisterStream
(&
m_file
);
36
Close
();
37
}
38
39
bool
40
AsciiFile::Fail
()
const
41
{
42
return
m_file
.fail();
43
}
44
45
bool
46
AsciiFile::Eof
()
const
47
{
48
return
m_file
.eof();
49
}
50
51
void
52
AsciiFile::Close
()
53
{
54
m_file
.close();
55
}
56
57
void
58
AsciiFile::Open
(
const
std::string& filename, std::ios::openmode mode)
59
{
60
NS_ASSERT
((mode & std::ios::app) == 0);
61
NS_ASSERT
(!
m_file
.fail());
62
63
m_file
.open(filename, mode);
64
}
65
66
void
67
AsciiFile::Read
(std::string& line)
68
{
69
NS_ASSERT
(
m_file
.good());
70
71
// Read the next line.
72
getline(
m_file
, line);
73
}
74
75
bool
76
AsciiFile::Diff
(
const
std::string& f1,
const
std::string& f2, uint64_t& lineNumber)
77
{
78
AsciiFile
ascii1;
79
AsciiFile
ascii2;
80
ascii1.
Open
(f1, std::ios::in);
81
ascii2.
Open
(f2, std::ios::in);
82
bool
bad = ascii1.
Fail
() || ascii2.
Fail
();
83
if
(bad)
84
{
85
return
true
;
86
}
87
88
std::string line1;
89
std::string line2;
90
lineNumber = 0;
91
bool
diff =
false
;
92
93
while
(!ascii1.
Eof
() && !ascii2.
Eof
())
94
{
95
ascii1.
Read
(line1);
96
ascii2.
Read
(line2);
97
98
lineNumber++;
99
100
bool
same = ascii1.
Fail
() == ascii2.
Fail
();
101
if
(!same)
102
{
103
diff =
true
;
104
break
;
105
}
106
if
(ascii1.
Eof
())
107
{
108
break
;
109
}
110
111
if
(line1 != line2)
112
{
113
diff =
true
;
// Lines do not match
114
break
;
115
}
116
}
117
118
return
diff;
119
}
120
121
}
// namespace ns3
ascii-file.h
assert.h
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::AsciiFile
A class representing an ascii file.
Definition
ascii-file.h:27
ns3::AsciiFile::Eof
bool Eof() const
Definition
ascii-file.cc:46
ns3::AsciiFile::Close
void Close()
Close the underlying file.
Definition
ascii-file.cc:52
ns3::AsciiFile::Fail
bool Fail() const
Definition
ascii-file.cc:40
ns3::AsciiFile::AsciiFile
AsciiFile()
Definition
ascii-file.cc:27
ns3::AsciiFile::m_file
std::fstream m_file
output file
Definition
ascii-file.h:75
ns3::AsciiFile::~AsciiFile
~AsciiFile()
Definition
ascii-file.cc:33
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:58
ns3::AsciiFile::Read
void Read(std::string &line)
Read next line from file.
Definition
ascii-file.cc:67
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:76
fatal-error.h
NS_FATAL_x macro definitions.
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 Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0