Welcome to mirror list, hosted at ThFree Co, Russian Federation.

File.h « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb5f7f875b0c8ba841af7826a2e466cdcd0c97ae (plain)
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
// $Id$

/* ---------------------------------------------------------------- */
/* Copyright 2005 (c) by RWTH Aachen - Lehrstuhl fuer Informatik VI */
/* Richard Zens                                                     */
/* ---------------------------------------------------------------- */
#ifndef FILE_H_
#define FILE_H_
#include <cstdio>
#include <vector>

static const off_t InvalidOffT=-1;

//  WARNING:
//    these functions work only for bitwise read/write-able types

template<typename T> inline size_t fWrite(FILE* f,const T& t) {
  if(fwrite(&t,sizeof(t),1,f)!=1) {
    std::cerr<<"ERROR:: fwrite!\n";abort();}
  return sizeof(t);
}

template<typename T> inline void fRead(FILE* f,T& t)  {
  if(fread(&t,sizeof(t),1,f)!=1) {std::cerr<<"ERROR: fread!\n";abort();}
}

template<typename T> inline size_t fWrite(FILE* f,const T* b,const T* e) {
  unsigned s=e-b;size_t rv=fWrite(f,s);
  if(fwrite(b,sizeof(T),s,f)!=s) {std::cerr<<"ERROR: fwrite!\n";abort();}
  return rv+sizeof(T)*s;
}

template<typename T> inline size_t fWrite(FILE* f,const T b,const T e) {
  unsigned s=std::distance(b,e);size_t rv=fWrite(f,s);
  if(fwrite(&(*b),sizeof(T),s,f)!=s) {std::cerr<<"ERROR: fwrite!\n";abort();}
  return rv+sizeof(T)*s;
}

template<typename C> inline size_t fWriteVector(FILE* f,const C& v) {
  unsigned s=v.size();
  size_t rv=fWrite(f,s);
  if(fwrite(&v[0],sizeof(typename C::value_type),s,f)!=s) {std::cerr<<"ERROR: fwrite!\n";abort();}
  return rv+sizeof(typename C::value_type)*s;
}

template<typename C> inline void fReadVector(FILE* f, C& v) {
  unsigned s;fRead(f,s);v.resize(s);
  unsigned r=fread(&(*v.begin()),sizeof(typename C::value_type),s,f);
  if(r!=s) {
    std::cerr<<"ERROR: freadVec! "<<r<<" "<<s<<"\n";abort();}
}

#ifdef WIN32
inline off_t fTell(FILE* f) {return ftell(f);}

inline void fSeek(FILE* f,off_t o) {
  if(fseek(f,o,SEEK_SET)<0) {
    std::cerr<<"ERROR: could not fseeko position "<<o<<"\n";
    if(o==InvalidOffT) std::cerr<<"You tried to seek for 'InvalidOffT'!\n";
    abort();
  }
}
#else
inline off_t fTell(FILE* f) {return ftello(f);}

inline void fSeek(FILE* f,off_t o) {
  if(fseeko(f,o,SEEK_SET)<0) {
    std::cerr<<"ERROR: could not fseeko position "<<o<<"\n";
    if(o==InvalidOffT) std::cerr<<"You tried to seek for 'InvalidOffT'!\n";
    abort();
  }
}
#endif

inline FILE* fOpen(const char* fn,const char* m) {
  if(FILE* f=fopen(fn,m)) return f; else {
    std::cerr<<"ERROR: could not open file "<<fn<<" with mode "<<m<<"\n";
    abort();}
}
inline void fClose(FILE* f) {fclose(f);} // for consistent function names only

#endif