addbindata.cc

  1 const char *help = "\
  2 progname: addbindata.cc\n\
  3 code2html: This program concatenates 2 bindata files into one.\n\
  4 version: Torch3 vision2.0, 2003-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #define NBFILES_MAX 5000
  8 
  9 #include "DiskXFile.h"
 10 #include "CmdLine.h"
 11 
 12 using namespace Torch;
 13 
 14 int main(int argc, char *argv[])
 15 {
 16 	char *filename_in1;
 17 	char *filename_in2;
 18 	char *filename_out;
 19 	bool verbose;
 20 	
 21 	CmdLine cmd;
 22 	cmd.setBOption("write log", false);
 23 
 24 	cmd.info(help);
 25 	cmd.addText("\nArguments:");
 26 	cmd.addSCmdArg("filename in 1", &filename_in1, "input bindata filename 1");
 27 	cmd.addSCmdArg("filename in 2", &filename_in2, "input bindata filename 2");
 28 	cmd.addSCmdArg("filename out", &filename_out, "output bindata filename");
 29 	cmd.addText("\nArguments:");
 30 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 31 	cmd.read(argc, argv);
 32 
 33 	DiskXFile *file = NULL;
 34 	DiskXFile *fileout = NULL;
 35   
 36   	fileout = new DiskXFile(filename_out, "w");
 37   	if(fileout == NULL)
 38 	{
 39       		error("Opening BinData file %s", filename_out);
 40       
 41       		return 1;
 42 	}
 43 
 44   	// Reading headers
 45   	int P = 0;
 46 	int n_patterns;
 47 	int n_inputs, m;
 48 
 49 	{		
 50 		file = new DiskXFile(filename_in1, "r");
 51 		
 52 		file->read(&n_patterns, sizeof(int), 1);
 53 		file->read(&m, sizeof(int), 1);
 54 
 55 		if(verbose)
 56 		{
 57 			print("Reading bindata file (%s)\n", filename_in1);
 58 			print("   n_inputs = %d\n", m);
 59 			print("   n_patterns = %d\n", n_patterns);  
 60 		}
 61 
 62 		n_inputs = m;
 63 		P += n_patterns;  
 64 
 65 		delete file;
 66 		file = NULL;
 67 	}
 68   
 69 	{		
 70 		file = new DiskXFile(filename_in2, "r");
 71 		
 72 		file->read(&n_patterns, sizeof(int), 1);
 73 		file->read(&m, sizeof(int), 1);
 74 
 75 		if(verbose)
 76 		{
 77 			print("Reading bindata file (%s)\n", filename_in2);
 78 			print("   n_inputs = %d\n", m);
 79 			print("   n_patterns = %d\n", n_patterns);  
 80 		}
 81 
 82 		if(m != n_inputs)
 83 		{
 84 			delete fileout;
 85 			delete file;
 86 
 87 			error("Files to merge does not have the same input dimension");
 88 
 89 			return 1; 				  
 90 		}
 91 		
 92 		P += n_patterns;  
 93 
 94 		delete file;
 95 		file = NULL;
 96 	}
 97   
 98 	fileout->write(&P, sizeof(int), 1);
 99 	fileout->write(&n_inputs, sizeof(int), 1);
100 
101 	if(verbose)
102 	{
103   		print("\n");  
104   		print("Writing bindata file :\n");  
105   		print("   n_inputs : %d\n", n_inputs);
106   		print("   n_patterns : %d\n", P);  
107         }
108 
109 	real *input = new real [n_inputs];
110 	
111 	{		
112 		file = new DiskXFile(filename_in1, "r");
113 		
114 		file->read(&n_patterns, sizeof(int), 1);
115 		file->read(&n_inputs, sizeof(int), 1);
116 
117 		for(int p = 0 ; p < n_patterns ; p++)
118 		{		  
119 			file->read(input, sizeof(real), n_inputs);
120 			fileout->write(input, sizeof(real), n_inputs);
121 		}
122 
123 		delete file;
124 		file = NULL;
125 	}
126 	
127 	{		
128 		file = new DiskXFile(filename_in2, "r");
129 		
130 		file->read(&n_patterns, sizeof(int), 1);
131 		file->read(&n_inputs, sizeof(int), 1);
132 
133 		for(int p = 0 ; p < n_patterns ; p++)
134 		{		  
135 			file->read(input, sizeof(real), n_inputs);
136 			fileout->write(input, sizeof(real), n_inputs);
137 		}
138 
139 		delete file;
140 		file = NULL;
141 	}
142 	
143 	delete [] input;
144 	delete fileout;
145 
146 	return 0;
147 }
148