2 * Copyright (C) 2004-2006 Felipe Sánchez-Martínez
3 * Copyright (C) 2006 Universitat d'Alacant
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * Utility functions. (source file)
23 * @author Felipe Sánchez-Martínez - fsanchez@dlsi.ua.es
31 #include <sys/types.h>
34 #include <sys/times.h>
35 #include <sys/resource.h>
37 #include <apertium/utf_converter.h>
41 string Utils::translation_script;
42 string Utils::likelihood_script;
43 string Utils::translation_script2;
44 string Utils::likelihood_script2;
48 Utils::translate(string script, const wstring& s) {
49 int fd_in[2]; //Pipe descriptor (the father writes text to the child)
50 int fd_out[2];//Pipe descriptor (the child writes the result to the father)
53 cerr<<"Error creating input pipe\n";
55 cerr<<"Error creating output pipe\n";
57 if (fork()==0) { //Code for the child
63 //Those descriptor that will not be use are closed
67 execlp(script.c_str(), script.c_str(), NULL);
69 //If execution arrives at this point, we are in trouble
74 } else { //Code for the father
75 //Those descriptor that will not be used are closed
83 //write(fd_in[1], (void*)s.c_str(), s.length());
85 FILE *fw=fdopen(fd_in[1], "w");
86 for (int i=0; i<s.length(); i++)
91 wait (&status); //Waiting for the child to finish
93 cerr<<"Error: Child process could not execute the translation script.\n";
94 cerr<<"Input string: ["<<UtfConverter::toUtf8(s)<<"]\n";
99 //Now the father reads the result from the child
103 // nread=read(fd_out[0], buf, 255);
106 //} while (nread==255);
109 FILE *fr=fdopen(fd_out[0], "r");
123 Utils::likelihood(string script, const wstring& s) {
124 int fd_in[2]; //Pipe descriptor (the father writes text to the child)
125 int fd_out[2];//Pipe descriptor (the child writes the result to the father)
127 string str=UtfConverter::toUtf8(s);
130 cerr<<"Error creating input pipe\n";
132 cerr<<"Error creating output pipe\n";
134 if (fork()==0) { //Code for the child
140 //Those descriptor that will not be use are closed
144 execlp(script.c_str(), script.c_str(), NULL);
146 //If execution arrives at this point, we are in trouble
151 } else { //Code for the father
155 //Those descriptor that will not be use are closed
160 write(fd_in[1], (void*)str.c_str(), str.length());
162 wait (&status); //Waiting for the child to finish
164 cerr<<"Error: Child process could not execute the likelihood script.\n";
165 cerr<<"Input string: ["<<str<<"]\n";
170 //Now the father reads the result from the child
174 nread=read(fd_out[0], buf, 255);
177 } while (nread==255);
180 //cerr<<"CAD:"<<ret.c_str()<<endl;
181 return atof(ret.c_str());
186 Utils::get_usage_time()
190 getrusage(RUSAGE_SELF,&usage);
191 t=usage.ru_utime.tv_sec+usage.ru_stime.tv_sec;
193 getrusage(RUSAGE_CHILDREN,&usage);
194 t+=usage.ru_utime.tv_sec+usage.ru_stime.tv_sec;
200 Utils::split_wstring(const wstring& input, const wstring& delimiter) {
203 vector<wstring> result;
207 while (pos<input.size()) {
208 new_pos=input.find(delimiter, pos);
210 new_pos=input.size();
211 s=input.substr(pos, new_pos-pos);
219 Utils::split_string(const string& input, const string& delimiter) {
222 vector<string> result;
226 while (pos<input.size()) {
227 new_pos=input.find(delimiter, pos);
229 new_pos=input.size();
230 s=input.substr(pos, new_pos-pos);
238 Utils::print_debug(const string& s) {
244 Utils::print_debug(const wstring& s) {
246 cerr<<UtfConverter::toUtf8(s);
250 Utils::print_debug(const double& d){
255 Utils::print_debug(const int& i) {
261 // Utils::string2wstring(const string& s) {
263 // wchar_t auxws[s.length()+1];
264 // mbstowcs(auxws, optarg, strlen(optarg)+1);
265 // auxws[strlen(optarg)]=L'\0';