00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "Core/precomp.h"
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <iostream>
00015 #include <string.h>
00016
00017 #include <sys/types.h>
00018 #include <sys/stat.h>
00019 #include <fcntl.h>
00020
00021 #ifdef WIN32
00022 #include <io.h>
00023 #else
00024 #include <unistd.h>
00025 #endif
00026
00027 #include "datafile_writer.h"
00028
00029 #ifndef WIN32
00030 #define OPEN_FLAGS O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
00031 #else
00032 #define OPEN_FLAGS _O_RDWR | O_CREAT | O_TRUNC | O_BINARY, _S_IREAD | _S_IWRITE
00033 #endif
00034
00035
00036
00037
00038
00039 OutputSourceProvider_Datafile::OutputSourceProvider_Datafile(const char *_filename)
00040 {
00041 filename = _filename;
00042 datafile_fd = open(filename, OPEN_FLAGS);
00043
00044 int index_pos = -1;
00045 write(datafile_fd, datafile_id, strlen(datafile_id));
00046 write(datafile_fd, &index_pos, sizeof(int));
00047 }
00048
00049 OutputSourceProvider_Datafile::~OutputSourceProvider_Datafile()
00050 {
00051 int index_pos = lseek(datafile_fd, 0, SEEK_CUR);
00052 int num_indexes = indexes.size();
00053
00054 write(datafile_fd, &num_indexes, sizeof(int));
00055
00056 for (
00057 std::list<DatafileIndex*>::iterator it = indexes.begin();
00058 it != indexes.end();
00059 it++)
00060 {
00061 int name_len = (*it)->name.get_length()+1;
00062 write(datafile_fd, &name_len, sizeof(short));
00063 write(datafile_fd, (*it)->name, name_len);
00064 write(datafile_fd, &(*it)->pos, sizeof(int));
00065 write(datafile_fd, &(*it)->size, sizeof(int));
00066 }
00067
00068 lseek(datafile_fd, strlen(datafile_id), SEEK_SET);
00069 write(datafile_fd, &index_pos, sizeof(int));
00070
00071 close(datafile_fd);
00072 }
00073
00074 gzFile OutputSourceProvider_Datafile::create_index(const char *index)
00075 {
00076 indexes.push_back(
00077 new DatafileIndex(
00078 index,
00079 lseek(datafile_fd, 0, SEEK_CUR)));
00080 return gzdopen(dup(datafile_fd), "w+b");
00081 }
00082
00083 void OutputSourceProvider_Datafile::close_index(gzFile gz_fd, int size)
00084 {
00085 gzclose(gz_fd);
00086
00087
00088 indexes.back()->size = size;
00089
00090
00091
00092
00093
00094
00095
00096 }
00097
00098 CL_OutputSource *OutputSourceProvider_Datafile::open_source(const char *handle)
00099 {
00100 return new OutputSource_Datafile(handle, this);
00101 }
00102
00103 CL_OutputSourceProvider *OutputSourceProvider_Datafile::clone()
00104 {
00105 return NULL;
00106 }
00107
00108
00109
00110
00111
00112 OutputSource_Datafile::OutputSource_Datafile(
00113 const char *handle,
00114 OutputSourceProvider_Datafile *_provider)
00115 {
00116 provider = _provider;
00117 output_fd = provider->create_index(handle);
00118 pos = 0;
00119 }
00120
00121 OutputSource_Datafile::~OutputSource_Datafile()
00122 {
00123 provider->close_index(output_fd, pos);
00124 }
00125
00126 void OutputSource_Datafile::set_system_mode()
00127 {
00128
00129 }
00130
00131 void OutputSource_Datafile::set_big_endian_mode()
00132 {
00133
00134 }
00135
00136 void OutputSource_Datafile::set_little_endian_mode()
00137 {
00138
00139 }
00140
00141 void OutputSource_Datafile::write_int32(int data)
00142 {
00143 write(&data, sizeof(int));
00144 }
00145
00146 void OutputSource_Datafile::write_uint32(unsigned int data)
00147 {
00148 write(&data, sizeof(unsigned int));
00149 }
00150
00151 void OutputSource_Datafile::write_short16(short data)
00152 {
00153 write(&data, sizeof(short));
00154 }
00155
00156 void OutputSource_Datafile::write_ushort16(unsigned short data)
00157 {
00158 write(&data, sizeof(unsigned short));
00159 }
00160
00161 void OutputSource_Datafile::write_char8(char data)
00162 {
00163 write(&data, sizeof(char));
00164 }
00165
00166 void OutputSource_Datafile::write_uchar8(unsigned char data)
00167 {
00168 write(&data, sizeof(unsigned char));
00169 }
00170
00171 void OutputSource_Datafile::write_float32(float data)
00172 {
00173 write(&data, sizeof(float));
00174 }
00175
00176 int OutputSource_Datafile::write(const void *data, int size)
00177 {
00178 gzwrite(output_fd, (void *) data, size);
00179 pos += size;
00180 return size;
00181 }
00182
00183 void OutputSource_Datafile::open()
00184 {
00185
00186 }
00187
00188 void OutputSource_Datafile::close()
00189 {
00190
00191 }
00192
00193 CL_OutputSource *OutputSource_Datafile::clone()
00194 {
00195 return NULL;
00196 }
00197
00198 int OutputSource_Datafile::tell()
00199 {
00200 return pos;
00201 }
00202
00203 int OutputSource_Datafile::size()
00204 {
00205 return pos;
00206 }
00207
00208 void OutputSource_Datafile::write_string(const char *string)
00209 {
00210 int size = strlen(string)+1;
00211 write(&size, sizeof(int));
00212 write((void *) string, size);
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388