/usr/include/shogun/io/BinaryStream.h is in libshogun-dev 3.2.0-7.3build4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2009 Soeren Sonnenburg
* Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef __BINARYSTREAM_H__
#define __BINARYSTREAM_H__
#include <shogun/io/SGIO.h>
#include <shogun/base/SGObject.h>
#include <shogun/lib/memory.h>
#include <stdio.h>
namespace shogun
{
/** @brief memory mapped emulation via binary streams (files)
*
* Implements memory mapped file emulation (\sa CMemoryMappedFile) via standard
* file operations like fseek, fread etc
*/
template <class T> class CBinaryStream : public CSGObject
{
public:
/** default constructor
*/
CBinaryStream() : CSGObject()
{
rw=NULL;
m_fname=NULL;
fd = NULL;
length = 0;
set_generic<T>();
}
/** constructor
*
* open a file for read mode
*
* @param fname name of file, zero terminated string
* @param flag determines read or read write mode (currently only 'r' is supported)
*/
CBinaryStream(const char* fname, const char* flag="r")
: CSGObject()
{
/* open_stream(bs.m_fname, bs.rw); */
SG_NOTIMPLEMENTED
set_generic<T>();
}
/** copy constructor
*
* @param bs binary stream to copy from
*/
CBinaryStream(const CBinaryStream &bs)
{
open_stream(bs.m_fname, bs.rw);
ASSERT(length==bs.length)
set_generic<T>();
}
/** destructor */
virtual ~CBinaryStream()
{
close_stream();
}
/** open file stream
*
* @param fname file name
* @param flag flags "r" for reading etc
*/
void open_stream(const char* fname, const char* flag="r")
{
rw=get_strdup(flag);
m_fname=get_strdup(fname);
fd = fopen(fname, flag);
if (!fd)
SG_ERROR("Error opening file '%s'\n", m_fname)
struct stat sb;
if (stat(fname, &sb) == -1)
SG_ERROR("Error determining file size of '%s'\n", m_fname)
length = sb.st_size;
SG_DEBUG("Opened file '%s' of size %ld byte\n", fname, length)
}
/** close a file stream */
void close_stream()
{
SG_FREE(rw);
SG_FREE(m_fname);
if (fd)
fclose(fd);
rw=NULL;
m_fname=NULL;
fd = NULL;
length = 0;
}
/** get the number of objects of type T cointained in the file
*
* @return length of file
*/
uint64_t get_length()
{
return length/sizeof(T);
}
/** get the size of the file in bytes
*
* @return size of file in bytes
*/
uint64_t get_size()
{
return length;
}
/** get next line from file
*
* The returned line may be modfied in case the file was opened
* read/write. It is otherwise read-only.
*
* @param len length of line (returned via reference)
* @param offs offset to be passed for reading next line, should be 0
* initially (returned via reference)
*
* @return line (NOT ZERO TERMINATED)
*/
char* get_line(uint64_t& len, uint64_t& offs)
{
return NULL;
}
/** count the number of lines in a file
*
* @return number of lines
*/
int32_t get_num_lines()
{
return 0;
}
/** read num elements starting from index into buffer
*
* @param buffer buffer that has to be at least num elements long
* @param index index into file starting from which elements are read
* @param num number of elements to be read
*/
void pre_buffer(T* buffer, long index, long num) const
{
ASSERT(index>=0)
ASSERT(num>=0)
if (num==0)
return;
if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname)
if ( fread(buffer, sizeof(T), num, fd) != num)
SG_ERROR("Error calling fread (file '%s')\n", m_fname)
}
/** read next
*
* @return next element
*/
inline T read_next() const
{
T ptr;
if ( fread(&ptr, sizeof(T), 1, fd) != 1)
{
fprintf(stderr, "Error calling fread (file '%s')\n", m_fname);
exit(1);
}
return ptr;
}
/** operator overload for file read only access
*
* @param index index
* @return element at index
*/
inline T operator[](int32_t index) const
{
if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname)
T ptr;
if ( fread(&ptr, sizeof(T), 1, fd) != 1)
SG_ERROR("Error calling fread (file '%s')\n", m_fname)
return ptr;
}
/** @return object name */
virtual const char* get_name() const { return "BinaryStream"; }
protected:
/** file descriptor */
FILE* fd;
/** size of file */
uint64_t length;
/** mode */
char* rw;
/** fname */
char* m_fname;
};
}
#endif // BINARY_STREAM
|