addfilebindata.cc
1 const char *help = "\
2 progname: addfilebindata.cc\n\
3 code2html: This program concatenates several bindata files into one bindata file.\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 "FileListCmdOption.h"
11 #include "CmdLine.h"
12
13 using namespace Torch;
14
15 int main(int argc, char *argv[])
16 {
17 char *filename_out;
18 bool verbose;
19
20 FileListCmdOption filelist("file name", "the list files or one data file");
21 filelist.isArgument(true);
22
23 CmdLine cmd;
24
25 cmd.info(help);
26 cmd.addText("\nArguments:");
27 cmd.addCmdOption(&filelist);
28 cmd.addSCmdArg("filename out", &filename_out, "output bindata filename");
29 cmd.addText("\nOptions:");
30 cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
31 cmd.read(argc, argv);
32
33 //
34 if(verbose)
35 {
36 print(" + n_filenames = %d\n", filelist.n_files);
37 for(int i = 0 ; i < filelist.n_files ; i++)
38 print(" filename[%d] = %s\n", i, filelist.file_names[i]);
39 }
40
41 if(filelist.n_files > NBFILES_MAX)
42 {
43 error("Cannot merge more than %d files", NBFILES_MAX);
44
45 return 1;
46 }
47
48 DiskXFile *file = NULL;
49 DiskXFile *fileout = NULL;
50
51 if(verbose)
52 print("Merging %d files :\n", filelist.n_files);
53
54 fileout = new DiskXFile(filename_out, "w");
55 if(fileout == NULL)
56 {
57 error("Opening BinData file %s", filename_out);
58
59 return 1;
60 }
61
62 // Reading headers
63 int P = 0;
64 int n_patterns;
65 int n_inputs, m;
66
67 for(int i = 0 ; i < filelist.n_files ; i++)
68 {
69 file = new DiskXFile(filelist.file_names[i], "r");
70
71 file->read(&n_patterns, sizeof(int), 1);
72 file->read(&m, sizeof(int), 1);
73
74 if(verbose)
75 {
76 print("Reading bindata file (%s)\n", filelist.file_names[i]);
77 print(" n_inputs = %d\n", n_inputs);
78 print(" n_patterns = %d\n", n_patterns);
79 }
80
81 if(i == 0)
82 {
83 n_inputs = m;
84 }
85 else
86 {
87 if(m != n_inputs)
88 {
89 delete fileout;
90 delete file;
91
92 error("Files to merge does not have the same input dimension");
93
94 return 1;
95 }
96 }
97
98 P += n_patterns;
99
100 delete file;
101 file = NULL;
102 }
103
104 fileout->write(&P, sizeof(int), 1);
105 fileout->write(&n_inputs, sizeof(int), 1);
106
107 if(verbose)
108 {
109 print("\n");
110 print("Writing bindata file :\n");
111 print(" n_inputs : %d\n", n_inputs);
112 print(" n_patterns : %d\n", P);
113 }
114
115 real *input = new real [n_inputs];
116
117 for(int i = 0 ; i < filelist.n_files ; i++)
118 {
119 file = new DiskXFile(filelist.file_names[i], "r");
120
121 file->read(&n_patterns, sizeof(int), 1);
122 file->read(&n_inputs, sizeof(int), 1);
123
124 for(int p = 0 ; p < n_patterns ; p++)
125 {
126 file->read(input, sizeof(real), n_inputs);
127 fileout->write(input, sizeof(real), n_inputs);
128 }
129
130 delete file;
131 file = NULL;
132 }
133
134 delete [] input;
135 delete fileout;
136
137 return 0;
138 }
139