File: DiskXFile.cc

package info (click to toggle)
torch3 3.1-0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,940 kB
  • ctags: 2,744
  • sloc: cpp: 24,245; python: 299; makefile: 153
file content (255 lines) | stat: -rw-r--r-- 5,461 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (C) 2003--2004 Ronan Collobert (collober@idiap.ch)
//                
// This file is part of Torch 3.1.
//
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "DiskXFile.h"
#include "string_utils.h"
#ifdef _MSC_VER
#include <fcntl.h>
#endif

namespace Torch {

bool DiskXFile::is_native_mode = true;

DiskXFile::DiskXFile(const char *file_name, const char *open_flags)
{
#ifdef _MSC_VER
  _fmode = _O_BINARY;
#endif

  its_a_pipe = false;
  bool zipped_file = false;
  if(!strcmp(open_flags, "r"))
  {
    if(strlen(file_name) > 3)
    {
      if(strcmp(file_name+strlen(file_name)-3, ".gz"))
        zipped_file = false;
      else
        zipped_file = true;
    }
    else
      zipped_file = false;

    if(zipped_file)
    {
      char *cmd_buffer = strConcat(2, "zcat ", file_name);
      file = fopen(file_name, "r");
      if(!file)
        error("DiskXFile: cannot open the file <%s> for reading", file_name);
      fclose(file);

      file = popen(cmd_buffer, open_flags);
      if(!file)
        error("DiskXFile: cannot execute the command <%s>", file_name, cmd_buffer);
      free(cmd_buffer);
    }
  }

  if(!zipped_file)
  {
    file = fopen(file_name, open_flags);
    if(!file)
      error("DiskXFile: cannot open <%s> in mode <%s>. Sorry.", file_name, open_flags);
  }
  is_opened = true;

  // Buffer
  buffer_block = NULL;
  buffer_block_size = 0;
}

DiskXFile::DiskXFile(FILE *file_)
{
  file = file_;
  is_opened = false;
  its_a_pipe = false;

  // Buffer
  buffer_block = NULL;
  buffer_block_size = 0;
}

int DiskXFile::read(void *ptr, int block_size, int n_blocks)
{
  int melanie = fread(ptr, block_size, n_blocks, file);

  if(!is_native_mode)
    reverseMemory(ptr, block_size, n_blocks);

  return(melanie);
}

int DiskXFile::write(void *ptr, int block_size, int n_blocks)
{
  if(!is_native_mode)
    reverseMemory(ptr, block_size, n_blocks);

  int melanie = fwrite(ptr, block_size, n_blocks, file);

  if(!is_native_mode)
    reverseMemory(ptr, block_size, n_blocks);

  return(melanie);
}

int DiskXFile::eof()
{
  return(feof(file));
}

int DiskXFile::flush()
{
  return(fflush(file));
}

int DiskXFile::seek(long offset, int whence)
{
  return(fseek(file, offset, whence));
}

long DiskXFile::tell()
{
  return(ftell(file));
}

void DiskXFile::rewind()
{
  ::rewind(file);
}

int DiskXFile::printf(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  int res = vfprintf(file, format, args);
  va_end(args);
  return(res);
}

int DiskXFile::scanf(const char *format, void *ptr)
{
  int res = fscanf(file, format, ptr);
  return(res);
}

char *DiskXFile::gets(char *dest, int size_)
{
  return(fgets(dest, size_, file));
}

//-----

bool DiskXFile::isLittleEndianProcessor()
{
  int x = 7;
  char *ptr = (char *)&x;

  if(ptr[0] == 0)
    return(false);
  else
    return(true);
}

bool DiskXFile::isBigEndianProcessor()
{
  return(!isLittleEndianProcessor());
}

bool DiskXFile::isNativeMode()
{
  return(is_native_mode);
}

void DiskXFile::setNativeMode()
{
  is_native_mode = true;
}

void DiskXFile::setLittleEndianMode()
{
  if(isLittleEndianProcessor())
    is_native_mode = true;
  else
    is_native_mode = false;
}

void DiskXFile::setBigEndianMode()
{
  if(isBigEndianProcessor())
    is_native_mode = true;
  else
    is_native_mode = false;
}

void DiskXFile::reverseMemory(void *ptr_, int block_size, int n_blocks)
{
  if(block_size == 1)
    return;

  char *ptr = (char *)ptr_;
  char *ptrr, *ptrw;

  if(block_size > buffer_block_size)
  {
    allocator->free(buffer_block);
    buffer_block = (char *)allocator->alloc(block_size);
  }

  for(int i = 0; i < n_blocks; i++)
  {
    ptrr = ptr + ((i+1)*block_size);
    ptrw = buffer_block;

    for(int j = 0; j < block_size; j++)
    {
      ptrr--;
      *ptrw++ = *ptrr;
    }

    ptrr = buffer_block;
    ptrw = ptr + (i*block_size);
    for(int j = 0; j < block_size; j++)
      *ptrw++ = *ptrr++;
  }
}

//-----

DiskXFile::~DiskXFile()
{
  if(is_opened)
  {
    if(its_a_pipe)
      pclose(file);
    else
      fclose(file);
  }
}

}