/*---------------------------------------------------------------------*/ BOOL OpenStateForWrite(H_ARCHIVE harchive) { _archive[harchive].last_error = ARC_NO_ERROR; _archive_error = ARC_NO_ERROR; /* Create filespec for state file and open for read */ sprintf(_archive[harchive].filespec, "%s%c%s", _archive[harchive].path, PATH_DELIMITER, STATE_FILENAME); if ((_archive[harchive].file = FileOpenForRead(_archive[harchive].filespec)) == VOID_H_FILE) { ArchiveLog(ARC_LOG_ERRORS, "OpenStateForWrite: Error opening state file: %s", _archive[harchive].filespec); _archive[harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* Read the current state */ if (!ReadState(harchive)) return (FALSE); if (_archive[harchive].state.write) { ArchiveLog(ARC_LOG_ERRORS, "OpenStateForWrite: Archive is open for write by another process: %s", _archive[harchive].path); _archive[harchive].last_error = ARC_PERMISSION_DENIED; return (FALSE); } if (!FileClose(_archive[harchive].file)) { _archive[harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* Reopen for write */ if ((_archive[harchive].file = FileOpenForWrite(_archive[harchive].filespec)) == VOID_H_FILE) { ArchiveLog(ARC_LOG_ERRORS, "OpenStateForWrite: Error opening state file: %s", _archive[harchive].filespec); _archive[harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* Mark as open for write */ _archive[harchive].state.write = TRUE; /* Archive is open for read-write access */ _archive[harchive].access = ARC_WRITE; /* Start the update timer */ Timer48Start(&_archive[harchive].update, 0); if (!WriteState(harchive)) return (FALSE); /* Start the update timer */ Timer48Start(&_archive[harchive].update, UPDATE_INTERVAL); ArchiveLog(ARC_LOG_MAXIMUM, "State file opened for write: %s", _archive[harchive].filespec); return (TRUE); }
Model * CreateModelFromObjFile( const std::string & filename ) { File *file = FileOpenForRead( filename ); ParseState ps; while ( file->AtEnd() == false && ps.mode != Mode_Failed ) { string line = file->ReadLine(); vector< Token > tokens = TokenizeString( line.c_str() ); ps.ProcessLine( tokens ); } delete file; if ( ps.mode == Mode_Failed ) { return NULL; } Model *m = new Model( filename ); m->SetPrimitive( Primitive_Triangles ); VertexBuffer & vb = m->GetVertexBuffer(); vb.SetVarying( ps.varying ); //Output( "model %s varying = %d", filename.c_str(), ps.varying ); vb.SetData( (int)ps.vbdata.size() * sizeof( float ), &ps.vbdata[0] ); m->GetIndexBuffer().SetData( (int)ps.ibdata.size() * sizeof( ushort ), &ps.ibdata[0] ); return m; }
/*---------------------------------------------------------------------*/ BOOL OpenStateForRead(H_ARCHIVE harchive) { _archive[harchive].last_error = ARC_NO_ERROR; _archive_error = ARC_NO_ERROR; /* Build the state filespec and open */ sprintf(_archive[harchive].filespec, "%s%c%s", _archive[harchive].path, PATH_DELIMITER, STATE_FILENAME); if ((_archive[harchive].file = FileOpenForRead(_archive[harchive].filespec)) == VOID_H_FILE) { ArchiveLog(ARC_LOG_ERRORS, "OpenStateForRead: Error opening state file: %s", _archive[harchive].filespec); _archive[harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* Read the current state */ if (!ReadState(harchive)) { ArchiveLog(ARC_LOG_ERRORS, "OpenStateForRead: Error trying to retrieve state: %s", _archive[harchive].filespec); FileClose(_archive[harchive].file); return (FALSE); } /* File is open for read access */ _archive[harchive].access = ARC_READ; /* Save file status so we can detect changes to the file */ if (!GetFileStat(_archive[harchive].filespec, &_archive[harchive].stat)) { ArchiveLog(ARC_LOG_ERRORS, "OpenStateForRead: Error retrieving file state: %s", _archive[harchive].filespec); FileClose(_archive[harchive].file); return (FALSE); } ArchiveLog(ARC_LOG_MAXIMUM, "State file opened for read: %s", _archive[harchive].filespec); return (TRUE); }
int LibFile_INI::StartParse(bool ingoreInvalidLine /* = true */, ExtProc_CB externalProcess /* = NULL */) { int retVal; RETURN_CHK( retVal, FileOpenForRead(1024) ); gExtProc_CB = externalProcess; LibStringClass str; INI_PARSE_STATE_t state = IPS_FINDING_SECTOR; while ( 0 == this->GetLine() ) { // 1. clearify whole line str.Init(this->lineStr); str.RemoveRestString("//"); str.RemoveRestString(";"); str.RemoveEmptyPrefixChar(); str.RemoveEmptyPostfixChar(); // 2. skip empty line if (0 == str.Size()) { continue; } // 3. handle symbol of equal str.InsertBefore("=", " "); str.InsertAfter("=", " "); switch (state) { case IPS_FINDING_SECTOR: { if (_IsSector(str.str)) { _AddNewSector(str.str); state = IPS_EXTRACTING_VAR; } else { // drop it } } break; case IPS_EXTRACTING_VAR: { if (_IsSector(str.str)) { _AddNewSector(str.str); } else { if (gExtProc_CB != NULL) { int retVal = (*gExtProc_CB)(str); if (retVal) break; } str.Split(true); if (str.subStrVector.size() == 1) { _AddNewSingleVarToVector(str.subStrVector[0]); } else if (str.subStrVector.size() < 3) { if (ingoreInvalidLine) break; else goto ERROR_HANDLE; } else if (0 != str.subStrVector[1].compare("=")) { if (ingoreInvalidLine) break; else goto ERROR_HANDLE; } else { retVal = _AddNewVarToMap(str.subStrVector[0], str.subStrVector[2]); if (retVal) { if (ingoreInvalidLine) break; else goto ERROR_HANDLE; } } } } break; default: EXIT_LOC_IF(1); break; } } return 0; ERROR_HANDLE: printf("Invalid variable string in line:%d\n", this->lineCount); printf(">> %s\n", str.CStr()); EXIT_LOC_IF(1); return 1; }
/*---------------------------------------------------------------------*/ BOOL GetNextDataPacket(H_STREAM hstream, RF_HEADER * hdr, RF_PACKET * packet) { TIME_RANGE time; ASSERT(packet != NULL); _archive[_reads[hstream].harchive].last_error = ARC_NO_ERROR; /* Return the next in range data packet */ while (TRUE) { debugf("GNDP read(%u)...", hstream); if (!ReadPacket(_reads[hstream].file, hdr, packet)) { /* Deal with read error... */ debugf("GNDP can't..."); if (FileError()) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* If end of file, move on to the next one... */ if (!FileClose(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } debugf("GNDP next event..."); if (!ArchiveFindNextEvent(_reads[hstream].harchive, &_reads[hstream].criteria, &_reads[hstream].event)) { _archive[_reads[hstream].harchive].last_error = ARC_END_OF_DATA; debugf("GNDP failed..."); return (FALSE); } if ((_reads[hstream].file = FileOpenForRead(_reads[hstream].event.filespec)) == VOID_H_FILE) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } continue; } debugf("GNDP pkt %u:%04hX?%04hX...", hdr->type, hdr->seq, _reads[hstream].in_seq); /* Packet sequence checking */ if (_reads[hstream].in_seq == VOID_UINT16) _reads[hstream].in_seq = hdr->seq; else if (_reads[hstream].in_seq != hdr->seq) { debugf("GNDP bad seq..."); _reads[hstream].in_seq = VOID_UINT16; if (!UnReadPacket(_reads[hstream].file)) { debugf("GNDP can't UNread"); _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } debugf("quit"); _archive[_reads[hstream].harchive].last_error = ARC_SEQUENCE_BREAK; return (FALSE); } if (hdr->type == ET) _reads[hstream].in_seq = 0; else _reads[hstream].in_seq = ExpectedSeqNumber(hdr->seq); /* Is is a DT packet? */ if (hdr->type == DT) { /* Is it from an unmasked channel? */ if (_reads[hstream].criteria.chn_mask & (0x0001 << (hdr->channel - 1))) { time.earliest = hdr->time; time.latest = hdr->time + ((REAL64) hdr->length / (REAL64) _reads[hstream].rate); debugf("GNDP pkt times: %.3f:%.3f (%u:%.1f)...", time.earliest, time.latest, hdr->length, _reads[hstream].rate); /* Is latest time < earliest specified? */ if (!UndefinedTime(_reads[hstream].criteria.time.earliest) && time.latest < _reads[hstream].criteria.time.earliest) continue; /* Is earliest > latest specified */ if (!UndefinedTime(_reads[hstream].criteria.time.latest) && time.earliest > _reads[hstream].criteria.time.latest) { /* Clear channel flag */ _reads[hstream].channels &= ~(0x0001 << (hdr->channel - 1)); /* Are all channels done? */ if (_reads[hstream].channels == 0x0000) { _archive[_reads[hstream].harchive].last_error = ARC_END_OF_DATA; debugf("GNDP EOD"); return (FALSE); } continue; } debugf("GNDP good..."); /* Set the channel flag */ _reads[hstream].channels |= (0x0001 << (hdr->channel - 1)); /* Track time range of retrieved data */ UpdateTimeRange(&_reads[hstream].time, time.earliest); UpdateTimeRange(&_reads[hstream].time, time.latest); /* Fixup the outbound sequence number */ debugf("GNDP update pkt seq"); FixupSequenceNumber(hstream, packet, hdr->type); _reads[hstream].out_seq = ExpectedSeqNumber(_reads[hstream].out_seq); break; } } } return (TRUE); }
/*---------------------------------------------------------------------*/ BOOL CreateEventHeader(H_STREAM hstream, RF_PACKET * packet) { CHAR string[32]; UINT8 stream, channel; UINT16 i, unit, length; INT32 position; REAL64 time; RF_HEADER hdr; ASSERT(packet != NULL); if (!ValidateStreamHandle(hstream)) return (FALSE); _archive[_reads[hstream].harchive].last_error = ARC_NO_ERROR; /* Open the file if needed */ if (_reads[hstream].file == VOID_H_FILE) { position = VOID_INT32; if ((_reads[hstream].file = FileOpenForRead(_reads[hstream].event.filespec)) == VOID_H_FILE) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } } else { /* Otherwise, save position in file */ position = FilePosition(_reads[hstream].file); if (!FileRewind(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } } /* Read first packet */ if (!ReadPacket(_reads[hstream].file, &hdr, &_reads[hstream].eh)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* Is it an EH? */ if (hdr.type == EH) { /* Get the sampling rate */ _reads[hstream].rate = DecodeRFRate(&_reads[hstream].eh); if (!IsValidRate(_reads[hstream].rate)) { _archive[_reads[hstream].harchive].last_error = ARC_NO_RATE; return (FALSE); } debugf("CEH EHrate: %.1f...", _reads[hstream].rate); } else { /* No, read last packet */ if (!FileSeekEOF(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } _reads[hstream].in_seq = VOID_UINT16; if (!UnReadPacket(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } if (!ReadPacket(_reads[hstream].file, &hdr, &_reads[hstream].eh)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } if (!FileRewind(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* Is it an ET? */ if (hdr.type == ET) { /* Get the sampling rate */ _reads[hstream].rate = DecodeRFRate(&_reads[hstream].eh); if (!IsValidRate(_reads[hstream].rate)) { _archive[_reads[hstream].harchive].last_error = ARC_NO_RATE; return (FALSE); } debugf("CEH ETrate: %.1f...", _reads[hstream].rate); } else { /* Try to derive rate... */ unit = VOID_UINT8; while (TRUE) { if (!ReadPacket(_reads[hstream].file, &hdr, &_reads[hstream].eh)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } if (FileAtEOF()) { _archive[_reads[hstream].harchive].last_error = ARC_NO_RATE; return (FALSE); } if (hdr.type == DT && hdr.length > 0) { unit = hdr.unit; stream = hdr.stream; channel = hdr.channel; time = hdr.time; length = hdr.length; break; } } if (unit == VOID_UINT8) { _archive[_reads[hstream].harchive].last_error = ARC_NO_RATE; return (FALSE); } while (TRUE) { if (!ReadPacket(_reads[hstream].file, &hdr, &_reads[hstream].eh)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } if (FileAtEOF()) { _archive[_reads[hstream].harchive].last_error = ARC_NO_RATE; return (FALSE); } if (hdr.type == DT && hdr.unit == unit && hdr.stream == stream && hdr.channel == channel) { _reads[hstream].rate = (REAL32) (((REAL64) length / (hdr.time - time) * 10) + 0.5) / 10; if (!IsValidRate(_reads[hstream].rate)) { _archive[_reads[hstream].harchive].last_error = ARC_NO_RATE; return (FALSE); } if (!FileRewind(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } ArchiveLog(ARC_LOG_ERRORS, "Warning: sampling rate (%.1fsps) is derived DT times, EH/ET packets will be incomplete!", _reads[hstream].rate); /* Store rate in packet */ memset((UINT8 *) (&_reads[hstream].eh) + 24, ' ', sizeof(RF_PACKET) - 24); if (_reads[hstream].rate < 1) sprintf(string, "%-4.1f", _reads[hstream].rate); else sprintf(string, "%-4hu", (UINT16)_reads[hstream].rate); for (i = 0; i < 4; i++) { if (string[i]) _reads[hstream].eh.pac.eh.rate[i] = string[i]; } /* Set packet length so this won't be confused with 120 data */ _reads[hstream].eh.hdr.bpp[0] = 0x02; _reads[hstream].eh.hdr.bpp[1] = 0x88; break; } } } debugf("CEH derived: %.1f...", _reads[hstream].rate); } /* Restore position if it was saved above */ if (position != VOID_INT32) { if (!FileSeekAbsolute(_reads[hstream].file, position)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } } /* Find first data packet */ if (!GetNextDataPacket(hstream, &hdr, packet)) return (FALSE); /* Fixup an event header */ _reads[hstream].out_seq = 0; _reads[hstream].out_evn = hdr.evn_no; FixupHeaderTrailer(hstream, EH); /* Unget the packet */ _reads[hstream].in_seq = VOID_UINT16; if (!UnReadPacket(_reads[hstream].file)) { _archive[_reads[hstream].harchive].last_error = ARC_FILE_IO_ERROR; return (FALSE); } /* EH is in user buffer and we're ready to read the first data packet */ _reads[hstream].out_seq++; memcpy(packet, &_reads[hstream].eh, RF_PACKET_SIZE); return (TRUE); } /* end CreateEventHeader() */