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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mitch Watrous (watrous@u.washington.edu)
18 *
19 * This file is based on pcap-file.cc by Craig Dowell (craigdo@ee.washington.edu)
20 */
21
22#include "ascii-file.h"
23
24#include "assert.h"
25#include "fatal-error.h"
26#include "fatal-impl.h"
27
28#include <iostream>
29#include <string>
30
31//
32// This file is used as part of the ns-3 test framework, so please refrain from
33// adding any ns-3 specific constructs such as Packet to this file.
34//
35namespace ns3
36{
37
39 : m_file()
40{
42}
43
45{
47 Close();
48}
49
50bool
52{
53 return m_file.fail();
54}
55
56bool
58{
59 return m_file.eof();
60}
61
62void
64{
65 m_file.close();
66}
67
68void
69AsciiFile::Open(const std::string& filename, std::ios::openmode mode)
70{
71 NS_ASSERT((mode & std::ios::app) == 0);
72 NS_ASSERT(!m_file.fail());
73
74 m_file.open(filename, mode);
75}
76
77void
78AsciiFile::Read(std::string& line)
79{
80 NS_ASSERT(m_file.good());
81
82 // Read the next line.
83 getline(m_file, line);
84}
85
86bool
87AsciiFile::Diff(const std::string& f1, const std::string& f2, uint64_t& lineNumber)
88{
89 AsciiFile ascii1;
90 AsciiFile ascii2;
91 ascii1.Open(f1, std::ios::in);
92 ascii2.Open(f2, std::ios::in);
93 bool bad = ascii1.Fail() || ascii2.Fail();
94 if (bad)
95 {
96 return true;
97 }
98
99 std::string line1;
100 std::string line2;
101 lineNumber = 0;
102 bool diff = false;
103
104 while (!ascii1.Eof() && !ascii2.Eof())
105 {
106 ascii1.Read(line1);
107 ascii2.Read(line2);
108
109 lineNumber++;
110
111 bool same = ascii1.Fail() == ascii2.Fail();
112 if (!same)
113 {
114 diff = true;
115 break;
116 }
117 if (ascii1.Eof())
118 {
119 break;
120 }
121
122 if (line1 != line2)
123 {
124 diff = true; // Lines do not match
125 break;
126 }
127 }
128
129 return diff;
130}
131
132} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
A class representing an ascii file.
Definition: ascii-file.h:38
bool Eof() const
Definition: ascii-file.cc:57
void Close()
Close the underlying file.
Definition: ascii-file.cc:63
bool Fail() const
Definition: ascii-file.cc:51
std::fstream m_file
output file
Definition: ascii-file.h:86
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:69
void Read(std::string &line)
Read next line from file.
Definition: ascii-file.cc:78
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:87
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:66
void UnregisterStream(std::ostream *stream)
Unregister a stream for flushing on abnormal exit.
Definition: fatal-impl.cc:143
void RegisterStream(std::ostream *stream)
Register a stream to be flushed on abnormal exit.
Definition: fatal-impl.cc:136
Every class exported by the ns3 library is enclosed in the ns3 namespace.