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

nl-stream.h « include « rvtl « hhmm « synlm « contrib - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f743e12b3e6151787d94944e5cb13a29048f247 (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
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
///////////////////////////////////////////////////////////////////////////////
//                                                                           //
// This file is part of ModelBlocks. Copyright 2009, ModelBlocks developers. //
//                                                                           //
//    ModelBlocks 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.                                    //
//                                                                           //
//    ModelBlocks is distributed in the hope that it will be useful,         //
//    but WITHOUT ANY WARRANTY; without even the implied warranty of         //
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          //
//    GNU General Public License for more details.                           //
//                                                                           //
//    You should have received a copy of the GNU General Public License      //
//    along with ModelBlocks.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//    ModelBlocks developers designate this particular file as subject to    //
//    the "Moses" exception as provided by ModelBlocks developers in         //
//    the LICENSE file that accompanies this code.                           //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

#ifndef _NL_STREAM__
#define _NL_STREAM__

#include "nl-string.h"


////////////////////////////////////////////////////////////////////////////////

class IStreamSource {
 private:

  // Data members...
  String       sBuff;  // string buffer, to allow re-reading in disjunctions
  unsigned int iAvail; // index of where string buffer begins; subtract from indices in IStream to get buffer index
  FILE*        pfSrc;  // file pointer

 public:

  IStreamSource ( FILE* pf )        : sBuff(256), iAvail(0), pfSrc(pf) { }
  IStreamSource ( const char* psz ) : sBuff(psz), iAvail(0), pfSrc()   { }

  char* c_array ( )                { return sBuff.c_array(); }
  char  get     ( unsigned int i ) { assert(i-iAvail<((unsigned int)(-1))/2);  // NOTE: PROBLEM IF ANY ISTREAM FALLS BEHIND BY MORE THAN MAXINT CHARACTERS!
                                     int c=0; while(i-iAvail>=sBuff.size()-1){sBuff.add()=((c=getc(pfSrc))==EOF)?'\0':c;} return sBuff.get(i-iAvail); }
  char& set     ( unsigned int i ) { assert(i-iAvail<((unsigned int)(-1))/2);  // NOTE: PROBLEM IF ANY ISTREAM FALLS BEHIND BY MORE THAN MAXINT CHARACTERS!
                                     int c=0; while(i-iAvail>=sBuff.size()-1){sBuff.add()=((c=getc(pfSrc))==EOF)?'\0':c;} return sBuff.set(i-iAvail); }
  FILE* getFile ( )                { return pfSrc; }

  // Output method...
  friend ostream& operator<< ( ostream& os, const IStreamSource& iss ) { return os<<"'"<<iss.sBuff<<"',"<<iss.iAvail<<","<<iss.pfSrc; }

  void compress ( ) { iAvail+=strlen(sBuff.c_array()); sBuff=String(256); }    // NOTE: NOT TESTED WITH FILES EXCEEDING MAXINT CHARACTERS! (BUT SHOULD WORK)
};


////////////////////////////////////////////////////////////////////////////////

class IStream {
 private:

  IStreamSource* psrc;   // pointer to source of stream, which contains buffer and file pointer
  unsigned int   iIndex; // stream index; subtract psrc->iAvail to get index in psrc->sBuff

  char  get     ( unsigned int i )   { return psrc->get(i);  }
  char& set     ( unsigned int i )   { return psrc->set(i);  }
  char* c_array ( unsigned int i=0 ) { return &psrc->set(i); }

 public:

  IStream ( )                                      : psrc(NULL),    iIndex(0) { }
  IStream ( IStreamSource& src, unsigned int i=0 ) : psrc(&src),    iIndex(i) { }
  IStream ( const IStream& is,  unsigned int i   ) : psrc(is.psrc), iIndex(i) { }

  // Specification methods...
  IStream& operator= ( const IStream& is ) { psrc=is.psrc; iIndex=is.iIndex; return *this; }

  // Extraction methods...
  bool operator== ( const IStream& is ) const { return (psrc==is.psrc && iIndex==is.iIndex); }
  bool operator!= ( const IStream& is ) const { return (psrc!=is.psrc || iIndex!=is.iIndex); }
  IStreamSource* getSource() { return psrc; }

  // Output method...
  friend ostream& operator<< ( ostream& os, const IStream& is ) { return os<<is.iIndex<<","<<is.psrc<<","<<*is.psrc; }

  // Match single char...
  friend IStream operator>> ( IStream is, char& c ) { 
    // Propagate fail...
    if (IStream()==is) return IStream();
    c=is.get(is.iIndex);
    return ('\0'==c) ? IStream() : IStream(is,is.iIndex+1);
  }

  // Match string delimiter...
  friend IStream operator>> ( IStream is, const char* psDlm ) {
    // Propagate fail...
    if (IStream()==is) { return IStream(); }
    unsigned int i;
    // Read in characters until fail or match delimiter...
    for (i=0; is.get(i+is.iIndex)!='\0' && psDlm[i]!='\0'; i++)
      if (is.get(i+is.iIndex)!=psDlm[i]) return IStream();
    return (psDlm[i]=='\0') ? IStream(is,i+is.iIndex) : IStream();
  }

  // Match anything else followed by zero-terminated string delimiter...
  template<class X> friend pair<IStream,X*> operator>> ( IStream is, X& x ) { return pair<IStream,X*>(is,&x); }
  template<class X> friend IStream          operator>> ( pair<IStream,X*> is_x, const char* psDlm ) { 
    IStream& is =  is_x.first;
    X&       x  = *is_x.second;
    // Propagate fail...
    if (IStream()==is) { return IStream(); }
    unsigned int d = strlen(psDlm);
    // Read in characters until fail or match delimiter...
    for (unsigned int i=is.iIndex; is.get(i)!='\0'; i++) {
      // Try to match delimiter ending at current point in stream...
      if ( i-is.iIndex+1>=d ) {
        unsigned int j;
        for (j=0; j<d && is.get(i+1+j-d)==psDlm[j]; j++);
        ////cerr<<"-->i="<<i<<",j="<<j<<",d="<<d<<",delim='"<<psDlm<<"',curr='"<<is.get(i)<<"'  "<<is<<"\n";
        if (j==d) {
          is.set(i+1-d)='\0'; x=X(is.c_array(is.iIndex)); is.set(i+1-d)=psDlm[0];
          return IStream(is,i+1);
        }
      }
    }
    return IStream();
  }

  // Match integer followed by zero-terminated string delimiter...
  friend IStream operator>> ( pair<IStream,int*> is_x, const char* psDlm ) { 
    IStream& is =  is_x.first;
    int&     x  = *is_x.second;
    // Propagate fail...
    if (IStream()==is) return IStream();
    unsigned int d = strlen(psDlm);
    // Read in characters until fail or match delimiter...
    for (unsigned int i=is.iIndex; is.get(i)!='\0'; i++) {
      // Try to match delimiter ending at current point in stream...
      if ( i+1>=is.iIndex+d ) {
        unsigned int j;
        for (j=0; j<d && is.get(i+1+j-d)==psDlm[j]; j++);
        if (j==d) {
          is.set(i+1-d)='\0'; x=atoi(is.c_array(is.iIndex)); is.set(i+1-d)=psDlm[0];
          return IStream(is,i+1);
        }
      }
    }
    return IStream();
  }

  // Match unsigned int followed by zero-terminated string delimiter...
  friend IStream operator>> ( pair<IStream,unsigned int*> is_x, const char* psDlm ) { 
    IStream& is     =  is_x.first;
    unsigned int& x = *is_x.second;
    // Propagate fail...
    if (IStream()==is) return IStream();
    unsigned int d = strlen(psDlm);
    // Read in characters until fail or match delimiter...
    for (unsigned int i=is.iIndex; is.get(i)!='\0'; i++) {
      // Try to match delimiter ending at current point in stream...
      if ( i+1>=is.iIndex+d ) {
        unsigned int j;
        for (j=0; j<d && is.get(i+1+j-d)==psDlm[j]; j++);
        if (j==d) {
          is.set(i+1-d)='\0'; x=atoi(is.c_array(is.iIndex)); is.set(i+1-d)=psDlm[0];
          return IStream(is,i+1);
        }
      }
    }
    return IStream();
  }

  // Match float followed by zero-terminated string delimiter...
  friend IStream operator>> ( pair<IStream,float*> is_x, const char* psDlm ) { 
    IStream& is =  is_x.first;
    float&  x  = *is_x.second;
    // Propagate fail...
    if (IStream()==is) return IStream();
    unsigned int d = strlen(psDlm);
    // Read in characters until fail or match delimiter...
    for (unsigned int i=is.iIndex; is.get(i)!='\0'; i++) {
      // Try to match delimiter ending at current point in stream...
      if ( i+1>=is.iIndex+d ) {
        unsigned int j;
        for (j=0; j<d && is.get(i+1+j-d)==psDlm[j]; j++);
        if (j==d) {
          is.set(i+1-d)='\0'; x=atof(is.c_array(is.iIndex)); is.set(i+1-d)=psDlm[0];
          return IStream(is,i+1);
        }
      }
    }
    return IStream();
  }

  // Match double followed by zero-terminated string delimiter...
  friend IStream operator>> ( pair<IStream,double*> is_x, const char* psDlm ) { 
    IStream& is =  is_x.first;
    double&  x  = *is_x.second;
    // Propagate fail...
    if (IStream()==is) return IStream();
    unsigned int d = strlen(psDlm);
    // Read in characters until fail or match delimiter...
    for (unsigned int i=is.iIndex; is.get(i)!='\0'; i++) {
      // Try to match delimiter ending at current point in stream...
      if ( i+1>=is.iIndex+d ) {
        unsigned int j;
        for (j=0; j<d && is.get(i+1+j-d)==psDlm[j]; j++);
        if (j==d) {
          is.set(i+1-d)='\0'; x=atof(is.c_array(is.iIndex)); is.set(i+1-d)=psDlm[0];
          return IStream(is,i+1);
        }
      }
    }
    return IStream();
  }

  // Match void pointer followed by zero-terminated string delimiter...
  friend IStream operator>> ( pair<IStream,void**> is_x, const char* psDlm ) { 
    IStream& is =  is_x.first;
    // Propagate fail...
    if (IStream()==is) return IStream();
    unsigned int d = strlen(psDlm);
    // Read in characters until fail or match delimiter...
    for (unsigned int i=is.iIndex; is.get(i)!='\0'; i++) {
      // Try to match delimiter ending at current point in stream...
      if ( i+1>=is.iIndex+d ) {
        unsigned int j;
        for (j=0; j<d && is.get(i+1+j-d)==psDlm[j]; j++);
        if (j==d) return IStream(is,i+1);
      }
    }
    return IStream();
  }
};


#endif //_NL_STREAM__