//////////////////////////////////////////////////////////////// // Technomaint 2004 (c) SofTech di Germinara Francesco 2000,2004 // www.germinara.it info@germinara.it // Story: // //////////////////////////////////////////////////////////////// // Checked Version: 6/5/2004 //////////////////////////////////////////////////////////////// #include"stdafx.h" #include"CFGFile.h" //Standard constructor CFGFile::CFGFile(){ m_strFileName=""; m_bInFileOpen=FALSE; m_msgInfo=""; fpRec=NULL; } //Display Message Box Info void CFGFile::DisplayMsgInfo(CString strMsgInfo){ strMsgInfo.TrimRight(); AfxMessageBox(strMsgInfo); } void CFGFile::CloseFile(){ if(m_strFileName.Find(".XML")!=-1){ CloseXML(); } this->Close(); m_bInFileOpen=FALSE; } //Open the file BOOL CFGFile::OpenFile(CString strFileName,BOOL bForWrite){ m_strFileName=strFileName; if(bForWrite == TRUE){ if(!this->Open(m_strFileName,CFile::modeCreate | CFile::modeReadWrite,&ex)){ TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); m_msgInfo.Format("ERRORE: Imposibile scrivere %s",szError); DisplayMsgInfo(m_msgInfo); m_bInFileOpen=FALSE; }else{ m_bInFileOpen=TRUE; m_strFileName.MakeUpper(); if(m_strFileName.Find(".XML")!=-1){ WriteHeadXML(); } } }else{ if(!this->Open(m_strFileName,CFile::modeRead,&ex)){ TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); m_msgInfo.Format("ERRORE: Imposibile leggere %s",szError); DisplayMsgInfo(m_msgInfo); m_bInFileOpen=FALSE; }else{ m_bInFileOpen=TRUE; m_strFileName.MakeUpper(); if(m_strFileName.Find(".XML")!=-1){ ReadXML(-1); } } } return m_bInFileOpen; } //Write data to file void CFGFile::WriteData(int nRecord){ if(IsOpen()&& fpRec!=NULL){ if(nRecord != -1) this->Seek( GetRecordSize() * nRecord,CFile::begin); fpRec->WriteData(*this); } } int CFGFile::GetRecordSize(){ //Valid only for Fixed Record length return fpRec->GetRecordSize()+2; //newline CR+LF } //Read data from file int CFGFile::ReadData(int nRecord){ //Valid only for Fixed Record length int nFields=0; if(IsOpen()&& fpRec!=NULL){ if(nRecord != -1) this->Seek( GetRecordSize() * nRecord,CFile::begin); if(fpRec->ReadData(*this)==TRUE){ nFields=fpRec->GetCount(); } } return nFields; } //Read sequential data from file int CFGFile::ReadSeqData(int nRecord){ int nFields=0; if(IsOpen()&& fpRec!=NULL){ if(nRecord == -1){ this->Seek(0,CFile::begin); //Only Positioning at the beginning return 0; } if(fpRec->ReadData(*this)==TRUE){ nFields=fpRec->GetCount(); } } return nFields; } int CFGFile::GetRecordsNumber(){ int numOfRecord=0; if(m_strFileName.Find(".XML")!=-1) return m_nRecords; if(IsOpen()&& fpRec!=NULL){ long int fileLength=this->GetLength(); int recordLength=GetRecordSize(); if((recordLength > 0) && (fileLength>0)) numOfRecord = fileLength / recordLength; else numOfRecord=0; } return numOfRecord; } void CFGFile::WriteHeadXML(){ parserObj = new COXParserObject(); parserObj->SetText(_T("xml version=\"1.0\"")); parserObj->SetType( COXParserObject::PROCINSTR ); parser.Root()->AddObject( parserObj ); pFileRoot = parser.Root()->AddElement(_T("FGFile")); pFileRoot->AddAttribute(_T("Version"), _T("Versione 1.0.0 del 27/12/2002")); pFileRoot->AddAttribute(_T("Author"), _T("SofTech di Germinara Francesco")); pFileRoot->AddAttribute(_T("WebSite"), _T("http://www.germinara.it")); pDatiElement =pFileRoot->AddElement( "Data" ); } //Write Data in XML Format void CFGFile::WriteXML(int nRecord){ int nFields=0; if(IsOpen()){ CString msg; msg.Format("%0.8ld",nRecord); nFields=fpRec->GetCount(); COXParserElement *pRecordElement = pDatiElement->AddElement(_T("Records")); pRecordElement->AddAttribute(_T("Number"), _T(msg)); msg.Format("%d",nFields); pRecordElement->AddAttribute(_T("FieldsNumber"), _T(msg)); CString nStrFieldIndex; for( int nFieldIndex = 0; nFieldIndex < nFields; nFieldIndex ++ ) { nStrFieldIndex.Format("%d",nFieldIndex); COXParserElement *pCampoElement = pRecordElement->AddElement(_T("Fields")); pCampoElement->AddAttribute(_T("FieldName"), _T(fpRec->m_fld[nFieldIndex].GetName())); pCampoElement->AddAttribute(_T("FieldDescription"), _T(fpRec->m_fld[nFieldIndex].GetDescr())); pCampoElement->AddAttribute(_T("FieldIndex"), _T(nStrFieldIndex)); pCampoElement->AddAttribute(_T("FieldLength"), _T(fpRec->m_fld[nFieldIndex].StrGetLen())); pCampoElement->AddAttribute(_T("FieldDecimalPlace"), _T(fpRec->m_fld[nFieldIndex].StrGetDec())); pCampoElement->AddAttribute(_T("FieldStringValue"), _T(fpRec->m_fld[nFieldIndex].GetStringValue())); pCampoElement->AddAttribute(_T("FieldNumericValue"), _T(fpRec->m_fld[nFieldIndex].StrGetNumericValue())); //Row Data Value COXParserObject* pObject = new COXParserObject; pObject->SetText( "" + fpRec->m_fld[nFieldIndex].GetValue() + "" ); pObject->SetType( COXParserObject::RAWTEXT ); pCampoElement->AddObject( pObject ); } } } //Read Data in XML Format BOOL CFGFile::ReadXML(int nRecord){ CString objName; int nObjIndex=0,nRecordIndexMax=0; if(nRecord == -1) //Only read the number of records. readParser.ParseFile( this ); m_nRecords=0; COXParserElement *root = readParser.Root(); COXParserElement *pFileRoot = root->FindElement(_T("FGFile")); if ( pFileRoot == NULL ) return FALSE; nObjIndex =pFileRoot->NumObjects(); nRecordIndexMax=0; for ( int nIndex = 0; nIndex < nObjIndex; nIndex ++ ) { COXParserObject *parserObj = pFileRoot->Object( nIndex ); if ( pFileRoot->GetType() != COXParserObject::ELEMENT ) continue; objName = ((COXParserElement*) parserObj )->GetName(); if ( strcmp(objName, _T("Data")) == 0 ) { COXParserElement *pRecordsElement =((COXParserElement*) parserObj ); nRecordIndexMax =pRecordsElement->NumObjects(); m_nRecords = nRecordIndexMax; //save record number information if(nRecord == -1) //Only read the number of records. return TRUE; /* for ( int nIndexRecord = 0; nIndexRecord < nRecordIndexMax; nIndexRecord ++ ) { COXParserElement *pRecordElement = (COXParserElement*)(pRecordsElement->Object( nIndexRecord )); Loop all record */ COXParserElement *pRecordElement; if(nRecord < nRecordIndexMax) pRecordElement = (COXParserElement*)(pRecordsElement->Object( nRecord )); else return FALSE; int nFieldsCount = pRecordElement->NumObjects(); COXAttribute *attr = NULL; CString strNumeroRecord; attr = ((COXParserElement*) pRecordElement )->FindAttribute(_T("Number")); strNumeroRecord=attr->GetStringValue(); attr = ((COXParserElement*) pRecordElement )->FindAttribute(_T("FieldsNumber")); CString strFieldFromAttribute=attr->GetStringValue(); if(nFieldsCount != atoi(strFieldFromAttribute)) return FALSE; for ( int nIndexField = 0; nIndexField < nFieldsCount; nIndexField ++ ) { COXParserElement *pFieldElement = (COXParserElement*)(pRecordElement->Object( nIndexField )); if( pFieldElement == NULL ) continue; attr = ((COXParserElement*) pFieldElement )->FindAttribute(_T("FieldName")); CString fieldName=attr->GetStringValue(); attr = ((COXParserElement*) pFieldElement )->FindAttribute(_T("FieldStringValue")); CString fieldStringValue=attr->GetStringValue(); attr = ((COXParserElement*) pFieldElement )->FindAttribute(_T("FieldNumericValue")); CString fieldNumericValue=attr->GetStringValue(); //Read ROW Data CString fieldRowData; COXParserElement* pRowData = ((COXParserElement*) pFieldElement )->FindElement(_T("RowData")); if ( pRowData != NULL ) { CString dataType; COXAttribute *pDataType = pRowData->FindAttribute(_T("Type")); if ( pDataType != NULL ) dataType = pDataType->GetStringValue(); else dataType = _T("String"); for( int nDataIndex = 0; nDataIndex < pRowData->NumObjects(); nDataIndex ++ ){ COXParserObject *pObject = pRowData->Object( nDataIndex ); if( pObject->GetType() != COXParserObject::PLAINTEXT ) continue; fieldRowData=pObject->GetText(); if(dataType == _T("String")){ fpRec->m_fld[nIndexField].SetDataField(fieldStringValue); } if(dataType == _T("Number")){ fpRec->m_fld[nIndexField].SetDataField(atof(fieldNumericValue)); } } } } /* } Loop all records */ } } return TRUE; } void CFGFile::CloseXML(){ parser.WriteFile(this); } void CFGFile::AssignRecord(char *rowData){ if(fpRec != NULL){ fpRec->SetDirectBuffer(rowData); } } //Write Sequential data to file void CFGFile::WriteSeqData(){ if(IsOpen()&& fpRec!=NULL){ fpRec->WriteData(*this); } } //Open the file BOOL CFGFile::OpenFileAppend(CString strFileName,BOOL bForWrite){ m_strFileName=strFileName; if(bForWrite == TRUE){ if(!this->Open(m_strFileName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite,&ex)){ TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); m_msgInfo.Format("ERRORE: Imposibile scrivere %s",szError); DisplayMsgInfo(m_msgInfo); m_bInFileOpen=FALSE; }else{ SeekToEnd(); m_bInFileOpen=TRUE; m_strFileName.MakeUpper(); if(m_strFileName.Find(".XML")!=-1){ WriteHeadXML(); } } }else{ if(!this->Open(m_strFileName,CFile::modeRead,&ex)){ TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); m_msgInfo.Format("ERRORE: Imposibile leggere %s",szError); DisplayMsgInfo(m_msgInfo); m_bInFileOpen=FALSE; }else{ m_bInFileOpen=TRUE; m_strFileName.MakeUpper(); if(m_strFileName.Find(".XML")!=-1){ ReadXML(-1); } } } return m_bInFileOpen; }