home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
High Voltage Shareware
/
high1.zip
/
high1
/
DIR9
/
WIZTOO.ZIP
/
INSTREAM.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-20
|
6KB
|
239 lines
// Module: INSTREAM.CPP
// September 20, 1993
// Fairfield, Iowa
// Aerosoft (R) Broadcast Channel Wizard Version 1.0
// Copyright (c) Aerosoft 1993 All rights reserved.
// This software source code is FREEWARE. You may be use the
// source code or redistribute the source code free of charge.
// However, you cannot sell this source code or any alteration of
// the source code.
// This module implements the member functions of class InStream.
// See file BCWIZ.TXT for more details.
#include <stdio.h>
#include <process.h>
#include <ctype.h>
#include "INSTREAM.H"
InStream::InStream (char *cpName)
{
if ((fid=fopen(cpName, "rt"))==NULL)
{
printf("*** Execution error: Can't open file %s. ***\n", cpName);
exit(0);
}
iNextCharToRead = 0;
iParsePoint = 0;
iLine = 1;
iColumn = 0;
iLen = 0;
iParenDepth = 0;
}
void InStream::ResetParsing (void)
{
iNextCharToRead = 0;
iParsePoint = 0;
iLen = 0;
iParenDepth = 0;
for (int i=0; i<BUFFSIZE; i++) caBuff[i] = '\0'; // facilitate program testing
}
int InStream::ReadyBuffer (int iPoint)
{
while (iPoint >= iNextCharToRead)
{
if (iNextCharToRead >= BUFFSIZE)
{
printf("*** Execution error: (%d:%d) Channel spec too long. ***\n",
iLine, iColumn);
exit(0);
}
if (fread(&caBuff[iNextCharToRead], 1, 1, fid) != 1) return 0;
if (caBuff[iNextCharToRead] == '\n')
{
iLine++;
iColumn = 0;
}
else
iColumn++;
iNextCharToRead++;
}
return 1;
}
static int IsWhiteSpace (char c)
{
if (c == ' ') return 1;
if (c == '\t') return 1;
if (c == '\n') return 1;
if (c == '\r') return 1;
return 0;
}
static int IsLabel (char c)
{
if (isalnum(c)) return 1;
if (c == '_') return 1;
return 0;
}
static int IsFirstOfLabel (char c)
{
if (isalpha(c)) return 1;
if (c == '_') return 1;
return 0;
}
// Nested parenthesis are removed by an inductive algorithm:
//
// basis: the first opening parenthesis '(' is not removed.
// the white-space after the next to last ')' is removed.
// NOTE: the last closing parenthesis ')' is never seen here.
//
// induction: advance parse point
// SkipNestedParen
int InStream::SkipNestedParen (void)
{
if (caBuff[iParsePoint] == '(')
{
if (++iParenDepth < 2) return 1;
}
else if (caBuff[iParsePoint] == ')')
{
if (--iParenDepth < 2)
{
// Skip any white-space here
if (!ReadyBuffer(++iParsePoint)) return 0;
while (IsWhiteSpace(caBuff[iParsePoint]))
if (!ReadyBuffer(++iParsePoint)) return NULL;
return 1;
}
}
if (!ReadyBuffer(++iParsePoint)) return 0;
return SkipNestedParen();
}
char *InStream::ParseForward (int &iLenArg)
{
// Advance to 1 past last token
iParsePoint += iLen;
if (!ReadyBuffer(iParsePoint)) return NULL;
// Skip any white-space here
while (IsWhiteSpace(caBuff[iParsePoint]))
if (!ReadyBuffer(++iParsePoint)) return NULL;
// Keep track of parenthesis nesting. Once inside a nested parenthesis,
// skip everything until past the closing parenthesis.
if (caBuff[iParsePoint] == '(')
if (!SkipNestedParen()) return NULL;
// Non-label tokens always have lenght=1
if (!IsFirstOfLabel(caBuff[iParsePoint]))
{
iLen = 1;
iLenArg = 1;
return &caBuff[iParsePoint];
}
// Determine length of label token
int iDelimPoint = iParsePoint+1;
if (!ReadyBuffer(iDelimPoint)) return NULL;
while (IsLabel(caBuff[iDelimPoint]))
if (!ReadyBuffer(++iDelimPoint)) return NULL;
iLen = iDelimPoint - iParsePoint;
iLenArg = iLen;
return &caBuff[iParsePoint];
}
char *InStream::ParseNumber (int &iLenArg)
{
// Advance to 1 past last token
iParsePoint += iLen;
if (!ReadyBuffer(iParsePoint)) return NULL;
// Skip any white-space here
while (IsWhiteSpace(caBuff[iParsePoint]))
if (!ReadyBuffer(++iParsePoint)) return NULL;
if (!isdigit(caBuff[iParsePoint]))
{
iLen = 0;
iLenArg = 0;
return &caBuff[iParsePoint];
}
// Determine length of number token
int iDelimPoint = iParsePoint+1;
if (!ReadyBuffer(iDelimPoint)) return NULL;
while (isdigit(caBuff[iDelimPoint]))
if (!ReadyBuffer(++iDelimPoint)) return NULL;
iLen = iDelimPoint - iParsePoint;
iLenArg = iLen;
return &caBuff[iParsePoint];
}
char *InStream::ParseBackward (int &iLenArg)
{
int iEndPoint = iParsePoint-1;
if (iEndPoint < 0)
{
iLenArg = 0;
return caBuff;
}
while (IsWhiteSpace(caBuff[iEndPoint]))
if (--iEndPoint < 0)
{
iLenArg = 0;
return caBuff;
}
if (!IsLabel(caBuff[iEndPoint]))
{
iLenArg = 0;
return &caBuff[iEndPoint];
}
int iScanPoint = iEndPoint-1;
if (iScanPoint < 0)
{
iLenArg = iEndPoint-iScanPoint+1;
return caBuff;
}
while (IsLabel(caBuff[iScanPoint]))
if (--iScanPoint < 0)
{
iLenArg = iEndPoint-iScanPoint+1;
return caBuff;
}
iLenArg = iEndPoint-iScanPoint;
return &caBuff[iScanPoint+1];
}