home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume13
/
okbridge
/
part05
/
ps.c
< prev
Wrap
C/C++ Source or Header
|
1992-01-12
|
4KB
|
152 lines
/* ps.c -- "parse-string" data structure.
!
! Copyright (C) 1990,1991 by Matthew Clegg
!
! This program may be copied and distributed freely. Please do not
! charge money for this program or for any program derived from it.
! If you modify this program, then include a notice stating plainly
! that your program is derived from the okbridge program and is not
! the same as the official okbridge program.
!
! I welcome any suggestions for improvement to okbridge, and
! I would be especially happy to receive improved source code.
! If you have comments or suggestions, or if you would like to
! join the okbridge mailing list, then write to
!
! mclegg@cs.ucsd.edu
!
*/
#ifdef __TURBOC__
#include <alloc.h>
#endif
/* #include <ctype.h> */
#include <stdio.h>
#include "ps.h"
parse_string ps_alloc (n)
int n;
/* Returns a parse_string which can hold up to n bytes of text. */
{
parse_string ps;
ps = (parse_string) malloc (n + PS_OFFSET + 1);
ps[PS_SIZE] = n;
return (ps);
};
/* void ps_reset(parse_string ps); */
/* Clears a parse_string so that new data can be copied into it. */
ps_put (ps, ch)
parse_string ps; char ch;
/* void ps_put (parse_string ps, char ch) */
/* Adds the character ch to the parse_string ps. */
{
if (ps[PS_LENGTH] < ps[PS_SIZE])
ps[PS_OFFSET + ps[PS_LENGTH]++] = ch;
};
ps_copy (ps, s)
parse_string ps; char *s;
/* void ps_copy (parse_string ps, char *s) */
/* Copies the string s to the parse_string ps.
* Resets ps before doing the copy.
*/
{
ps[PS_POS] = ps[PS_LENGTH] = 0;
while ((*s != '\0') && (ps[PS_LENGTH] < ps[PS_SIZE]))
ps[PS_OFFSET + ps[PS_LENGTH]++] = *(s++);
};
ps_read (ps, fp)
parse_string ps; FILE *fp;
/* void ps_read (parse_string ps, FILE *fp) */
/* Copies the next text line from the file fp to the parse_string ps.
* Resets ps before doing the copy.
*/
{
char ch;
ps[PS_POS] = ps[PS_LENGTH] = 0;
ch = getc(fp);
while ((ch != '\n') && (ch != EOF)) {
if (ps[PS_LENGTH] < ps[PS_SIZE])
ps[PS_OFFSET + ps[PS_LENGTH]++] = ch;
ch = getc (fp);
};
};
/* char ps_scan (parse_string ps); */
/* Returns the current character in the parse_string ps.
* Does not advance the pointer in ps.
*/
int ps_matches (ps, key)
parse_string ps; char *key;
/* int ps_matches (parse_string ps, char *key) */
/* Returns TRUE if the next sequence of characters in the parse string
* matches key. If so, then advances the pointer in the parse string
* past key. Otherwise, returns FALSE.
*/
{
int i;
char *text;
ps[PS_OFFSET + ps[PS_LENGTH]] = '\0';
text = ps + PS_OFFSET + ps[PS_POS];
for (i=0; key[i] != '\0'; i++)
if (key[i] != text[i])
return (0);
ps[PS_POS] += i;
return (1);
};
int upper (ch)
int ch;
{
if (('a' <= ch) && (ch <= 'z'))
return (ch - 'a' + 'A');
else
return (ch);
};
int ps_matches_ic (ps, key)
parse_string ps; char *key;
/* int ps_matches_ic (parse_string ps, char *key) */
/* Returns TRUE if the next sequence of characters in the parse string
* matches key, after both are converted to upper case. If so, then
* advances the pointer in the parse string past key.
*/
{
int i;
char *text;
ps[PS_OFFSET + ps[PS_LENGTH]] = '\0';
text = ps + PS_OFFSET + ps[PS_POS];
for (i=0; key[i] != '\0'; i++)
if (upper(key[i]) != upper(text[i]))
return (0);
ps[PS_POS] += i;
return (1);
};
/* void ps_next (parse_string ps); */
/* Advances the pointer in the parse_string ps to the next character. */
/* int ps_size (parse_string ps); */
/* Returns the number of characters allocated for text in ps. */
/* int ps_length (parse_string ps); */
/* Returns the number of characters of text currently used in ps. */
/* int ps_pos (parse_string ps); */
/* Returns the index of the current character in ps. */