The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tCompareBits.inc
Go to the documentation of this file.
1 /**
2  * @file tCompareBits
3  * file name: tCompareBits.inc
4  * @author Betti Oesterholz
5  * @date 01.11.2009
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * This includefile contains functions for comparing bits and byts.
11  *
12  * Copyright (C) @c GPL3 2009 Betti Oesterholz
13  *
14  * This program is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  *
27  *
28  * This includefile contains functions for comparing bits and byts.
29  *
30  */
31 /*
32 History:
33 01.11.2009 Oesterholz created
34 27.12.2009 Oesterholz function compareDoubleFib() for comparing doubles added
35 13.01.2011 Oesterholz compareDouble() used
36 */
37 
38 
39 #ifndef T_COMPARE_BITS
40 #define T_COMPARE_BITS
41 
42 #include <iostream>
43 #include <fstream>
44 #include <ostream>
45 #include <istream>
46 #include <iomanip>
47 
48 #include "fibDatatyps.h"
49 
50 
51 /**
52  * This functions compares the byts stored in the file with the giben byts
53  * in the char array pcByts.
54  *
55  * @param szFileName the name of the file, wher the byts to compare with
56  * pcByts are stored
57  * @param pcByts the byts to compare with the byts in the file
58  * @param uiNumberofByts the number of byts to compare
59  * @return the number of errors occured in the test
60  */
61 int compareBytsWithFile( const char * szFileName, const char * pcByts,
62  const unsigned int uiNumberOfByts ){
63 
64  int iReturn = 0;//returnvalue of the test; the number of occured Errors
65 
66  cout<<endl<<"Comparing the given byts with the byts in the file: "<<endl;
67 
68  //load the byts from the file
69  unsigned int uiBytsLoaded = 0;
70  ifstream * file = new ifstream( szFileName , ios_base::in | ios_base::binary );
71 
72  if ( (file == NULL) || ( ! file->is_open()) ){
73  cerr<<"Error: Could not open the file "<< szFileName << " . "<<endl;
74  iReturn++;
75  return iReturn;
76  }
77 
78  char pcLoadedByts[ uiNumberOfByts + 8 ];
79 
80  while ( ! file->eof() && ! file->fail() && ( uiBytsLoaded < (uiNumberOfByts + 8) ) ){
81 
82  file->get( pcLoadedByts[ uiBytsLoaded ] );
83  uiBytsLoaded++;
84  }
85  //check loaded
86  if ( ! file->eof() ){
87  cerr<<"Error: Still more data to load, but "<< uiBytsLoaded <<
88  " byts loaded when yust "<< uiNumberOfByts <<
89  " byts to compare with . "<<endl;
90  iReturn++;
91  }else{
92  uiBytsLoaded--;//don't count last byte on eof
93  }
94  if ( file->bad() ){//TODO why file->fail() gives back false
95  cerr<<"Error: Error while loading data. "<<endl;
96  iReturn++;
97  }
98  delete file;
99 
100  if ( uiBytsLoaded != uiNumberOfByts ){
101  cerr<<"Error: Number of byts not equal, "<< uiBytsLoaded <<
102  " byts loaded when yust "<< uiNumberOfByts <<
103  " byts to compare with . "<<endl;
104  iReturn++;
105  }
106 
107  //print both
108  cout<<hex;
109  unsigned short usLineLenght = 32;
110  for ( unsigned int uiActualPosition = 0;
111  ( uiActualPosition < uiNumberOfByts ) ||
112  ( uiActualPosition < uiBytsLoaded );
113  uiActualPosition += usLineLenght ){
114 
115  cout<<"Original : ";
116  for ( unsigned short usLinePosition = 0;
117  usLinePosition < usLineLenght; usLinePosition++ ){
118 
119  if ( (uiActualPosition + usLinePosition) < uiNumberOfByts ){
120  cout<<"0x"<<setfill('0')<<setw(2)<<
121  (unsigned short)((unsigned char)pcByts[ uiActualPosition + usLinePosition ]) <<";";
122  }else{//no more chars to print
123  break;
124  }
125  }
126 
127  cout<<endl<<"File : ";
128  for ( unsigned short usLinePosition = 0;
129  usLinePosition < usLineLenght; usLinePosition++ ){
130 
131  if ( (uiActualPosition + usLinePosition) < uiBytsLoaded ){
132  cout<<"0x"<<setfill('0')<<setw(2)<<
133  (unsigned short)((unsigned char)pcLoadedByts[ uiActualPosition + usLinePosition ]) <<";";
134  }else{//no more chars to print
135  break;
136  }
137  }
138  cout<<endl;
139  }
140  cout<<dec;
141 
142  //compare the byts
143  for ( unsigned int uiActualPosition = 0;
144  ( uiActualPosition < uiNumberOfByts ) ||
145  ( uiActualPosition < uiBytsLoaded );
146  uiActualPosition++ ){
147 
148  if ( ( uiActualPosition < uiNumberOfByts ) && ( uiBytsLoaded <= uiActualPosition ) ){
149 
150  cerr<<"Error: The "<< uiActualPosition <<" byte could not be loaded. "<<endl;
151  iReturn++;
152  break;//can't compare more byts
153  }else if ( ( uiNumberOfByts <= uiActualPosition ) && ( uiActualPosition < uiBytsLoaded ) ){
154  cerr<<"Error: The "<< uiActualPosition <<" byte could be loaded, but isn't ther to compare. "<<endl;
155  iReturn++;
156  break;//can't compare more byts
157  }else{//compare the two byts
158  if ( pcByts[ uiActualPosition ] != pcLoadedByts[ uiActualPosition ] ){
159  cerr<<"Error: The "<< uiActualPosition <<"'th byte in the file is "<<
160  hex << "0x"<<setfill('0')<<setw(2)<<
161  (unsigned short)((unsigned char)pcLoadedByts[ uiActualPosition ]) << " but should be "<<
162  "0x"<<setfill('0')<<setw(2)<<
163  (unsigned short)((unsigned char)pcByts[ uiActualPosition ]) <<" "<<dec<<endl;
164  iReturn++;
165  }
166  }
167  }
168  cout<<dec;
169 
170  return iReturn;
171 }
172 
173 
174 /**
175  * This functions compares two double fib numbers.
176  * Realy small differences will be ignored.
177  *
178  * @param dValue1 the first number to compare
179  * @param dValue2 the second number to compare
180  * @return true if the first number is equal to the second, else false
181  */
182 bool compareDoubleFib( const doubleFib & dValue1, const doubleFib & dValue2 ){
183 
184  return fib::compareDouble( dValue1, dValue2 );
185 }
186 
187 
188 
189 
190 
191 //T_COMPARE_BITS
192 #endif
193