File: ccp4_parser_f.c

package info (click to toggle)
libccp4 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,100 kB
  • sloc: ansic: 19,540; fortran: 18,766; sh: 11,561; makefile: 73
file content (590 lines) | stat: -rw-r--r-- 18,787 bytes parent folder | download | duplicates (3)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
     ccp4_parser_f.c: Fortran API to ccp4_parser.c
     Copyright (C) 2001  CCLRC, Peter Briggs

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program 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.
*/

/** @page cparser_f_page Fortran API to CParser
 *
 *  @section cparser_f_file_list File list

<ul>
<li>ccp4_parser_f.c
</ul>
 *
 *  @section cparser_f_overview Overview

This library consists of a set of wrappers to the CParser library
giving the same API as the original parser.f

 */

/** @file ccp4_parser_f.c
 *
 *  @brief Fortran API to ccp4_parser.c.
 *
 *  @author Peter Briggs
 */

/*   ccp4_parser_f.c
     Peter Briggs CCP4 August 2001

     This file contains Fortran API functions which provide the following
     functionality originally found in the parser.f file:

     PARSER  read and interpret data from the input stream
     PARSE   free format read routine
     PARSEDL change delimiters

     plus internal utility functions which should not be accessed
     directly from an application.
*/

/*#define FORTRAN_CALL_DEBUG 1*/

#if defined (FORTRAN_CALL_DEBUG)
#  define PARSER_DEBUG(x) x
#else
#  define PARSER_DEBUG(x)
#endif

/* CCP4 header file for machine dependency */
#include "ccp4_sysdep.h"

/* CCP4 header file for Fortran interfaces */
#include "ccp4_fortran.h"

/* C parser header file */
#include "ccp4_parser.h"
#include "ccp4_general.h"

/* rcsid[] = "$Id$" */

/*------------------------------------------------------------------*/

/* Utility functions */     

/*------------------------------------------------------------------*/

/* fparse_isblank

   Given a string and its length, returns 1 if the only characters are
   whitespace or newlines, and 0 if there are other characters.
*/
int fparse_isblank(const char *line, const int line_len)
{
  int i;
  if (line_len > 0) {
    for (i=0; i<line_len; i++) {
      if (!charmatch(line[i]," \t\n")) return 0;
    }
    return 1;
  }
  return 0;
}     
/*------------------------------------------------------------------*/

/* fparse_strncpypad

   Copy lfstr characters from C-style string cstr into Fortran-style string
   fstr. lfstr is the maximum length of the Fortran-style string.

   If cstr contains fewer characters than lfstr then fstr is padded with
   spaces. Note that fstr will not contain an end-of-string null character,
   so printf using %s may also show trailing garbage.
 */
int fparse_strncpypad(char *fstr, const char *cstr, const int lfstr)
{
  int i;

  if (!cstr || !fstr || !lfstr) return 0;

  strncpy(fstr,cstr,lfstr);
  if (strlen(cstr) < lfstr) {
    for (i=strlen(cstr); i<lfstr; i++) fstr[i] = ' ';
  }
  return 1;
}     

/*------------------------------------------------------------------*/

/* fparse_populate_arrays

   Given a pointer to a CCP4PARSERARRAY, will populate the equivalent
   arrays supplied by the Fortran application.

   Should only be called from PARSER or PARSE.
 */
int fparse_populate_arrays(CCP4PARSERARRAY *parser, int *ibeg, int *iend,
			   int *ityp, float *fvalue, fpstr cvalue, int cvalue_len,
			   int *idec)
{
  int i,iindex;
  char *pstr;
  CCP4PARSERTOKEN *token = NULL;

  /* Check we have something to work with */
  if (!parser) return 0;

  /* Set a convenient pointer to the array of tokens */
  token = parser->token;

  /* Loop over all tokens */
  for (i=0; i<parser->ntokens; i++) {
    PARSER_DEBUG(printf("fparse_populate_arrays: Token %d\n",i);)

    /* Positions for first and last characters
       Add one on because Fortran numbers array elements from one */
    ibeg[i] = token[i].ibeg+1;
    iend[i] = token[i].iend+1;
    PARSER_DEBUG(printf("fparse_populate_arrays: ibeg = %d, iend = %d\n",ibeg[i],iend[i]);)

    /* Token types
       0 = null field
       1 = character string
       2 = number
    */
    if (!token[i].isnull) {
      /* Either string or number */
      
      if (token[i].isstring) {
	/* String token */
	PARSER_DEBUG(printf("fparse_populate_arrays: string token\n");)
	ityp[i] = 1;
	PARSER_DEBUG(printf("fparse_populate_arrays: 4-letter version is \"%s\"\n",token[i].word);)
	if (token[i].strlength > 4) {
	  idec[i] = 4;
	} else {
	  idec[i] = token[i].strlength;
	}
	PARSER_DEBUG(printf(" ityp = %d\n",ityp[i]);)
	
      } else if (token[i].isnumber) {
	/* Numerical token */
	PARSER_DEBUG(printf("fparse_populate_arrays: number token\n");)
	ityp[i] = 2;
	fvalue[i] = (float) token[i].value;
	if (token[i].frcdigits) {
	  /* Real number */
	  idec[i] = (token[i].intdigits+1)*100 + token[i].frcdigits;
	} else {
	  /* Integer number */
	  idec[i] = token[i].intdigits;
	}
	PARSER_DEBUG(printf(" ityp = %d\n",ityp[i]);)
      } else {
	/* Unrecognised token type */
	PARSER_DEBUG(printf("fparse_populate_arrays: unrecognised token type - setting to null");)
	ityp[i] = 0;
      }
      
      /* cvalue is set for both string and numerical tokens
	 It appears that Fortran allocates a block of memory for
	 string arrays which is basically string(1) followed by
	 string(2) etc, and then the length string_len is the
	 number of characters in a single element.
      */
      iindex = i*cvalue_len;
      pstr   = &(cvalue[iindex]);
      PARSER_DEBUG({
	printf("fparse_populate_arrays: token string stored as \"%s\"\n",token[i].word);
	printf("fparse_populate_arrays: initial value of cvalue[%d] is \"%s\"\n",i,pstr);
      })
      /* Store the value and pad if necessary */
      fparse_strncpypad(pstr,token[i].word,cvalue_len);
      PARSER_DEBUG(printf("fparse_populate_arrays: cvalue[%d] is now \"%s\"\n",i,pstr);)
      
    } else {
      /* Null token */
      PARSER_DEBUG(printf("fparse_populate_arrays: null field");)
      ityp[i] = 0;
      PARSER_DEBUG(printf(" ityp = %d\n",ityp[i]);)
    }
    /* All assignments complete for this token */
  }
  return 1;
}     


/*------------------------------------------------------------------*/

/* fparse_delimiters

   Set and get delimiters for use in ccp4_parse(r). This should only
   be called internally (from PARSDL).

   If called with parser set to NULL, the lists of characters in
   new_delimiters and new_nulldelimiters are stored.

   When called with a valid pointer to a parser structure, the stored
   lists of characters form the new delimiter sets via a call to
   ccp4_parse_delimiters.

   NB: if delimiter strings are set with a call to fparse_delimiters
   then they persist in memory unless a subsequent call is made with
   all arguments set to NULL. This represents a potential memory leak.
*/
int fparse_delimiters(CCP4PARSERARRAY *parser, char *new_delimiters,
		      char *new_nulldelimiters)
{
  int ndelim;
  static char *delimiters=NULL, *nulldelimiters=NULL;

  PARSER_DEBUG(puts("fparse_delimiters: starting");)

  if (!parser) {

    PARSER_DEBUG({
      puts("fparse_delimiters: setting local lists");
      if (new_delimiters) printf("fparse_delimiters: new_delimiters = \"%s\"\n",new_delimiters);
      if (new_nulldelimiters) printf("fparse_delimiters: new_nulldelimiters = \"%s\"\n",new_nulldelimiters);
    })
    /* Set the local lists of delimiters
       depending on the input */
    /* Standard delimiters */
    if (!new_delimiters) {
      if (delimiters) free(delimiters);
      delimiters = NULL;
    } else {
      ndelim = strlen(new_delimiters) + 1;
      delimiters = (char *) realloc(delimiters,ndelim*sizeof(char));
      if (delimiters) {
	strncpy(delimiters,new_delimiters,ndelim);
	PARSER_DEBUG(printf("fparse_delimiters: delimiters set to \"%s\"\n",delimiters);)
      } else {
	ccperror(4,"fparse_delimiters: couldn't reallocate delimiters");
      }
    }
    /* Null delimiters */
    if (!new_nulldelimiters) {
      if (nulldelimiters) free(nulldelimiters);
      nulldelimiters = NULL;
    } else {
      ndelim = strlen(new_nulldelimiters) + 1;
      nulldelimiters = (char *) realloc(nulldelimiters,ndelim*sizeof(char));
      if (nulldelimiters) {
	strncpy(nulldelimiters,new_nulldelimiters,ndelim);
	PARSER_DEBUG(printf("fparse_delimiters: nulldelimiters set to \"%s\"\n",nulldelimiters);)
      } else {
	ccperror(4,"fparse_delimiters: couldn't reallocate null delimiters");
      }
    }
  } else {
    /* Set the parser array so that it uses the delimiters
       already stored */

    PARSER_DEBUG({
      puts("fparse_delimiters: setting parser to use stored delimiters");
    })
    if (!ccp4_parse_delimiters(parser,delimiters,nulldelimiters))
        ccperror(4,"fparse_delimiters: couldn't reset delimiters");
    PARSER_DEBUG(printf("fparse_delimiters: now set to \"%s\" and \"%s\"\n",parser->delim, parser->nulldelim);)
    return 0;
  }
  return 1;
}     

/*------------------------------------------------------------------*/

/* PARSER

   This function implements the Fortran equivalent of:

   SUBROUTINE PARSER(KEY,LINE,IBEG,IEND,ITYP,FVALUE,CVALUE,IDEC,NTOK,LEND,PRINT)

   which is the original CCP4 keyword parser.
   The function wraps the ccp4_parser routines to mimic the behaviour of the
   original subroutine.
 */

FORTRAN_SUBR(PARSER,parser,
             (fpstr key, fpstr line, int *ibeg, int *iend, int *ityp, float *fvalue,
              fpstr cvalue, int *idec, int *ntok, ftn_logical *lend, 
              const ftn_logical *print, fpstr_size_t key_len, 
	      fpstr_size_t line_len, fpstr_size_t cvalue_len),
             (fpstr key, fpstr line, int *ibeg, int *iend, int *ityp, float *fvalue,
              fpstr cvalue, int *idec, int *ntok, ftn_logical *lend, 
              const ftn_logical *print),
             (fpstr key, fpstr_size_t key_len, 
	      fpstr line, fpstr_size_t line_len, int *ibeg, int *iend,
              int *ityp, float *fvalue, 
	      fpstr cvalue, fpstr_size_t cvalue_len, int *idec,
              int *ntok, ftn_logical *lend, const ftn_logical *print))
{
  static FILE *fparse_fp = NULL;
  int  max_line_len,lline,cprint = 0;
  char *cline;
  CCP4PARSERARRAY *parser = NULL;

  PARSER_DEBUG(puts("PARSER: starting");)

  /* On input ntok is the maximum number of fields to be parsed
     If ntok < 20 then set to 20 */
  if (*ntok < 20) *ntok = 20;
  /* also reset if ntok is silly - this is a trap for uninitialised ntok */
  if (*ntok > 10000) *ntok = 10000;

  PARSER_DEBUG({
    printf("PARSER: set maximum number of tokens to %d\n",*ntok);
    printf("PARSER: line is initially \"%s\"\n",line);
  })

  /* copy to C string, but also allocate enough memory for return value */
  cline  = (char *) ccp4_utils_malloc((FTN_LEN(line)+1)*sizeof(char));
  lline = ccp4_utils_flength(FTN_STR(line),FTN_LEN(line));
  strncpy(cline,FTN_STR(line),lline);
  cline[lline] = '\0';
  /* Get the maximum line length
     Use the FTN_LEN macro for this since we can't rely on
     getting the line_len argument on all systems */
  max_line_len = FTN_LEN(line);
  PARSER_DEBUG(printf("PARSER: line length is %d\n",max_line_len);)

  /* Set up a parser array to handle ntok tokens */
  parser = (CCP4PARSERARRAY *) ccp4_parse_start(*ntok);
  if (!parser) {
    PARSER_DEBUG(printf("PARSER: failed to allocate memory");)
    *lend = FORTRAN_LOGICAL_TRUE;
    return;
  }
  PARSER_DEBUG(puts("PARSER: parser array initialised");)

  /* Set up the delimiters */
    PARSER_DEBUG(puts("PARSER: fetching delimiters");)
  fparse_delimiters(parser,NULL,NULL);
  PARSER_DEBUG(puts("PARSER: delimiters set");)

  /* Set up the maximum and minimum exponents used to avoid
     under/overflow
     Since Fortran REALs are equivalent to C floats, make sure
     these limits are appropriate for floats */
  ccp4_parse_maxmin(parser,FLT_MAX_10_EXP,FLT_MIN_10_EXP);

  /* Silent or verbose output? */
    PARSER_DEBUG(printf("PARSER: print set to %d\n",*print);)
  if (*print != FORTRAN_LOGICAL_FALSE) cprint = 1;

  PARSER_DEBUG({ 
    if (cprint) {
      puts("PARSER: verbose output ON");
    } else {
      puts("PARSER: verbose output OFF");
    }
  })

  /* Was ccp4_parser reading from an external file last time? */
  if (fparse_fp) {
    PARSER_DEBUG(printf("PARSER: we were reading from an external file\n");)
    parser->fp = fparse_fp;
  }

  /* Call ccp4_parser to do the work */
  PARSER_DEBUG({
    printf("PARSER: line sent as \"%s\"\n",cline);
  })
  *ntok = ccp4_parser(cline,max_line_len,parser,cprint);
  PARSER_DEBUG({
    printf("PARSER: returned %d tokens from ccp4_parser\n",*ntok);
    printf("PARSER: line returned as \"%s\"\n",cline);
  })

  /* Check for end-of-file */
  if (!*ntok) {
    PARSER_DEBUG(puts("PARSER: reached end of file");)
    *lend = FORTRAN_LOGICAL_TRUE;
  } else {
    PARSER_DEBUG(puts("PARSER: end of file not reached yet");)
    *lend = FORTRAN_LOGICAL_FALSE;

    /* Keyword
       NB You need to pad the string with spaces before sending back to
       Fortran
    */
    PARSER_DEBUG(printf("PARSER: ccp4_parser keyword is \"%s\"\n",parser->keyword);)
    ccp4_CtoFString(FTN_STR(key),FTN_LEN(key),parser->keyword);
    PARSER_DEBUG(printf("PARSER: PARSER keyword is \"%s\"\n",key);)

    PARSER_DEBUG(printf("PARSER: ccp4_parser line is \"%s\"\n",cline);)
    ccp4_CtoFString(FTN_STR(line),FTN_LEN(line),cline);
    PARSER_DEBUG(printf("PARSER: PARSER line is \"%s\"\n",line);)

    /* Populate the Fortranic arrays */
    PARSER_DEBUG(printf("PARSER: about to populate the arrays\n");)
    fparse_populate_arrays(parser,ibeg,iend,ityp,fvalue,cvalue,FTN_LEN(cvalue),idec);
    PARSER_DEBUG(printf("PARSER: arrays populated\n");)
  }
  PARSER_DEBUG(puts("PARSER: finished assignments");)

  /* Check if ccp4_parser was reading from an external file
     which is still open */
  if (parser->fp) {
    PARSER_DEBUG(printf("PARSER: reading from external file\n");)
    fparse_fp = parser->fp;
  } else {
    fparse_fp = NULL;
  }

  /* Free the parser array */
  ccp4_parse_end(parser);
  PARSER_DEBUG(puts("PARSER: freed the memory, returning");)

  free(cline);

  return;
}     

/*------------------------------------------------------------------*/

/* PARSE

   This function implements the Fortran equivalent of:

   SUBROUTINE PARSE(LINE,IBEG,IEND,ITYP,FVALUE,CVALUE,IDEC,N)
*/
FORTRAN_SUBR(PARSE,parse,
	     (fpstr line, int *ibeg, int *iend, int *ityp, float *fvalue,
	      fpstr cvalue, int *idec, int *n, int line_len, int cvalue_len),
	     (fpstr line, int *ibeg, int *iend, int *ityp, float *fvalue,
	      fpstr cvalue, int *idec, int *n),
	     (fpstr line, int line_len, int *ibeg, int *iend,
	      int *ityp, float *fvalue, fpstr cvalue, int cvalue_len, int *idec,
	      int *n))
{
  char *temp_line;
  static int maxtok = 0;
  CCP4PARSERARRAY *parser = NULL;

  temp_line = ccp4_FtoCString(FTN_STR(line), FTN_LEN(line));

  PARSER_DEBUG({
    puts("PARSE: starting");
    printf("PARSE: line is initially \"%s\"\n",temp_line);
  })

  /* Set a value for the maximum number of tokens

     When called for the first time n should be less than zero, and
     ABS(n) is the maximum number of tokens.
     When called subsequently, if n > 0 then this is the number of
     tokens already read in.

     To handle this, set maxtok only when a -ve value of n is input.
  */
  if (*n < 0) {
    maxtok = -1*(*n);
  }
  /* If ntok is still zero then this is probably an error so return
     without any action */
  if (maxtok == 0) {
    printf("PARSE: zero number of tokens specified - aborting\n");
    return;
  }

  /* Set up a parser array to handle maxtok tokens */
  parser = (CCP4PARSERARRAY *) ccp4_parse_start(maxtok);
  if (!parser) {
    PARSER_DEBUG(printf("PARSE: failed to allocate memory");)
    return;
  }
  PARSER_DEBUG(puts("PARSE: parser array initialised");)

  /* Set up the delimiters */
  PARSER_DEBUG(puts("PARSER: fetching delimiters");)
  fparse_delimiters(parser,NULL,NULL);
  PARSER_DEBUG(puts("PARSER: delimiters set");)
  
  /* Call ccp4_parse to do the work */
  *n = ccp4_parse(temp_line,parser);

  PARSER_DEBUG(printf("PARSE: returned %d tokens from ccp4_parse\n",*n);)

  /* Populate the Fortranic arrays */
  PARSER_DEBUG(printf("PARSE: about to populate the arrays\n");)
  fparse_populate_arrays(parser,ibeg,iend,ityp,fvalue,cvalue,FTN_LEN(cvalue),idec);
  PARSER_DEBUG(printf("PARSE: arrays populated\n");)
  
  /* Free the parser array */
  ccp4_parse_end(parser);
  PARSER_DEBUG(puts("PARSE: freed the memory, returning");)

  free(temp_line);

  return;
}

/*------------------------------------------------------------------*/

/* PARSDL

   Set delimiters for PARSE, PARSER

   SUBROUTINE PARSDL(NEWDLM,NNEWDL,NSPECD)
*/
FORTRAN_SUBR(PARSDL,parsdl,
	     (fpstr newdlm, int *nnewdl, int *nspecd, fpstr_size_t newdlm_len),
	     (fpstr newdlm, int *nnewdl, int *nspecd),
	     (fpstr newdlm, fpstr_size_t newdlm_len, int *nnewdl, int *nspecd))
{
  int nnulldl;
  char *delim=NULL, *nulldelim=NULL;

  PARSER_DEBUG(puts("PARSDL: starting");)

  if (*nnewdl <= 0) {

    /* Reset the delimiters to the defaults */
    PARSER_DEBUG(puts("PARSDL: resetting to default delimiters");)
    fparse_delimiters(NULL,NULL,NULL);
  } else {
    /* Check the number of new delimiters is no more than the
       number of characters actually supplied */
    if (FTN_LEN(newdlm) < *nnewdl) {
      ccperror(4,"PARSDL: too few delimiter characters supplied");
      return;
    }
    /* Extract the list of delimiter characters */
    delim = (char *) malloc((*nnewdl+1)*sizeof(char));
    if (delim) {
      strncpy(delim,newdlm,*nnewdl);
      delim[*nnewdl] = '\0';
    }
    PARSER_DEBUG(printf("PARSDL: delimiters are \"%s\"\n",delim);)
    /* Extract the list of null delimiter characters (if any) */
    nnulldl = *nnewdl - *nspecd;
    if (nnulldl > 0) {
      nulldelim = (char *) malloc((nnulldl+1)*sizeof(char));
      if (nulldelim) {
	strncpy(nulldelim,&(newdlm[*nspecd]),nnulldl);
	nulldelim[nnulldl] = '\0';
      }
      PARSER_DEBUG(printf("PARSDL: null delimiters are \"%s\"\n",nulldelim);)
    } else {
      nulldelim = (char *) malloc(sizeof(char));
      if (nulldelim) {
	nulldelim[0] = '\0';
      }
    }
    /* Store the delimiters for later reference */
    fparse_delimiters(NULL,delim,nulldelim);
  }
  /* Free locally allocated memory */
  if (delim) free(delim);
  if (nulldelim) free(nulldelim);

  PARSER_DEBUG(puts("PARSDL: finished");)
  return;
}