~ubuntu-branches/ubuntu/oneiric/gpp4/oneiric

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
C
C     keyparse.f: high-level interface to CCP4 parser functions
C     Copyright (C) 1995  Dave Love, Kevin Cowtan
C
C     This library is free software: you can redistribute it and/or
C     modify it under the terms of the GNU Lesser General Public License
C     version 3, modified in accordance with the provisions of the 
C     license to address the requirements of UK law.
C 
C     You should have received a copy of the modified GNU Lesser General 
C     Public License along with this library.  If not, copies may be 
C     downloaded from http://www.ccp4.ac.uk/ccp4license.php
C 
C     This program is distributed in the hope that it will be useful,
C     but WITHOUT ANY WARRANTY; without even the implied warranty of
C     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
C     GNU Lesser General Public License for more details.
C
C     A simplified interface to the %$!$#@$ (so-called) parser:
C
C     The idea is to call a routine (MEMOPARSE) to read a keyworded line
C     into a hidden internal buffer and then have a simple chain of
C     calls to a set of routines which check the appropriate keyword,
C     look for any extra arguments associated with it, and set values in
C     their arguments as appropriate.  No if ... then ... else (or, much
C     worse, assigned goto) is necessary, nor maintaining the parser
C     arrays -- the relevant checking is done internally.  At the end of
C     the checks, call PARSEDIAGNOSE to print any appropriate messages
C     and loop back to MEMOPARSE if PARSEDIAGNOSE's argument is true
C     (otherwise continue and process the data read in).  You don't need
C     to check for `END' or end-of-file.  Geddit?
C
C     Escape hatch: use PARSEKEYARG to get all the tokens after a random
C     keyword and call PARSE (or whatever) to deal with them as
C     necessary.  This is usually deprecated, however -- go for
C     simply-structured input.
C
C     10 call memoparse (.true.)  ! setup and echo i/p
C        call parseint ('IVAL', ival)  ! ival.eq.3 after `IVAL 3'
C        call parsecell (cell)   ! assign cell from `CELL 20 30 40' etc.
C        call parsekeyarg ('FRED', rest)  ! the full horror in REST
C        call parse (rest, ....)
C         [firkle with the `parse'd arrays]
C         ...
C        call parsediagnose (cont) ! check
C        if (cont) goto 10
C   c     now do something useful...
C
C     Fixme: the set of routines below might need extending...
C     Fixme: consider whether more obscure names worthwhile to avoid
C     possible clashes.
C
C     Dave Love $Date: 2008-06-09 10:41:02 $
C     Additions by Kevin Cowtan
C     
      subroutine memoparse (echo)
C
C     Call PARSER and stash the returned values away for later testing
C     when the other entrypoints are called. (OK, so it's not really
C     memoisation...).
C
C     ECHO is set to echo the parser i/p.

      implicit none
C     Args
      character*(*) key, subkey, spgnam, pgname, rest
      character*30 prglab(*)
      character*1 chnam
      logical echo, flag, cont
      integer ival, nsym, numsgp, nsymp, n, ivals (n), nth, mtznum,
     +  nprglab, toks, inat0, inat1, ires0, ires1, imode, ifail
      real rval, cell (6), rsym (4,4,*), resmin, resmax, smin, smax,
     +  rvals(n)

C     Stores for parser stuff
      integer maxtoks, maxline
C     Let's not mess around...
      parameter (maxtoks = 500, maxline=2000)
      integer ibeg(maxtoks), iend(maxtoks), ityp(maxtoks), idec(maxtoks)
      real fvalue (maxtoks)
      character*4 cvalue (maxtoks), memokey, memosubkey
      character line*(maxline)
      integer ntok

C     locals 
      logical someerr, eof, argerr, success(maxtoks)
      integer i, k

      save
      data someerr, eof /2*.false./
      
      ntok=maxtoks
      argerr = .false.
      line = ' '
C     in case of immediate EOF:
      success(1) = .true.
      call parser(memokey, line, ibeg, iend, ityp, fvalue, cvalue, idec,
     +     ntok, eof, echo)
C     END == EOF always
      if (memokey.eq.'END') eof = .true.
C     not sure if necessary:
      if (eof) memokey = ' '
      do i=1,ntok
       success(i) = .false.
      enddo
      return
      
      entry parsekey (key, flag)
C     bare KEY -- set FLAG if found
      if (memokey.eq.key) then
C       matched key
        if (ntok.eq.1) then
          success(1) = .true.
          flag = .true.
        else
          argerr = .true.
          call lerror (1, 0, 'No argument expected')
        end if
      end if
      return

      entry parsekeyarg (key, rest)
C     KEY + rest of line -- rest of line returned in REST
      if (memokey.eq.key) then
C       matched key
        if (ntok.gt.1) then
          do i=1,ntok
           success(i) = .true.
          enddo
          rest = line (ibeg(2):iend(ntok))
        else
          rest = ' '
          success(1) = .true.
C          argerr = .true.
C          call lerror (1, 0, 'Argument expected')
        end if
      end if
      return

      entry parseint (key, ival)
C     KEY + integer -- returned in IVAL
      if (memokey.eq.key) then
C       matched key
        if (ntok.eq.2 .and. ityp (2).eq.2) then
          ival = nint (fvalue(2))
          do i=1,2
           success(i) = .true.
          enddo
        else 
          argerr = .true.
          call lerror (1, 0, 'Integer argument expected')
        end if
      end if
      return
      
      entry parsereal (key, rval)
C     KEY + real -- returned in RVAL
      if (memokey.eq.key) then
C       matched key
        if (ntok.eq.2 .and. ityp (2).eq.2) then
          rval = fvalue(2)
          do i=1,2
           success(i) = .true.
          enddo
        else 
          argerr = .true.
          call lerror (1, 0, 'Real argument expected')
        end if
      end if
      return
      
      entry parsenargs(key, toks)
      toks = 0
      if (memokey .eq. key) toks = ntok
      return

      entry parsenints (key, n, ivals)
C     KEY + upto N integers -- N reset to number found, returned in IVALS
      if (memokey.eq.key) then
       success(1)=.true.
        if (ntok.ge.2 .and. ntok.le. n+1) then
          do i = 1, min (n, ntok-1)
            if (ityp (i+1).ne.2) return
            ivals (i) = nint (fvalue (i+1))
            n = i
            success(i+1) = .true.
          end do
        else
          argerr = .true.
          n = ntok
          call lerror (1, 0, 'Incorrect number of integer arguments')
        end if
      end if
      return
      
      entry parsenreals (key, n, rvals)
C     KEY + upto N reals -- N reset to number found, returned in RVALS
      if (memokey.eq.key) then
       success(1)=.true.
        if (ntok.ge.2 .and. ntok.le. n+1) then
          do i = 1, min (n, ntok-1)
            if (ityp (i+1).ne.2) return
            rvals (i) = fvalue (i+1)
            n = i
            success(i+1) = .true.
          end do
        else
          argerr = .true.
          n = ntok
          call lerror (1, 0, 'Incorrect number of real arguments')
        end if
      end if
      return
      
      entry parsesubkey (key, subkey, flag)
C     KEY + subkeyword SUB -- set FLAG if found
      if (memokey.eq.key) then
C       matched key
       success(1) = .true.
       if (subkey.ne.' ') then
        do k=2,ntok
         memosubkey=cvalue(k)
         call ccpupc(memosubkey)
         if (memosubkey.eq.subkey) then
          flag=.true.
          success(k)=.true.
         endif
        enddo
       else
        flag=.true.
       end if
      end if
      return
      
      entry parsesubkeyarg (key, subkey, nth, rest)
C     KEY + subkey + rest of line -- rest of line returned in REST
      if (memokey.eq.key) then
C       matched key
        success(1) = .true.
        k = 1
        if (subkey .ne. ' ') then
          k = 9999
          do i=2,ntok
            memosubkey=cvalue(i)
            call ccpupc(memosubkey)
            if (memosubkey.eq.subkey) then
             k = i
             goto 90
            endif
          enddo
        endif
   90   if (k.le.ntok) then
          if (ntok.ge.k+nth) then
            rest = line (ibeg(k+nth):iend(ntok))
            do i=2,ntok
              success(i) = .true.
            enddo
          else
            argerr = .true.
            call lerror (1, 0, 'Argument expected after sub-keyword')
          end if
        end if
      end if
      return

      entry parsesubint (key, subkey, nth, flag, ival)
C     KEY + n'th integer after subkey -- returned in IVAL
C     ERROR only if flag=true
      if (memokey.eq.key) then
C  ... matched key
       success(1) = .true.
       k=1
       if (subkey.ne.' ') then
        k=9999
        do i=2,ntok
         memosubkey=cvalue(i)
         call ccpupc(memosubkey)
         if (memosubkey.eq.subkey) then
          k=i
          goto 100
         endif
        enddo
       endif
  100  if (k.le.ntok) then
C  .... matched subkey (if set)
        success(k) = .true.
        if (ntok.ge.nth+k .and. ityp(nth+k).eq.2) then
         ival = nint (fvalue(nth+k))
         success(nth+k) = .true.
        else if (flag) then
         argerr = .true.
         call lerror (1, 0, 'Integer sub-argument expected')
        endif
       endif
      endif
      return
      
      entry parsesubreal (key, subkey, nth, flag, rval)
C     KEY + n'th real after subkey -- returned in RVAL
C     ERROR only if flag=true
      if (memokey.eq.key) then
C  ... matched key
       success(1) = .true.
       k=1
       if (subkey.ne.' ') then
        k=9999
        do i=2,ntok
         memosubkey=cvalue(i)
         call ccpupc(memosubkey)
         if (memosubkey.eq.subkey) then
          k=i
          goto 110
         endif
        end do
       endif
  110  if (k.le.ntok) then
C  .... matched subkey (if set)
        success(k) = .true.
        if (ntok.ge.nth+k .and. ityp(nth+k).eq.2) then
         rval = fvalue(nth+k)
         success(nth+k) = .true.
        else if (flag) then
         argerr = .true.
         call lerror (1, 0, 'Real sub-argument expected')
        endif
       endif
      endif
      return
      
      entry parsesubchar (key, subkey, nth, flag, rest)
C     KEY + n'th string after subkey -- returned in REST
      if (memokey.eq.key) then
C  ... matched key
       success(1) = .true.
       k=1
       if (subkey.ne.' ') then
        k=9999
        do i=2,ntok
         memosubkey=cvalue(i)
         call ccpupc(memosubkey)
         if (memosubkey.eq.subkey) then
          k=i
          goto 120
        endif
        end do
       endif
  120  if (k.le.ntok) then
C  .... matched subkey (if set)
        success(k) = .true.
        if (ntok.ge.nth+k .and. (ityp(nth+k).ne.0 .or. .not.flag) ) then
         rest = line(ibeg(nth+k):iend(nth+k))
         success(nth+k) = .true.
        else if (flag) then
         argerr = .true.
         call lerror (1, 0, 'Character sub-argument expected')
        endif
       endif
      endif
      return
      
      entry parsecell (cell)
C     CELL -- returned in CELL
      if (memokey.eq.'CELL') then
        call rdcell (2, ityp, fvalue, ntok, cell)
        do i=1,ntok
         success(i) = .true.
        enddo
      end if
      return
      
      entry parsesymm (spgnam, numsgp, pgname, nsym, nsymp, rsym)
C     SYMMetry -- usual values returned
      if (memokey.eq.'SYMM') then
        nsym = 0
        call rdsymm(2, line, ibeg, iend, ityp, fvalue, ntok, spgnam,
     +       numsgp, pgname, nsym, nsymp, rsym)
        do i=1,ntok
         success(i) = .true.
        enddo
      end if
      return

      entry parseatomselect(key, inat0, inat1, ires0, ires1, chnam,
     +                      imode)
C     KEYword followed by atom/residue selection syntax
C     key atom <ai> [[to] <aj>] |
C         residue [all|ons|ca] [chain <chn>] <ri> [[to] <rj>]
C     Returns values of <ai>,<aj>,imode...
      if (memokey.eq.key) then
C       matched key
        ifail = -1
        call rdatomselect(2, inat0, inat1, ires0, ires1, chnam, imode,
     +                     ntok, line, ibeg, iend, ityp, idec, fvalue,
     +                     ifail)
        do i=1,ntok
          if (i.ne.ifail) success(i)=.true.
        enddo
      endif
      return

      entry parsereso (resmin, resmax, smin, smax)
C     RESOlution -- usual values returned
      if (memokey.eq.'RESO') then
        call rdreso (2, ityp, fvalue, ntok, resmin, resmax, smin, smax)
        do i=1,ntok
         success(i) = .true.
        enddo
      end if
      return
      
      entry parselabin(mtznum,prglab,nprglab)
C     LABIn -- requires mtz file number, program labels, and number of.
      if (memokey.eq.'LABI') then
        call lkyin(mtznum,prglab,nprglab,ntok,line,ibeg,iend)
        do i=1,ntok
         success(i) = .true.
        enddo
      end if
      return
      
      entry parselabout(mtznum,prglab,nprglab)
C     LABOut -- requires mtz file number, program labels, and number of.
      if (memokey.eq.'LABO') then
        call lkyout(mtznum,prglab,nprglab,ntok,line,ibeg,iend)
        do i=1,ntok
         success(i) = .true.
        enddo
      end if
      return

      entry parsefail()
      do i=1,ntok
       success(i) = .false.
      enddo
      return

      entry parseend(key)
C     KEY -- is treated as input terminator as same as END
      if (memokey .eq. key) eof = .true.
      return
      
      entry parsediagnose (cont)
C     Call at end of tests for possible 'Invalid keyword' diagnostic or
C     abort if at EOF and had an error.  Continue processing (no EOF)
C     if (cont).
      if (.not.argerr .and. .not.eof) then
       if (.not.success(1)) then
        call lerror (1, 0, 'Invalid keyword')
        someerr = .true.
       endif
       do i=2,ntok
        if (.not.success(i)) then
         write (line,950)i
 950     format ('Invalid sub-keyword in position ',i2)
         call lerror (1, 0, line)
         someerr = .true.
        endif
       enddo
      endif
      if (argerr) then
        someerr = .true.
      end if
      argerr = .false.
      if (eof) then
        cont = .false.
        if(someerr) call ccperr (1, 'Input error (see above)')
      else
        cont = .true.
      end if
      do i=1,ntok
       success(i) = .false.
      enddo
      return
C
      end