main() { symtabADT table; scannerADT scanner; string line, name, value; table = NewSymbolTable(); scanner = NewScanner(); SetScannerSpaceOption(scanner, IgnoreSpaces); while (TRUE) { printf("-> "); line = GetLine(); if (StringEqual(line, "quit")) break; SetScannerString(scanner, line); name = ReadToken(scanner); if (MoreTokensExist(scanner)) { if (!StringEqual(ReadToken(scanner), "=")) { Error("Missing equal sign in assignment"); } value = ReadToken(scanner); if (MoreTokensExist(scanner)) { Error("Statement contains additional data"); } Enter(table, name, value); } else { value = Lookup(table, name); if (value == UNDEFINED) { printf("'%s' is undefined.\n", name); } else { printf("%s\n", value); } } } FreeSymbolTable(table); }
main() { dataBlockT db; string line, cmd, token, var; int pos; printf("List structure test program\n"); printf("Type \"help\" for help\n"); db = NewDataBlock(); while (TRUE) { printf(">"); line = GetLine(); SetScannerString(db->scanner, line); cmd = ConvertToLowerCase(ReadToken(db->scanner)); if (IsVariableName(cmd)) { var = cmd; token = ReadToken(db->scanner); if (StringEqual(token, "")) { DisplayStringList(GetValue(db, var)); } else if (StringEqual(token, "=")) { ParseOperation(db, var); } else { Error("Unexpected token %s", token); } } else if (StringEqual(cmd, "help")) { HelpCmd(); } else if (StringEqual(cmd, "quit")) { break; } else { SaveToken(db->scanner, cmd); ParseOperation(db, NULL); } } }
//=========================================================================== // DoKeyword // The next token is expected to be a keyword. //=========================================================================== int DoKeyword(void) { ReadToken(); if(ISTOKEN("syntax")) { ReadToken(); if(ISTOKEN("simple")) { syntax = STX_SIMPLE; Message("Using simple syntax."); } else { Message("Unknown syntax '%s'.", token); return false; } } else if(ISTOKEN("group")) { ReadToken(); group = strtol(token, 0, 0); if(group < 1 || group > NUM_GROUPS) { Message("Illegal group number %i (1..%i allowed).", group, NUM_GROUPS); return false; } Message("Switching to group %i.", group); group--; // Make it an index number, though. } return true; }
int HandleCRAMMD5( int inSocket ) { unsigned char pChallenge[255] = { 0, }; char pHostname[128] = { 0, }; char *pResponse = NULL; char *pUsername = NULL; struct timeval stCurrentTime; int iResult = -1; // we will be using ReadToken and WriteToken to send data back and forth for this // example, though not necessarily the most efficient, it is simplistic for this // demonstration. // Since CRAM-MD5 was requested, let's generate a challenge and send it to the client // using example method in RFC 1460, page 12. gethostname( pHostname, 127 ); gettimeofday( &stCurrentTime, NULL ); // assume no error occurred snprintf( pChallenge, 255, "<%ld.%ld@%s>", (long) getpid(), stCurrentTime.tv_sec, pHostname ); printf( "Sending challenge %s\n", pChallenge ); // send our challenge to the client if( SendToken(inSocket, pChallenge, strlen(pChallenge)) > 0 ) { // now wait for username and response to return if( ReadToken(inSocket, &pUsername) > 0 ) { if( ReadToken(inSocket, &pResponse) > 0 ) { // here is where we authenticate the user using Open Directory iResult = AuthCRAMMD5( pUsername, pChallenge, pResponse ); // send a response if( iResult == eDSNoErr ) { SendToken( inSocket, "Accepted", sizeof("Accepted") ); } else { SendToken( inSocket, "Rejected", sizeof("Rejected") ); } } } } // free up any strings we allocated if( pUsername != NULL ) { free( pUsername ); pUsername = NULL; } if( pResponse != NULL ) { free( pResponse ); pResponse = NULL; } return iResult; }
static void SelectionOperation(dataBlockT db, string lhs) { string var; int pos; if (lhs != NULL) Error("NthElement result cannot be assigned"); CheckForToken(db->scanner, "("); var = ConvertToLowerCase(ReadToken(db->scanner)); CheckForToken(db->scanner, ","); pos = StringToInteger(ReadToken(db->scanner)); CheckForToken(db->scanner, ")"); printf("%s\n", NthElement(GetValue(db, var), pos)); }
int HandleCleartext( int inSocket ) { char *pUsername = NULL; char *pPassword = NULL; char *pRecordname = NULL; int iResult = -1; // we will be using ReadToken and WriteToken to send data back and forth for this // example, though not necessarily the most efficient, it is simplistic for this // demonstration. // we expect 2 tokens, username then password // read the username from the network stream if( ReadToken(inSocket, &pUsername) > 0 ) { // read the password from the network stream if( ReadToken(inSocket, &pPassword) > 0 ) { iResult = AuthCleartext( pUsername, pPassword ); // send a response if( iResult == eDSNoErr ) { SendToken( inSocket, "Accepted", sizeof("Accepted") ); } else { SendToken( inSocket, "Rejected", sizeof("Rejected") ); } } } // free any allocated strings if( pUsername != NULL ) { free( pUsername ); pUsername = NULL; } if( pPassword != NULL ) { free( pPassword ); pPassword = NULL; } if( pRecordname != NULL ) { free( pRecordname ); pRecordname = NULL; } return iResult; }
void test(void) { char buf[128]; xprintf(0x21, "Hello World!\r\n"); ReadToken(buf, TOKEN_PASSWORD, 8); xprintf(0x21, "password = '******'\r\n", buf); }
/* ================= idLexer::SkipBracedSection Skips until a matching close brace is found. Internal brace depths are properly skipped. ================= */ int idLexer::SkipBracedSection( bool parseFirstBrace ) { idToken token; int depth; depth = parseFirstBrace ? 0 : 1; do { if( !ReadToken( &token ) ) { return false; } if( token.type == TT_PUNCTUATION ) { if( token == "{" ) { depth++; } else if( token == "}" ) { depth--; } } } while( depth ); return true; }
void ParsedObject::MatchToken(const LispString* aToken) { if (aToken != iLookAhead) Fail(); ReadToken(); }
/* ================ Lexer::ReadFloat ================ */ float Lexer::ReadFloat( void ) { if ( !ReadToken() ) throw LexerError( LexerError::END_OF_FILE ); if ( !String::IsNumeric( token.GetString() ) ) throw LexerError( LexerError::BAD_TOKEN, token.line, "float", token.GetString() ); return String::ToFloat( token.GetString() ); }
static TokenStream *PrescanMacroArg(TokenStream *a, yystypepp * yylvalpp) { int token; TokenStream *n; RewindTokenStream(a); do { token = ReadToken(a, yylvalpp); if (token == CPP_IDENTIFIER && LookUpSymbol(macros, yylvalpp->sc_ident)) break; } while (token > 0); if (token <= 0) return a; n = NewTokenStream("macro arg", 0); PushEofSrc(); ReadFromTokenStream(a, 0, 0); while ((token = cpp->currentInput->scan(cpp->currentInput, yylvalpp)) > 0) { if (token == CPP_IDENTIFIER && MacroExpand(yylvalpp->sc_ident, yylvalpp)) continue; RecordToken(n, token, yylvalpp); } PopEofSrc(); DeleteTokenStream(a); return n; } /* PrescanMacroArg */
TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream *a, TPpToken* ppToken) { int token; TokenStream *n; RewindTokenStream(a); do { token = ReadToken(a, ppToken); if (token == CPP_IDENTIFIER && LookUpSymbol(ppToken->atom)) break; } while (token != EOF); if (token == EOF) return a; n = new TokenStream; PushEofSrc(); ReadFromTokenStream(a, 0, 0); while ((token = currentInput->scan(this, currentInput, ppToken)) > 0) { if (token == CPP_IDENTIFIER && MacroExpand(ppToken->atom, ppToken, 0) == 1) continue; RecordToken(n, token, ppToken); } PopEofSrc(); delete a; return n; }
bool FFeedbackContextMarkup::ParseCommand(const FString& Line, FFeedbackContext* Warn) { const TCHAR *Text = *Line; if(ReadToken(Text, TEXT("@progress"))) { FString Status; bool bHaveStatus = ReadString(Text, Status); int32 Numerator, Denominator; bool bHaveProgress = ReadProgress(Text, Numerator, Denominator); if(*Text == 0) { if(bHaveProgress && bHaveStatus) { Warn->StatusUpdate(Numerator, Denominator, FText::FromString(Status)); return true; } if(bHaveProgress) { Warn->UpdateProgress(Numerator, Denominator); return true; } } } return false; }
bool IACFile::Read(wxInputStream &stream) { bool isok = false; // true if minimum one token was read from file Invalidate(); wxString token; m_tokensI = 0; if (stream.IsOk()) { for (;;) { token = ReadToken(stream); if (!token.IsEmpty()) { m_tokens.Add(token); m_tokensI++; isok = true; } else { break; } } } m_tokensI = 0; // for (std::vector<size_t>::iterator it = m_newlineTokens.begin(); it != m_newlineTokens.end(); ++it) // { // wxMessageBox( wxString::Format( _T("ID: %i :"), *it ) + m_tokens[*it] ); // } if (isok) { // decode tokens if some were found isok = Decode(); } m_isok = isok; return isok; }
/************************************************************* * Que 3. Post fix expression evaluation * * Comment : 1) prepare stack for holding operands only. * 2) Push when numbers are read. * 3) Pop two numbers when ever operand is encountered. * * Parameters : expression array * Returns : int (eval result) *************************************************************/ int PostFixEval(char A[]) { LinkedListStack stack; int tokenType = EMPTY,num,i=0;; char token; while((tokenType = ReadToken(A,i,token,num)) != EMPTY) { if(tokenType == OPERAND) //Numbers { stack.Push(token - '0'); } else if(tokenType == OPERATOR) //Mathematical operator { int A = stack.Pop(); int B = stack.Pop(); stack.Push(Operate(A,B,token)); } else { //Should not reach here assert(1 == 0); } } if(stack.IsEmpty()) return -1; return stack.Pop(); }
/* macro_scan --- ** return the next token for a macro expanion, handling macro args */ static int macro_scan(MacroInputSrc *in, yystypepp * yylvalpp) { int i; int token = ReadToken(in->mac->body, yylvalpp); if (token == CPP_IDENTIFIER) { for (i = in->mac->argc - 1; i >= 0; i--) if (in->mac->args[i] == yylvalpp->sc_ident) break; if (i >= 0) { ReadFromTokenStream(in->args[i], yylvalpp->sc_ident, 0); return cpp->currentInput->scan(cpp->currentInput, yylvalpp); } } if (token > 0) return token; in->mac->busy = 0; cpp->currentInput = in->base.prev; if (in->args) { for (i = in->mac->argc - 1; i >= 0; i--) DeleteTokenStream(in->args[i]); free(in->args); } free(in); return cpp->currentInput->scan(cpp->currentInput, yylvalpp); } /* macro_scan */
TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream* a, TPpToken* ppToken, bool newLineOkay) { int token; TokenStream *n; RewindTokenStream(a); do { token = ReadToken(a, ppToken); if (token == PpAtomIdentifier && LookUpSymbol(ppToken->atom)) break; } while (token != EndOfInput); if (token == EndOfInput) return a; n = new TokenStream; pushInput(new tMarkerInput(this)); pushTokenStreamInput(a); while ((token = scanToken(ppToken)) != tMarkerInput::marker) { if (token == PpAtomIdentifier && MacroExpand(ppToken->atom, ppToken, false, newLineOkay) != 0) continue; RecordToken(n, token, ppToken); } popInput(); delete a; return n; }
bool ParseTag(FColor& OutColor) { FString TagString; EStringParserToken Token = ReadToken(); for (; Token != EStringParserToken::EndOfString && Token != EStringParserToken::CloseTag; Token = ReadToken()) { if (Token == EStringParserToken::RegularChar) { TagString.AppendChar(DataString[DataIndex]); } DataIndex++; } bool bResult = false; if (Token == EStringParserToken::CloseTag) { const FString TagColorLower = TagString.ToLower(); const bool bIsColorName = GColorList.IsValidColorName(*TagColorLower); if (bIsColorName) { OutColor = GColorList.GetFColorByName(*TagColorLower); bResult = true; } else { bResult = OutColor.InitFromString(TagString); } } return bResult; }
char *ReadToken (server_request *ServerRequest) { char *c = &ServerRequest->Request[ServerRequest->TokenizerIndex]; if (!*c) { return NULL; } char *Start = c; s32 Length = 0; while (*c != 0 && *c != ' ' && *c != '\n' && *c != '\r') { ++Length; ++c; } if (!Length) { ++ServerRequest->TokenizerIndex; return ReadToken(ServerRequest); } ServerRequest->TokenizerIndex += Length; char *Token = PushMemory(Length + 1); strncpy(Token, Start, Length); Token[Length] = 0; return Token; }
bool JsonString::ReadValue() { Token token; ReadToken(token); bool successful = true; switch(token.type_) { case tokenObjectBegin: objnum++; successful = ReadObject(token); break; case tokenArrayBegin: successful = ReadArray(token); break; case tokenNumber: case tokenString: case tokenTrue: case tokenFalse: case tokenNull: break; default: return false; } return successful; }
bool Loader::ReadBool(bool *k) { double d = 0; char token[LOADER_INPUT_LENGTH]; if (ReadDouble(d)) { if (IsEqual(d, 1.0)) *k = true; else if (IsZero(d)) *k = false; else return false; return true; } else if (ReadToken(token)) { if (token[0] == 't') *k = true; else if (token[0] == 'f') *k = false; else return false; return true; } else return false; }
/* ================ Lexer::FindToken ================ */ void Lexer::FindToken( const char *str ) { while ( ReadToken() ) { if ( String::Icmp( token.GetString(), str ) == 0 ) return; } throw LexerError( LexerError::MISSING_TOKEN, token.line, str, "[EOF]" ); }
uint32 ParseTag( FString& OutData ) { FString TagString; int32 OryginalIndex = Index; uint8 token = ReadToken(); while (token != EndOfString && token != CloseTag) { if (token == RegularChar) { TagString.AppendChar(DataString[Index++]); } token = ReadToken(); } int OutTag = ErrorTag; if (token != CloseTag) { Index = OryginalIndex; OutData = FString::Printf( TEXT("{%s"), *TagString); OutData.AppendChar(DataString[Index-1]); return OutTag; } if (GColorList.IsValidColorName(*TagString.ToLower())) { OutTag = DefinedColor; OutData = TagString; } else { FColor Color; if (Color.InitFromString(TagString)) { OutTag = OtherColor; OutData = TagString; } else { OutTag = ErrorTag; OutData = FString::Printf( TEXT("{%s"), *TagString); OutData.AppendChar(DataString[Index-1]); } } //Index++; return OutTag; }
static void ConcatOperation(dataBlockT db, string lhs) { listADT result; string v1, v2; CheckForToken(db->scanner, "("); v1 = ConvertToLowerCase(ReadToken(db->scanner)); CheckForToken(db->scanner, ","); v2 = ConvertToLowerCase(ReadToken(db->scanner)); CheckForToken(db->scanner, ")"); result = ListConcat(GetValue(db, v1), GetValue(db, v2)); if (lhs == NULL) { DisplayStringList(result); } else { SetValue(db, lhs, result); } }
static void ConsOperation(dataBlockT db, string lhs) { listADT result; string id, var; CheckForToken(db->scanner, "("); id = ReadToken(db->scanner); CheckForToken(db->scanner, ","); var = ConvertToLowerCase(ReadToken(db->scanner)); CheckForToken(db->scanner, ")"); result = ListCons(id, GetValue(db, var)); if (lhs == NULL) { DisplayStringList(result); } else { SetValue(db, lhs, result); } }
static void CheckForToken(scannerADT scanner, string expected) { string token; token = ReadToken(scanner); if (!StringEqual(token, expected)) { Error("Found '%s' when expecting '%s'", token, expected); } }
/* ================ Lexer::CheckToken ================ */ bool Lexer::CheckToken( const char *string ) { if ( !ReadToken() ) throw LexerError( LexerError::END_OF_FILE ); if ( String::Icmp( token.GetString(), string ) != 0 ) { UnreadToken(); return false; } return true; }
// expect a token int idTokenParser::ExpectAnyToken( idToken* token ) { if( !ReadToken( token ) ) { Error( "couldn't read expected token" ); return 0; } return 1; }
Graph *ReadGraph(char *filename, LabelList *labelList, BOOLEAN directed) { Graph *graph; FILE *graphFile; ULONG lineNo; // Line number counter for graph file char token[TOKEN_LEN]; ULONG vertexListSize = 0; // Size of currently-allocated vertex array ULONG edgeListSize = 0; // Size of currently-allocated edge array ULONG vertexOffset = 0; // Dummy argument to ReadVertex and ReadEdge // Allocate graph graph = AllocateGraph(0,0); // Open graph file graphFile = fopen(filename,"r"); if (graphFile == NULL) { fprintf(stderr, "Unable to open graph file %s.\n", filename); exit(1); } // Parse graph file lineNo = 1; while (ReadToken(token, graphFile, &lineNo) != 0) { if (strcmp(token, "v") == 0) // read vertex ReadVertex(graph, graphFile, labelList, &vertexListSize, &lineNo, vertexOffset); else if (strcmp(token, "e") == 0) // read 'e' edge ReadEdge(graph, graphFile, labelList, &edgeListSize, &lineNo, directed, vertexOffset); else if (strcmp(token, "u") == 0) // read undirected edge ReadEdge(graph, graphFile, labelList, &edgeListSize, &lineNo, FALSE, vertexOffset); else if (strcmp(token, "d") == 0) // read directed edge ReadEdge(graph, graphFile, labelList, &edgeListSize, &lineNo, TRUE, vertexOffset); else { fclose(graphFile); FreeGraph(graph); fprintf(stderr, "Unknown token %s in line %lu of graph file %s.\n", token, lineNo, filename); exit(1); } } fclose(graphFile); //***** trim vertex, edge and label lists return graph; }
void CSelectionKeyHandler::LoadSelectionKeys() { std::ifstream ifs(filesystem.LocateFile("selectkeys.txt").c_str()); selectNumber = 0; while (ifs.peek() != EOF && !ifs.eof()) { std::string key, sel; ifs >> key; if (ifs.peek() == EOF || ifs.eof()) break; ifs >> sel; bool shift=false; bool control=false; bool alt=false; unsigned char keyname=0; while(true){ std::string s=ReadToken(key); if(s=="Shift"){ shift=true; } else if (s=="Control"){ control=true; } else if (s=="Alt"){ alt=true; } else { char c=s[0]; if(c>='A' && c<='Z') keyname=SDLK_a + (c - 'A'); if(c>='0' && c<='9') keyname=SDLK_0 + (c -'0'); break; } ReadDelimiter(key); } std::string keybindstring; if ( alt ) keybindstring += "Alt"; if ( control ) { if ( keybindstring.size() != 0 ) keybindstring += "+"; keybindstring += "Ctrl"; } if ( shift ) { if ( keybindstring.size() != 0 ) keybindstring += "+"; keybindstring += "Shift"; } if ( keybindstring.size() != 0 ) keybindstring += "+"; keybindstring += keyname; keyBindings->Command( "bind " + keybindstring + " select " + sel ); } }