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