annotate GEMBASSY-1.0.3/include/gfile.c @ 0:8300eb051bea draft

Initial upload
author ktnyt
date Fri, 26 Jun 2015 05:19:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
1 /******************************************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
2 ** @source GEMBASSY file routines
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
3 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
4 ** @version 1.0
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
5 ** @modified December 27 2012 Hidetoshi Itaya Created this file
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
6 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
7 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
8 ** This library is free software; you can redistribute it and/or
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
9 ** modify it under the terms of the GNU Library General Public
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
10 ** License as published by the Free Software Foundation; either
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
11 ** version 2 of the License, or (at your option) any later version.
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
12 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
13 ** This library is distributed in the hope that it will be useful,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
16 ** Library General Public License for more details.
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
17 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
18 ** You should have received a copy of the GNU Library General Public
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
19 ** License along with this library; if not, write to the
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
20 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
21 ** Boston, MA 02111-1307, USA.
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
22 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
23
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
24 #include "gfile.h"
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
25
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
26
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
27
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
28
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
29 /* @func gValID ***************************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
30 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
31 ** Checks if an input string is a valid ID
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
32 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
33 ** @param id [r] [AjPStr] ID to check
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
34 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
35 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
36 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
37
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
38 ajint gValID(AjPStr id){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
39 AjPFilebuff buff = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
40 AjPStr url = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
41 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
42 AjPRegexp pval = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
43
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
44 url = ajStrNewC("http://web.sfc.keio.ac.jp/~t11080hi/valID/valID.cgi?id=");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
45 url = ajStrNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
46 ajFmtPrintS(&url, "http://rest.g-language.org/%S", id);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
47
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
48 //ajStrAppendS(&url, id);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
49
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
50 if(!gFilebuffURLS(url, &buff)) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
51 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
52 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
53
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
54 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
55
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
56 ajBuffreadLine(buff, &line);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
57
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
58 pval = ajRegCompC("^0");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
59
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
60 if(ajRegExec(pval, line))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
61 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
62
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
63 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
64 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
65
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
66
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
67
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
68
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
69 /* @func gStrAppendURLS *******************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
70 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
71 ** Downloads file from a specified URL and writes to given output file
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
72 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
73 ** @param [r] url [AjPStr] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
74 ** @param [r] string [AjPStr] String to write into
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
75 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
76 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
77 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
78
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
79 AjBool gStrAppendURLS(AjPStr url, AjPStr* string){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
80 AjPFilebuff buff = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
81 AjPStr file = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
82 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
83
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
84 if(!*string)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
85 *string = ajStrNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
86
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
87 if(!gFilebuffURLS(url, &buff))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
88 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
89
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
90 while(ajBuffreadLine(buff, &line)){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
91 ajStrAppendS(string, line);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
92 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
93
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
94 ajFilebuffDel(&buff);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
95
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
96 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
97 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
98
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
99
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
100
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
101
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
102 /* @func gStrAppendURLC *******************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
103 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
104 ** Downloads file from a specified URL and writes to given output file
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
105 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
106 ** @param [r] url [char*] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
107 ** @param [r] string [AjPStr] String to write into
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
108 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
109 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
110 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
111
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
112 AjBool gStrAppendURLC(char* url, AjPStr* string){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
113 if(!gStrAppendURLS(ajStrNewC(url), string))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
114 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
115 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
116 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
117
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
118 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
119 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
120
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
121
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
122
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
123
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
124 /* @func gFileOutURLS *********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
125 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
126 ** Downloads file from a specified URL and writes to given output file
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
127 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
128 ** @param [r] url [AjPStr] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
129 ** @param [r] outf [AjPFile] File object to write into
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
130 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
131 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
132 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
133
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
134 AjBool gFileOutURLS(AjPStr url, AjPFile* outf){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
135 AjPFilebuff buff = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
136 AjPStr file = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
137 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
138
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
139 if(!gFilebuffURLS(url, &buff))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
140 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
141
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
142 while(ajBuffreadLine(buff, &line)){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
143 ajWriteline(*outf, line);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
144 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
145
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
146 ajFilebuffDel(&buff);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
147
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
148 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
149 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
150
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
151
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
152
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
153
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
154 /* @func gFileOutURLC *********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
155 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
156 ** Downloads file from a specified URL and writes to given output file
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
157 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
158 ** @param [r] url [char*] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
159 ** @param [r] outf [AjPFile] File object to write into
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
160 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
161 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
162 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
163
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
164 AjBool gFileOutURLC(char* url, AjPFile* outf){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
165 if(!gFileOutURLS(ajStrNewC(url), outf))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
166 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
167 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
168 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
169
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
170 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
171 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
172
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
173
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
174
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
175
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
176 /* @func gFilebuffURLS ********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
177 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
178 ** Downloads file from a specified URL and inputs in file buffer
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
179 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
180 ** @param [r] url [AjPStr] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
181 ** @param [r] buff [AjPFilebuff] File buffer to set
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
182 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
183 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
184 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
185
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
186 AjBool gFilebuffURLS(AjPStr url, AjPFilebuff* buff){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
187 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
188 AjPStr host = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
189 AjPStr path = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
190 ajint port = 80;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
191
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
192 ajHttpUrlDeconstruct(url, &port, &host, &path);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
193
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
194 *buff = ajHttpRead(NULL, NULL, NULL, host, port, path);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
195
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
196 if(!*buff)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
197 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
198
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
199 ajFilebuffHtmlNoheader(*buff);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
200
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
201 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
202 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
203
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
204
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
205
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
206
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
207 /* @func gFilebuffURLC ********************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
208 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
209 ** Downloads file from a specified URL and inputs in file buffer
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
210 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
211 ** @param [r] url [char*] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
212 ** @param [r] buff [AjPFilebuff] File buffer to set
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
213 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
214 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
215 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
216
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
217 AjBool gFilebuffURLC(char* url, AjPFilebuff* buff){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
218 gFilebuffURLS(ajStrNewC(url), buff);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
219
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
220 if(!*buff)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
221 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
222
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
223 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
224 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
225
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
226
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
227
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
228
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
229 /* @func gAssignUniqueName ****************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
230 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
231 ** Creates a unique filename
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
232 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
233 ** @return [AjPStr] the unique filename
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
234 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
235 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
236
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
237 void gAssignUniqueName(AjPStr *string) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
238 static char ext[2] = "A";
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
239
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
240 ajFmtPrintS(string, "%08d%s", getpid(), ext);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
241
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
242 if( ++ext[0] > 'Z' ) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
243 ext[0] = 'A';
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
244 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
245 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
246
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
247
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
248
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
249
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
250 /* @func gCreateUniqueName ****************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
251 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
252 ** Returns a unique filename
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
253 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
254 ** @return [AjPStr] the unique filename
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
255 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
256 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
257
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
258 AjPStr gCreateUniqueName() {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
259 AjPStr string;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
260
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
261 gAssignUniqueName(&string);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
262
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
263 return string;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
264 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
265
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
266
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
267
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
268
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
269 /* @func gFormatGenbank *******************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
270 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
271 ** Creates a genbank format string with sequence and features
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
272 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
273 ** @param [r] seq [AjPSeq] Sequence object to write
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
274 ** @param [r] inseq [AjPStr] String to write to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
275 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
276 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
277 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
278
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
279 AjBool gFormatGenbank(AjPSeq seq, AjPStr *inseq){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
280 AjPSeqout seqout = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
281 AjPFeattabOut featout = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
282 AjPFeattable feat = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
283 AjPStr seqline = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
284 AjPStr featline = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
285 AjPFile seqfile = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
286 AjPFile featfile = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
287 AjPStr filename = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
288 AjBool hasfeats = ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
289
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
290 gAssignUniqueName(&filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
291 feat = ajSeqGetFeatCopy(seq);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
292
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
293 if(!feat) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
294 hasfeats = ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
295 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
296
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
297 seqout = ajSeqoutNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
298
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
299 if(!ajSeqoutOpenFilename(seqout,filename))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
300 embExitBad();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
301
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
302 ajSeqoutSetFormatS(seqout,ajStrNewC("genbank"));
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
303 ajSeqoutWriteSeq(seqout,seq);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
304 ajSeqoutClose(seqout);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
305 ajSeqoutDel(&seqout);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
306
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
307 seqfile = ajFileNewInNameS(filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
308 ajSysFileUnlinkS(filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
309
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
310 if(hasfeats) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
311 featout = ajFeattabOutNew();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
312
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
313 if(!ajFeattabOutOpen(featout,filename))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
314 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
315
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
316 ajFeattableWriteGenbank(featout,feat);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
317
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
318 ajFeattableDel(&feat);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
319 //ajFeattabOutDel(&featout);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
320 ajFileClose(&(featout->Handle));
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
321
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
322 featfile = ajFileNewInNameS(filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
323 ajSysFileUnlinkS(filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
324 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
325
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
326 while(ajReadline(seqfile,&seqline)){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
327 if(hasfeats && ajStrMatchC(seqline,"ORIGIN\n")){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
328 while(ajReadline(featfile,&featline)){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
329 ajStrAppendS(inseq, featline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
330 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
331 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
332 ajStrAppendS(inseq, seqline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
333 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
334
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
335 ajStrDel(&seqline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
336 ajStrDel(&featline);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
337 ajStrDel(&filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
338 ajFileClose(&seqfile);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
339 ajFileClose(&featfile);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
340
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
341 return hasfeats;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
342 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
343
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
344
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
345
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
346
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
347 /* @func gGetFileContent ******************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
348 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
349 ** Reads file content and sets it to string pointer
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
350 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
351 ** @param [r] content [AjPSeq] String to write to
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
352 ** @param [r] filename [AjPSeq] Filename to open
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
353 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
354 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
355 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
356
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
357 AjBool gGetFileContent(AjPStr* content, AjPStr filename){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
358 AjPFile file = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
359 AjPStr line = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
360
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
361 if((file = ajFileNewInNameS(filename)) == NULL)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
362 return ajFalse;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
363
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
364 while(ajReadline(file, &line))
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
365 ajStrAppendS(content, line);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
366
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
367 if(file)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
368 ajFileClose(&file);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
369
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
370 ajSysFileUnlinkS(filename);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
371
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
372 return ajTrue;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
373 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
374
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
375
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
376
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
377
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
378 /* @func gtaiFileOutURLS ******************************************************
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
379 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
380 ** Downloads file from a specified URL and inputs in file buffer
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
381 **
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
382 ** @param [r] url [AjPStr] URL to download file from
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
383 ** @param [r] buff [AjPFilebuff] File buffer to set
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
384 ** @return [AjBool]
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
385 ** @@
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
386 ******************************************************************************/
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
387
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
388 AjBool gtaiFileOutURLS(AjPStr url, AjPFile* outf, AjBool tai){
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
389 if(tai)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
390 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
391 CURL *curl;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
392 CURLcode curl_res;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
393
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
394 Memory *mem = malloc(sizeof(Memory*));
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
395
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
396 mem->size = 0;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
397 mem->memory = NULL;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
398
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
399 curl_global_init(CURL_GLOBAL_ALL);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
400
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
401 curl = curl_easy_init();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
402
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
403 if(curl)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
404 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
405 curl_easy_setopt(curl, CURLOPT_URL, ajCharNewS(url));
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
406 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
407 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
408 curl_easy_setopt(curl, CURLOPT_WRITEDATA, mem);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
409 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
410
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
411 curl_res = curl_easy_perform(curl);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
412
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
413 if(CURLE_OK == curl_res)
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
414 {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
415 char* redir;
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
416 curl_res = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &redir);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
417
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
418 if((CURLE_OK == curl_res) && redir) {
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
419 ajStrAssignC(&url, redir);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
420 ajStrExchangeCC(&url, "cai.csv", "tai.csv");
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
421 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
422 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
423
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
424 free(mem);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
425 curl_easy_cleanup(curl);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
426 curl_global_cleanup();
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
427 }
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
428
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
429 return gFileOutURLS(url, outf);
8300eb051bea Initial upload
ktnyt
parents:
diff changeset
430 }