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-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//
24namespace ns3
25{
26
32
38
39bool
41{
42 return m_file.fail();
43}
44
45bool
47{
48 return m_file.eof();
49}
50
51void
53{
54 m_file.close();
55}
56
57void
58AsciiFile::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
66void
67AsciiFile::Read(std::string& line)
68{
69 NS_ASSERT(m_file.good());
70
71 // Read the next line.
72 getline(m_file, line);
73}
74
75bool
76AsciiFile::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
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
A class representing an ascii file.
Definition ascii-file.h:27
bool Eof() const
Definition ascii-file.cc:46
void Close()
Close the underlying file.
Definition ascii-file.cc:52
bool Fail() const
Definition ascii-file.cc:40
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:58
void Read(std::string &line)
Read next line from file.
Definition ascii-file.cc:67
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
NS_FATAL_x macro definitions.
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.