void LitResAuthenticationDataParser::startElementHandler(const char *tag, const char **attributes) { myAttributes.clear(); while (*attributes != 0) { std::string name(*attributes++); if (*attributes == 0) { break; } std::string value(*attributes++); myAttributes.insert(std::make_pair(name, value)); } processTag(tag); }
/** * Parse HTML page */ char* ParseHtml(char* html) { char* story = (char *) malloc(sizeof(char)); Stack* stack = getNewStack(); // Prime remainder of algorithm char* content = strsep(&html, "<"); if (content) { // Chew threw first tag contents char* properties = strsep(&html, ">"); properties[strlen(properties)] = 0; properties = trimWhiteSpace(properties); while(properties) { // False if the tag is not properly formatted if (!processTag(properties, stack)) { // Free story and return error message free(story); return "Story not parsed correctly. Sorry :(\n"; } // Find content content = strsep(&html, "<"); content[strlen(content)] = 0; content = trimWhiteSpace(content); // Allocate space for content story = realloc(story, strlen(story) + strlen(content) + 1); if (!story) { printf("ERROR : Error reallocating memory\n"); return NULL; } // Concatenate the two together and null terminate the string // content = trimWhiteSpace(content); strcat(story, content); story[strlen(story)] = '\0'; // Get properties of next tag properties = strsep(&html, ">"); } } return story; }
bool PmlReader::parseDocument(ZLInputStream &stream) { enum { READ_NORMAL_DATA, READ_TAG, READ_TAG_PARAMETER, } parserState = READ_NORMAL_DATA; size_t tagNameLength = 0; std::string tagName; std::string parameterString; bool startParameterReading = false; size_t tagCounter = 0; static bool FLAG = true; while (!myIsInterrupted) { const char *ptr = myStreamBuffer; const char *end = myStreamBuffer + stream.read(myStreamBuffer, pmlStreamBufferSize); if (ptr == end) { break; } const char *dataStart = ptr; bool readNextChar = true; while (ptr != end) { switch (parserState) { case READ_NORMAL_DATA: if (*ptr == '\n') { if (ptr > dataStart) { processCharData(dataStart, ptr - dataStart); } newLine(); FLAG = true; dataStart = ptr + 1; } else if (FLAG && isspace(*ptr)) { } else { FLAG = false; if (*ptr == '\\') { if (ptr > dataStart) { processCharData(dataStart, ptr - dataStart); } dataStart = ptr + 1; tagName.erase(); parserState = READ_TAG; } } break; case READ_TAG: if ((ptr == dataStart) && (tagName.empty())) { if (*ptr == '\\') { processCharData(ptr, 1); dataStart = ptr + 1; parserState = READ_NORMAL_DATA; } else { tagNameLength = findTagLength(ptr); if (tagNameLength == 0) { dataStart = ptr + 1; parserState = READ_NORMAL_DATA; ++tagCounter; } else { --tagNameLength; } } } else { if (tagNameLength == 0) { tagName.append(dataStart, ptr - dataStart); if (*ptr == '=') { dataStart = ptr + 1; parameterString.erase(); parserState = READ_TAG_PARAMETER; ++tagCounter; } else { readNextChar = false; processTag(tagName); dataStart = ptr; parserState = READ_NORMAL_DATA; ++tagCounter; } } else { --tagNameLength; } } break; case READ_TAG_PARAMETER: if (*ptr == '"') { if (!startParameterReading) { startParameterReading = true; dataStart = ptr + 1; } else { parameterString.append(dataStart, ptr - dataStart); processTag(tagName, parameterString); parserState = READ_NORMAL_DATA; dataStart = ptr + 1; startParameterReading = false; } } break; } if (readNextChar) { ++ptr; } else { readNextChar = true; } } if (dataStart < end) { switch (parserState) { case READ_NORMAL_DATA: processCharData(dataStart, end - dataStart); case READ_TAG: tagName.append(dataStart, end - dataStart); break; case READ_TAG_PARAMETER: parameterString.append(dataStart, end - dataStart); break; default: break; } } } return myIsInterrupted; }
void WX_ReplaceTagsInTextFile(char *inFname, char *outFname) { FILE *infd,*outfd; char rdBuf[READ_BUFSIZE]; char tagBuf[MAX_TAG_SIZE]; ParserControlVars pVars; if ((infd = fopen(inFname, "r")) == NULL) { DPRINTF("Tag Processor was unable to open %s for reading.\n", inFname); } else if ((outfd = fopen(outFname, "w+")) == NULL) { DPRINTF("Tag Processor was unable to open %s for writing.\n", outFname); } else { // set default formatting if no data pVars.formatForNoData = 'D'; // use dashes when no data is available pVars.spacerForMultipleRecords = ','; // when outputting data from multiple records, separate with comma pVars.outfd = outfd; // Read each line in the file and process the tags on that line. while (fgets(rdBuf, READ_BUFSIZE, infd) != NULL) { int i=0; while ((rdBuf[i] != 0) && (i < READ_BUFSIZE)) { // Init optional vars and strings to null int recordNum = 0; char recordNumStr[MAX_TAG_SIZE]; recordNumStr[0] = 0; pVars.formatControlStr[0] = 0; pVars.outputStr[0] = 0; if (strncmp(&rdBuf[i], "^WXTAG_", 7) == 0) { i+=7; // Skip over begining of tag if (extractTagParam(rdBuf, pVars.sensorToGetFrom, &i) != 0) { // ie IDU, ODU, WG, RG, etc DPRINTF("Unable to get Tag Type on line: \n %s\n", rdBuf); sprintf(pVars.outputStr,"WXERROR_BADTYPE-%s",pVars.sensorToGetFrom); fputs(pVars.outputStr,outfd); } else if (extractTagParam(rdBuf, pVars.fieldToGet, &i) != 0){ // ie TEMP, DEWPOINT, SPEED, etc DPRINTF("Unable to get Tag Field on line: \n %s\n",rdBuf); sprintf(pVars.outputStr,"WXERROR_BADFIELD-%s-%s",pVars.sensorToGetFrom,pVars.fieldToGet); fputs(pVars.outputStr,outfd); } else { // These next two are optional, the format control string has an _ in front of it, while the record number has a - if (rdBuf[i] == '_') extractTagParam(rdBuf, pVars.formatControlStr, &i); // Optional - _M, _Y, etc if (rdBuf[i] == '#') { i++; extractTagParam(rdBuf, recordNumStr, &i); // Optional #1:96:1 } if (rdBuf[i] == '^') { // got to end of tag successfully i++; // Skip over end of tag character if (recordNumStr[0] == 0) { pVars.weatherDatap= &wxData; // Use the current dataset (not historical) to process this tag processTag(&pVars); fputs(pVars.outputStr, outfd); //DPRINTF(pVars.outputStr); // echo to local console } else if (recordNumStr[1] == 'I') { // Get min historical value pVars.weatherDatap = WX_GetMinDataRecord(); processTag(&pVars); fputs(pVars.outputStr, outfd); //DPRINTF(pVars.outputStr); // echo to local console } else if (recordNumStr[1] == 'A') { // Get max historical record pVars.weatherDatap= WX_GetMaxDataRecord(); processTag(&pVars); fputs(pVars.outputStr, outfd); //DPRINTF(pVars.outputStr); // echo to local console } else { int recordNumList[WX_NUM_RECORDS_TO_STORE]; int recordCount, i; if ((recordCount = processRecordNumberStr(recordNumStr, recordNumList)) != 0) for (i=0;i<recordCount;i++) { //printf("getting %d of %d: ",recordNumList[i], recordCount); pVars.weatherDatap = WX_GetWeatherDataRecord(recordNumList[i]); processTag(&pVars); fputs(pVars.outputStr, outfd); //DPRINTF(pVars.outputStr); // echo to local console if (i < (recordCount-1)) { fputc(pVars.spacerForMultipleRecords, outfd); //DPRINTF("%c",pVars.spacerForMultipleRecords); } //printf("\n"); } else { sprintf(pVars.outputStr, "WXERROR_BADRECORDNUM-%s-%s",pVars.sensorToGetFrom,pVars.fieldToGet); fputs(pVars.outputStr,outfd); DPRINTF("Error processing tag: %s\n",pVars.outputStr); } } } else { sprintf(pVars.outputStr, "WXERROR_BADTAG-%s-%s",pVars.sensorToGetFrom,pVars.fieldToGet); fputs(pVars.outputStr,outfd); DPRINTF("Error processing sensor tag: %s\n",pVars.outputStr); } } } else { //DPRINTF("%c",rdBuf[i]); // echo to local console fputc(rdBuf[i++], outfd); } } } fclose(infd); fclose(outfd); } }