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