forked from hschwenk/cslm-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolsgz.cpp
172 lines (146 loc) · 3.85 KB
/
Toolsgz.cpp
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
/*
* This file is part of the continuous space language and translation model toolkit
* for statistical machine translation and large vocabulary speech recognition.
*
* Copyright 2015, Holger Schwenk, LIUM, University of Le Mans, France
*
* The CSLM toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*
*/
using namespace std;
#include <stdexcept>
#include <stdlib.h>
#include "Tools.h"
#include "Toolsgz.h"
int Weights::Read(const char *fname)
{
ifstream wftmp;
wftmp.open(fname);
if (wftmp.fail())
Error("ERROR");
float f;
while (wftmp >> f) val.push_back(f);
wftmp.close();
return val.size();
}
Weights::Weights(const char *fname)
{
wf.open(fname);
if (wf.fail())
Error("ERROR");
}
int Weights::ScanLine()
{
if (wf.eof()) Error("Weights::ScanLine() called without open file");
debug0("scan: waiting...\n");
string line;
getline(wf,line);
if (!wf.good()) return 0;
printf("scan: got '%s'\n",line.c_str());
if (line.size()==0) return 0;
// parse the values
uint pos=0, epos;
val.clear();
while ((epos=line.find(WEIGHT_DELIM,pos))<100000) {
val.push_back(Scan<float>(line.substr(pos,epos-pos)));
pos=epos+strlen(WEIGHT_DELIM);
}
val.push_back(Scan<float>(line.substr(pos,line.size())));
printf("scan: parsed into %d values\n",(int) val.size());
wf.clear();
return val.size();
}
//
//
//
void gzifstream::open(char *fname)
{
//check if file is readable
std::filebuf* fb = new std::filebuf();
_fail=(fb->open(fname, std::ios::in)==NULL);
char *sptr=strrchr(fname,'.');
if (sptr && strcmp(sptr,".gz")==0) {
fb->close(); delete fb;
gz_streambuf = new gzfilebuf(fname);
} else {
gz_streambuf = fb;
}
this->init(gz_streambuf);
}
void gzofstream::open(char *fname)
{
//check if file is readable
std::filebuf* fb = new std::filebuf();
_fail=(fb->open(fname, std::ios::out)==NULL);
//cerr << "fail: " << _fail <<endl;
char *sptr=strrchr(fname,'.');
if (sptr && strcmp(sptr,".gz")==0) {
fb->close(); delete fb;
gz_streambuf = new gzfilebuf(fname);
} else {
gz_streambuf = fb;
}
this->init(gz_streambuf);
}
//
//
//
inputfilestream::inputfilestream(const std::string &filePath)
: std::istream(0),
m_streambuf(0)
{
//check if file is readable
std::filebuf* fb = new std::filebuf();
_good=(fb->open(filePath.c_str(), std::ios::in)!=NULL);
if (filePath.size() > 3 &&
filePath.substr(filePath.size() - 3, 3) == ".gz")
{
fb->close(); delete fb;
m_streambuf = new gzfilebuf(filePath.c_str());
} else {
m_streambuf = fb;
}
this->init(m_streambuf);
}
inputfilestream::~inputfilestream()
{
delete m_streambuf; m_streambuf = 0;
}
void inputfilestream::close()
{
}
outputfilestream::outputfilestream(const std::string &filePath)
: std::ostream(0),
m_streambuf(0)
{
//check if file is readable
std::filebuf* fb = new std::filebuf();
_good=(fb->open(filePath.c_str(), std::ios::out)!=NULL);
if (filePath.size() > 3 && filePath.substr(filePath.size() - 3, 3) == ".gz")
{
fb->close(); delete fb;
m_streambuf = new gzfilebuf(filePath.c_str(), 0);
} else {
m_streambuf = fb;
}
this->init(m_streambuf);
}
outputfilestream::~outputfilestream()
{
delete m_streambuf; m_streambuf = 0;
}
void outputfilestream::close()
{
}