📄 parser.cc
字号:
/* * Copyright (c) 1997-2001 University of Southern California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the Information Sciences * Institute of the University of Southern California. * 4. Neither the name of the University nor of the Institute may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */#include <stdlib.h>#include <stdio.h>#include <ctype.h>#include "parser.h"#include "trace.h"//----------------------------------------------------------------------////----------------------------------------------------------------------Attribute::Attribute(char _flag, void * _value_ptr, char * _value_type, char * _default_value, bool _required, char * _label) { flag = _flag; value_ptr = _value_ptr; other_ptr = NULL; value_type = _value_type; default_value = _default_value; required = _required; seen = false; label = _label; next = NULL; }//----------------------------------------------------------------------////----------------------------------------------------------------------char * Attribute::parseValue(char * run, char * line) { char buffer[MAXVALUE]; double angle; if (!strcmp(value_type, "int")) { *((int *) value_ptr) = atoi(run); run = advanceToSpace(run); } else if (!strcmp(value_type, "double")) { *((double *) value_ptr) = atof(run); run = advanceToSpace(run); } else if (!strcmp(value_type, "string") || !strcmp(value_type, "hex")) { run = extractString(run,(char *) value_ptr); } else if (!strcmp(value_type, "string l")) { run = extractString(run, buffer); // Check for use as length in link event if (other_ptr && isdigit(buffer[0])) { fprintf(stderr, "Nam syntax has changed: %sPlease use -h instead of -l for length.\n\n",line); *((double *) other_ptr) = atof(buffer); } else { strcpy((char *) value_ptr, buffer); } } else if (!strcmp(value_type, "color")) { // Checking for angle to be bacwards compatible // with the -o flag standing for orientation run = extractString(run, buffer); angle = determineAngle(buffer); if (angle == -1.0) { // We must have a color strcpy((char *) value_ptr, buffer); } else { if (other_ptr) { fprintf(stderr, "Nam syntax has changed: %sPlease use -O instead of -o for orientation.\n\n",line); *((double *) other_ptr) = angle; } } } else if (!strcmp(value_type, "time")) { if (*run == '*') { *((double *) value_ptr) = 0.0; run++; } else { *((double *) value_ptr) = atof(run); run = advanceToSpace(run); } } else if (!strcmp(value_type, "comment")) { if (*run == '{') { run++; // Grab first value (Source) ((PacketAttr *)value_ptr)->esrc = atoi(run); // Grab second value (Destination) run = advanceToSpace(run); run = eatSpaces(run); ((PacketAttr *) value_ptr)->edst = atoi(run); // Eat the rest of the comment while (*run != '}' && *run != '\n') { run++; } // Eat last curly brace if (*run == '}') { run++; } } else { fprintf(stderr, "Error in comment field: %s\n", line); } } else if (!strcmp(value_type, "flag")) { if (*run == '-' || *run == '\0' || isspace(*run)) { // For cases where the flag is -x followed by another attribute // or on the end of the line. If -x is followed by another attribute // run should point to - in -n 1 // For example -x -n 1 or -x\0 or -n 1 -x\n *((int *) value_ptr) = 1; } else if (isdigit(*run)) { // For intialization cases or when you get -x 2 *((int *) value_ptr) = atoi(run); run = advanceToSpace(run); } else { // The syntax is really messed up so report an // error and skip over the value fprintf(stderr, "Invalid value for flag attribute type in line: %s\n",line); run = advanceToSpace(run); } } else if (!strcmp(value_type, "char")) { *((char *) value_ptr) = *run; run = advanceToSpace(run); } else if (!strcmp(value_type, "orientation")) { run = extractString(run, buffer); angle = determineAngle(buffer); if (angle == -1.0) { angle = 0.0; } *((double *) value_ptr) = angle; } else if (!strcmp(value_type, "packet source")) { if (*run == '*') { *((int *) value_ptr) = -1; run++; } else { *((int *) value_ptr) = atoi(run); run = advanceToSpace(run); } } else if (!strcmp(value_type, "shape")) { run = extractString(run, buffer); // Check for being used as y_vel_ in old nam // syntax if (isdigit(buffer[0]) || (buffer[0] == '-' && isdigit(buffer[1]))) { if (other_ptr) { fprintf(stderr, "Nam syntax has changed: %sPlease use -V instead of -v for y velocity \n\n",line); *((double *) other_ptr) = atof(buffer); } } else { strcpy((char *) value_ptr, buffer); } } else if (!strcmp(value_type, "tcl expression")) { //*((char **) value_ptr) = run; *((int *) value_ptr) = run - line; //Advance to next flag or end of line run = advanceToEndofLine(run); } return run;}//----------------------------------------------------------------------////----------------------------------------------------------------------voidAttribute::setDefaultValue() { parseValue(default_value, default_value);}//----------------------------------------------------------------------// double// Attribute::determineAngle(char * buffer)// - Determines angle based on up-right, up, down, down-left, etc// constants. Also will try to extract a numeric angle from buffer// if unable to determine angle -1 is returned//----------------------------------------------------------------------doubleAttribute::determineAngle(char * buffer) { double angle; if (isalpha(*buffer) || *buffer == '"' || *buffer == '#') { // Set angle based on constants if (!strcmp(buffer, "right")) { angle = 0.0; } else if (!strcmp(buffer, "up-right") || !strcmp(buffer, "right-up")) { angle = 0.25; } else if (!strcmp(buffer, "up")) { angle = 0.50; } else if (!strcmp(buffer, "up-left") || !strcmp(buffer, "left-up")) { angle = 0.75; } else if (!strcmp(buffer, "left")) { angle = 1.0; } else if (!strcmp(buffer, "left-down") || !strcmp(buffer, "down-left")) { angle = 1.25; } else if (!strcmp(buffer, "down")) { angle = 1.50; } else if (!strcmp(buffer, "down-right") || !strcmp(buffer, "right-down")) { angle = 1.75; } else { angle = -1.0; } } else { angle = atof(buffer); } return angle;}//----------------------------------------------------------------------////----------------------------------------------------------------------TraceSyntax::TraceSyntax(char event_id, char * _label) { type = event_id; label = _label; attributes = NULL; last_attribute = NULL; total_required_attributes = 0; number_of_attributes = 0; next = NULL;}//----------------------------------------------------------------------////----------------------------------------------------------------------Attribute * TraceSyntax::addAttribute(char flag, void * value_ptr, char * type, char * default_value, bool required, char * label) { Attribute * attribute = new Attribute(flag, value_ptr, type, default_value, required, label); if (attribute) { // Add attribute to list of attributes for this ParseTraceSyntax if (!attributes) { attributes = attribute; last_attribute = attribute; } else { last_attribute->next = attribute; last_attribute = attribute; } number_of_attributes++; if (required) { total_required_attributes++; } } return attribute;}//----------------------------------------------------------------------// Attribute * // TraceSyntax::addAttribute(char flag, void * value_ptr, char * type,// char * default_value, bool required,// char * label, void * other_ptr) //// - other_ptr is a pointer to an alternate location to put data.// It is used to enable backwards compatability with the old// nam syntax.//----------------------------------------------------------------------Attribute * TraceSyntax::addAttribute(char flag, void * value_ptr, char * type, char * default_value, bool required, char * label, void * other_ptr) { Attribute * attribute; attribute = addAttribute(flag, value_ptr, type, default_value, required, label); if (attribute) { attribute->other_ptr = other_ptr; } return attribute; }//----------------------------------------------------------------------////----------------------------------------------------------------------voidTraceSyntax::setDefaultAttributeValues() { Attribute * attribute = attributes; while (attribute) { attribute->setDefaultValue(); attribute->seen = false; attribute = attribute->next; }}//----------------------------------------------------------------------////----------------------------------------------------------------------//<zheng: +++>bool ParseTable::nam4wpan = false;int ParseTable::wpan_bradius = -1;//</zheng: +++>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -