int ext2::Read(FileDescriptorBase *fileDescriptor, void *buff, uint numBytes) { if(numBytes == 0) return(0); FileDescriptor *tmpDescriptor = reinterpret_cast<FileDescriptor*>(fileDescriptor); uint bytesToRead = MIN(tmpDescriptor->fileInode.size - tmpDescriptor->filePosition, numBytes); uint bytesRead = 0; int amt; // this could be faster by reading directly into buff and saving a mem copy... but easier to code :-) while(bytesToRead > bytesRead) { amt = MIN(blockSize - tmpDescriptor->blockPosition, bytesToRead - bytesRead); // copy over the data MemCopy(reinterpret_cast<uchar*>(buff) + bytesRead, tmpDescriptor->blockData + tmpDescriptor->blockPosition, amt); tmpDescriptor->blockPosition += amt; tmpDescriptor->filePosition += amt; // check if we're done with this block and need to read in the next one if(tmpDescriptor->blockPosition == blockSize && tmpDescriptor->filePosition < tmpDescriptor->fileInode.size) { ReadDataBlock(++tmpDescriptor->blockNumber, tmpDescriptor->fileInode.blockPointers, tmpDescriptor->blockData); tmpDescriptor->blockPosition = 0; } bytesRead += amt; } return(bytesRead); }
//Read selected object. int CGXCommunication::Read(CGXDLMSObject* pObject, int attributeIndex, std::string& value) { value.clear(); int ret; std::vector<CGXByteBuffer> data; CGXReplyData reply; //Read data from the meter. if ((ret = m_Parser->Read(pObject, attributeIndex, data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0 || (ret = m_Parser->UpdateValue(*pObject, attributeIndex, reply.GetValue())) != 0) { return ret; } //Update data type. DLMS_DATA_TYPE type; if ((ret = pObject->GetDataType(attributeIndex, type)) != 0) { return ret; } if (type == DLMS_DATA_TYPE_NONE) { type = reply.GetValue().vt; if ((ret = pObject->SetDataType(attributeIndex, type)) != 0) { return ret; } } //Get read value as string. //Note! This is for example. It's faster if you handle read COSEM object directly. std::vector<std::string> values; pObject->GetValues(values); value = values[attributeIndex - 1]; return DLMS_ERROR_CODE_OK; }
//Close connection to the meter. int CGXCommunication::Close() { int ret; std::vector<CGXByteBuffer> data; CGXReplyData reply; if ((ret = m_Parser->ReleaseRequest(data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0) { //Show error but continue close. } if ((ret = m_Parser->DisconnectRequest(data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0) { //Show error but continue close. } return 0; }
//Initialize connection to the meter. int CGXCommunication::InitializeConnection() { if (m_Trace > GX_TRACE_LEVEL_WARNING) { printf("InitializeConnection\r\n"); } std::vector<CGXByteBuffer> data; CGXReplyData reply; int ret = 0; //Get meter's send and receive buffers size. if ((ret = m_Parser->SNRMRequest(data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0 || (ret = m_Parser->ParseUAResponse(reply.GetData())) != 0) { printf("SNRMRequest failed %d.\r\n", ret); return ret; } reply.Clear(); if ((ret = m_Parser->AARQRequest(data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0 || (ret = m_Parser->ParseAAREResponse(reply.GetData())) != 0) { if (ret == DLMS_ERROR_CODE_APPLICATION_CONTEXT_NAME_NOT_SUPPORTED) { printf("Use Logical Name referencing is wrong. Change it!\r\n"); return ret; } printf("AARQRequest failed %d.\r\n", ret); return ret; } reply.Clear(); // Get challenge Is HLS authentication is used. if (m_Parser->IsAuthenticationRequired()) { if ((ret = m_Parser->GetApplicationAssociationRequest(data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0 || (ret = m_Parser->ParseApplicationAssociationResponse(reply.GetData())) != 0) { return ret; } } return DLMS_ERROR_CODE_OK; }
int CGXCommunication::Method(CGXDLMSObject* pObject, int attributeIndex, CGXDLMSVariant& value) { int ret; std::vector<CGXByteBuffer> data; CGXReplyData reply; //Get meter's send and receive buffers size. if ((ret = m_Parser->Method(pObject, attributeIndex, value, data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0) { return ret; } return DLMS_ERROR_CODE_OK; }
DataBlock* File::readFromFile(std::string name) { readOrWrite = 0; fileName = name; //Get the directory int lastSlash = fileName.find_first_of("/"); if(lastSlash != std::string::npos) { fileDirectory = fileName.substr(0, lastSlash+1); } fileStream.open(fileName.c_str(), std::fstream::in);//Tell we need to open to read if(fileStream.is_open()) //check if file opened { std::string word; std::string block; int maxSize = block.max_size(); DataBlock* datablock = new DataBlock(); //Add: getline - first line = header? while(std::getline(fileStream, word))//get all data to remove whitespace { //Do a check to determine if comments exist and get rid of them. word = omitComments(word); //Check for spaces except on values and get rid of them word = omitWhitespace(word); //Append word to blocks if((int)(block.size()+word.size()) > maxSize) { Logger()<<"STRING IS FULL - HOW?!?!!"<<std::endl; break; } block += word; } fileStream.close(); if(ReadDataBlock(block, datablock))//Read the block string (no whitespace or comments) in datablock { success = true; return datablock; } delete datablock; } else { success = false; Logger()<<"Could not open file to read from: "<<fileName<<std::endl; return nullptr; } success = false; Logger()<<"Something went wrong when attempting to read from file: "<<fileName<<std::endl; return nullptr; }
int ext2::Open(FileDescriptorBase *fileDescriptor, const string &file, const int flags) { // FIX ME (void)flags; FileDescriptor *fd = reinterpret_cast<FileDescriptor*>(fileDescriptor); int inode = FindInodeByPath(string(file)); if(inode < 0) { printf("COULDN'T FIND INODE\n"); return(-1); } fd->inodeNumber = inode; // set the inode number ReadInode(inode, &fd->fileInode); // read in the inode /* printf("SIZE: %d\n", fd->fileInode.size); printf("OWNER ID: %d\n", fd->fileInode.ownerUID); printf("FILE MODE: %d\n", fd->fileInode.fileMode); printf("BLOCK COUNT: %d\n", fd->fileInode.blockCount); printf("FILE FLAGS: %d\n", fd->fileInode.fileFlags); */ if(!(fd->fileInode.fileMode & FILE_FILE_MODE)) { // Panic::PrintMessage("Not a file\n"); printf("Not a file\n"); return(-2); } // read in the first data block if(fd->fileInode.size > 0) { fd->blockData = new uchar[blockSize]; // make the memory // Read in the first data block ReadDataBlock(0, fd->fileInode.blockPointers, fd->blockData); } else fd->blockData = NULL; // zero the block number and positions fd->blockNumber = fd->filePosition = fd->blockPosition = 0; return(0); }
int ext2::Write(FileDescriptorBase *fileDescriptor, void *buff, uint numBytes) { if(numBytes == 0) return(0); FileDescriptor *tmpDescriptor = reinterpret_cast<FileDescriptor*>(fileDescriptor); uint bytesWritten = 0; int amt; while(numBytes > bytesWritten) { amt = MIN(blockSize - tmpDescriptor->blockPosition, numBytes - bytesWritten); // copy in the current block MemCopy(tmpDescriptor->blockData + tmpDescriptor->blockPosition, reinterpret_cast<uchar*>(buff) + bytesWritten, amt); // write this block to the disk WriteDataBlock(tmpDescriptor->blockNumber, tmpDescriptor, tmpDescriptor->blockData); // update the positions tmpDescriptor->blockPosition += amt; tmpDescriptor->filePosition += amt; if(tmpDescriptor->filePosition > tmpDescriptor->fileInode.size) tmpDescriptor->fileInode.size = tmpDescriptor->filePosition; // we're done with this block and their's another to read in if(tmpDescriptor->blockPosition == blockSize && tmpDescriptor->filePosition < tmpDescriptor->fileInode.size) { ReadDataBlock(++tmpDescriptor->blockNumber, tmpDescriptor->fileInode.blockPointers, tmpDescriptor->blockData); tmpDescriptor->blockPosition = 0; } // we're at the end of the block and the file else if(tmpDescriptor->blockPosition == blockSize && tmpDescriptor->filePosition == tmpDescriptor->fileInode.size) { MemSet(tmpDescriptor->blockData, 0, blockSize); // set the block to all zeros tmpDescriptor->blockPosition = 0; // reset the block position ++tmpDescriptor->blockNumber; // increase the block number } bytesWritten += amt; } return bytesWritten; }
//Get Association view. int CGXCommunication::GetAssociationView() { if (m_Trace > GX_TRACE_LEVEL_WARNING) { printf("GetAssociationView\r\n"); } int ret; std::vector<CGXByteBuffer> data; CGXReplyData reply; if ((ret = m_Parser->GetObjectsRequest(data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0 || (ret = m_Parser->ParseObjects(reply.GetData(), true)) != 0) { printf("GetObjects failed %d.\r\n", ret); return ret; } return DLMS_ERROR_CODE_OK; }
int ext2::Seek(FileDescriptorBase *fileDescriptor, int offset, int whence) { if(whence == SEEK_SET && offset < 0) return(-2); if(whence == SEEK_END && offset > 0) return(-3); FileDescriptor *tmpFd = reinterpret_cast<FileDescriptor*>(fileDescriptor); int fileSize = tmpFd->fileInode.size; // based on the whence, calculate the new file position switch(whence) { case SEEK_SET: // we know that offset is positive tmpFd->filePosition = MIN(offset, fileSize); break; case SEEK_CUR: // calculate the new file position tmpFd->filePosition = MIN(int(tmpFd->filePosition + offset), fileSize) < 0 ? \ 0 : MIN(int(tmpFd->filePosition + offset), fileSize); break; case SEEK_END: // we know offset is negative or zero tmpFd->filePosition = fileSize + offset < 0 ? 0 : fileSize + offset; break; default: return(-4); } // based off the file position, calculate everything else tmpFd->blockPosition = tmpFd->filePosition % blockSize; tmpFd->blockNumber = tmpFd->filePosition / blockSize; // bring in the right data block (this could be a wasted call... oh well) ReadDataBlock(tmpFd->blockNumber, tmpFd->fileInode.blockPointers, tmpFd->blockData); return(tmpFd->filePosition); }
int CGXCommunication::ReadList( std::vector<std::pair<CGXDLMSObject*, unsigned char> >& list) { int ret; CGXReplyData reply; std::vector<CGXByteBuffer> data; //Get values from the meter. if ((ret = m_Parser->ReadList(list, data)) != 0) { return ret; } std::vector<CGXDLMSVariant> values; for (std::vector<CGXByteBuffer>::iterator it = data.begin(); it != data.end(); ++it) { if ((ret = ReadDataBlock(*it, reply)) != 0) { return ret; } if (list.size() != 1 && reply.GetValue().vt == DLMS_DATA_TYPE_ARRAY) { values.insert(values.end(), reply.GetValue().Arr.begin(), reply.GetValue().Arr.end()); } else if (reply.GetValue().vt != DLMS_DATA_TYPE_NONE) { // Value is null if data is send multiple frames. values.push_back(reply.GetValue()); } reply.Clear(); } if (values.size() != list.size()) { //Invalid reply. Read items count do not match. return DLMS_ERROR_CODE_INVALID_PARAMETER; } return m_Parser->UpdateValues(list, values); }
int CGXCommunication::ReadRowsByEntry( CGXDLMSProfileGeneric* pObject, unsigned int index, unsigned int count, CGXDLMSVariant& rows) { rows.Clear(); int ret; std::vector<CGXByteBuffer> data; CGXReplyData reply; if ((ret = m_Parser->ReadRowsByEntry(pObject, index, count, data)) != 0 || (ret = ReadDataBlock(data, reply)) != 0 || (ret = m_Parser->UpdateValue(*pObject, 2, reply.GetValue())) != 0) { return ret; } //Get rows value as string. //Note! This is for example. It's faster if you handle read COSEM object directly. std::vector<std::string> values; pObject->GetValues(values); rows = values[2 - 1]; return DLMS_ERROR_CODE_OK; }
void CIszImageStream::SyncCache() { uint64 currentSector = (m_position / m_header.sectorSize); uint64 neededBlock = (currentSector * m_header.sectorSize) / m_header.blockSize; if(neededBlock == m_cachedBlockNumber) { return; } if(neededBlock >= m_header.blockNumber) { throw std::runtime_error("Trying to read past eof."); } const BLOCKDESCRIPTOR& blockDescriptor = SeekToBlock(neededBlock); memset(m_cachedBlock, 0, m_header.blockSize); switch(blockDescriptor.storageType) { case ADI_ZERO: ReadZeroBlock(blockDescriptor.size); break; case ADI_DATA: ReadDataBlock(blockDescriptor.size); break; case ADI_ZLIB: ReadGzipBlock(blockDescriptor.size); break; #ifdef HAVE_BZIP case ADI_BZ2: ReadBz2Block(blockDescriptor.size); break; #endif default: throw std::runtime_error("Unsupported block storage mode."); break; } m_cachedBlockNumber = neededBlock; }
ExtFile *Partition::ReadDirectory(EXT2DIRENT *dirent) { string filename; ExtFile *newEntry; char *pos; int ret; if(!dirent) return NULL; if(!dirent->dirbuf) { dirent->dirbuf = (EXT2_DIR_ENTRY *) new char[blocksize]; if(!dirent->dirbuf) return NULL; ret = ReadDataBlock(&dirent->parent->inode, dirent->next_block, dirent->dirbuf); if(ret < 0) return NULL; dirent->next_block++; } again: if(!dirent->next) dirent->next = dirent->dirbuf; else { pos = (char *) dirent->next; dirent->next = (EXT2_DIR_ENTRY *)(pos + dirent->next->rec_len); if(IS_BUFFER_END(dirent->next, dirent->dirbuf, blocksize)) { dirent->next = NULL; if(dirent->read_bytes < dirent->parent->file_size) { ret = ReadDataBlock(&dirent->parent->inode, dirent->next_block, dirent->dirbuf); if(ret < 0) return NULL; dirent->next_block++; goto again; } return NULL; } } dirent->read_bytes += dirent->next->rec_len; filename.assign(dirent->next->name, dirent->next->name_len); if((filename.compare(".") == 0) || (filename.compare("..") == 0)) goto again; newEntry = ReadInode(dirent->next->inode); if(!newEntry) { LOG("Помилка читання Inode %d батьківський inode %d.\n", dirent->next->inode, dirent->parent->inode_num); return NULL; } newEntry->file_type = dirent->next->filetype; newEntry->file_name = filename; return newEntry; }
bool Excellon::Read( const char *p_szFileName, const bool force_mirror /* = false */ ) { printf("Excellon::Read(%s)\n", p_szFileName ); if (force_mirror) { m_mirror_image_x_axis = true; } // First read in existing PointType object locations so that we don't duplicate points. for (HeeksObj *obj = heeksCAD->GetFirstObject(); obj != NULL; obj = heeksCAD->GetNextObject() ) { if (obj->GetType() != PointType) continue; double pos[3]; obj->GetStartPoint( pos ); m_existing_points.insert( std::make_pair( CNCPoint( pos ), CDrilling::Symbol_t( PointType, obj->m_id ) ) ); } // End for std::ifstream input( p_szFileName, std::ios::in ); if (input.is_open()) { m_current_line = 0; while (input.good()) { char memblock[512]; memset( memblock, '\0', sizeof(memblock) ); input.getline( memblock, sizeof(memblock)-1 ); if (memblock[0] != '\0') { if (! ReadDataBlock( memblock )) return(false); } // End if - then } // End while // Now go through and add the drilling cycles for each different tool. std::set< CTool::ToolNumber_t > tool_numbers; for (Holes_t::const_iterator l_itHole = m_holes.begin(); l_itHole != m_holes.end(); l_itHole++) { tool_numbers.insert( l_itHole->first ); } // End for for (std::set<CTool::ToolNumber_t>::const_iterator l_itToolNumber = tool_numbers.begin(); l_itToolNumber != tool_numbers.end(); l_itToolNumber++) { double depth = 2.5; // mm CDrilling *new_object = new CDrilling( m_holes[ *l_itToolNumber ], *l_itToolNumber, depth ); new_object->m_speed_op_params.m_spindle_speed = m_spindle_speed; new_object->m_speed_op_params.m_vertical_feed_rate = m_feed_rate; new_object->m_params.m_peck_depth = 0.0; // Don't peck for a Printed Circuit Board. new_object->m_params.m_dwell = 0.0; // Don't wait around to clear stringers either. new_object->m_params.m_standoff = 2.0; // Printed Circuit Boards a quite flat theApp.m_program->Operations()->Add(new_object,NULL); } // End for return(true); // Success } // End if - then else { // Couldn't read file. printf("Could not open '%s' for reading\n", p_szFileName ); return(false); } // End if - else } // End Read() method
int ext2::FindByNameInInode(ulong inode, string name) { Inode theInode; ushort curSize; DirEntry *tmpDir; uchar *buff = new uchar[blockSize]; int ret = -1; ReadInode(inode, &theInode); // read in the inode // printf("INODE SIZE: %d\n", theInode.size); // printf("INODE BLOCK COUNT: %d\n", theInode.blockCount); // // TODO: Need to change when this stops... // for(int i=0; i < NUM_BLOCK_PTRS; ++i) { if(theInode.blockPointers[i] == 0) continue; // read in the data block if(ReadDataBlock(i, theInode.blockPointers, buff) < 0) { printf("Error reading data block: %d\n", i); return(-1); } curSize = 0; while(curSize < blockSize) { tmpDir = reinterpret_cast<DirEntry *>(&buff[curSize]); if(tmpDir->inode != 0) { // printf("NAME: %s\n", tmpDir->name); // printf("INODE: %d\n", tmpDir->inode); // printf("TYPE: %d\n", tmpDir->fileType); // we found the dir we were looking for if(name == string(tmpDir->name, tmpDir->nameLength)) { // found what we're looking for, cleanup and return int ret = tmpDir->inode; delete [] buff; return(ret); // return the inode } } curSize += tmpDir->recordLength; } } delete [] buff; // free up our memory return(ret); }