// DXFReader.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <mmsystem.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
using namespace std;
struct Line{
char * originalP ;
double x1
,y1,z1,x2,y2,z2;
UINT orgSize; //线信息字节大小
UINT curSize; //线当前信息字节大小
};
void AdjustPos(char* istrBuffer,int & pos,char* tmp);
string ReadLine(istringstream& instr,char* istrBuffer,int & pos,char* &preLine,char* &curLine,int& rowIndex);
string TrimString(string tmpStr);
Line CreatLine( char*& istrBuffer, int & pos, istringstream& instr, char * &preLine, char * &curLine, int& rowIndex );
string swriter(Line &);
// 唯一的应用程序对象
CWinApp theApp;
void execute();
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
execute();
}
}
else
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: GetModuleHandle 失败\n"));
nRetCode = 1;
}
return nRetCode;
}
/// <summary> 创建执行程序. </summary>
///
/// <remarks> zhengzhe, 2011/11/9. </remarks>
void execute()
{
wcout.imbue(locale("chs"));
clock_t start,end;
start = clock();
const char* srcFile = "c:\\t3.dxf";
const char* desFile = "c:\\t3_1.dxf";
CFile m_curFile(_T(srcFile),CFile::modeRead);
UINT uFileSize = (UINT)m_curFile.GetLength();
char* istrBuffer = new char[uFileSize]; //输入缓冲区
char* ostrBuffer ; //输出缓冲区
vector<Line> vecLines;
m_curFile.Read(istrBuffer,uFileSize);
m_curFile.Close();
//string str(strBuffer);
//std::istringstream instr(str);
istringstream instr;
instr.str(istrBuffer);
int rowIndex = 0,pos = 0;
int num=0;
char *preLine=istrBuffer ,*curLine=istrBuffer;
while(!instr.eof())
{
//读取并返回一行数据
string strLine = ReadLine(instr,istrBuffer,pos,preLine,curLine,rowIndex);
//裁剪数据
string s = TrimString(strLine);
//开始读取实体数据
if (s=="ENTITIES")
{
do
{
s = TrimString(ReadLine(instr,istrBuffer,pos,preLine,curLine,rowIndex));
if (s=="LINE")
{
vecLines.push_back(CreatLine(istrBuffer, pos, instr, preLine, curLine, rowIndex));
}
} while (s!="ENDSEC");
}
if (s=="EOF")
break;
}
end = clock();
cout << "Vector Size is :" <<vecLines.size()<<endl;
int count = vecLines.size()>5?5:vecLines.size();
for (int i=0 ;i<count;i++)
{
int size = vecLines[i].orgSize;
char *p = new char[size+1];
strncpy(p,vecLines[i].originalP,size);
p[size]='\0';
cout <<"vec "<<i<<" \n\tsize is :" << size << "\n\tstart at " << &(vecLines[i].originalP) << "\n\twith original content:\n" << p <<endl;
}
cout << "Start changing...." <<endl;
//修改数据
Line & line1 = vecLines[0];
line1.x1=2941.815581111809;
line1.y1=1653.848673386335;
line1.z1=0.0;
line1.x2=5941.815581111809;
line1.y2=3653.848673386335;
line1.z2=0.0;;
//输出信息
string output = swriter(line1);
cout <<"After changed line has :\n"<<output.length()<<"bytes\n" << output <<endl;
//调整大小
line1.curSize = output.length();
//写文件
int mSize = (vecLines[0].originalP-istrBuffer)/sizeof(char); //未修改的字节大小
int oSize = uFileSize+vecLines[0].curSize-vecLines[0].orgSize; //计算输出缓存大小
ostrBuffer = new char[oSize];
memcpy(ostrBuffer,istrBuffer,mSize); //拷贝未修改部分
char* oPtr = ostrBuffer+sizeof(char)*mSize; //初始化输出
char* inPtr = istrBuffer+sizeof(char)*mSize; //初始化原数据
inPtr = inPtr+vecLines[0].orgSize*sizeof(char); //移动源数据指针
strcpy(oPtr,output.c_str()); //拷贝信息
oPtr = oPtr + sizeof(char)*(output.length()); //移动输出指针
memcpy(oPtr,inPtr,(istrBuffer+sizeof(char)*uFileSize-inPtr)/sizeof(char));
CFile m_outFile(_T(desFile),CFile::modeCreate|CFile::modeWrite);
m_outFile.Write(ostrBuffer,oSize);
cout << "Writeing complete..." <<endl;
cout << "Run time:" << (double)(end-start)/CLOCKS_PER_SEC <<"seconds " <<endl;
delete[] istrBuffer;
delete[] ostrBuffer;
}
string TrimString(string tmpStr)
{
char s[256]={0};
sscanf(tmpStr.c_str(),"%s",s); //读取字符串
return string(s);
}
string ReadLine(istringstream& instr,char* istrBuffer,int & pos,char* &preLine,char* &curLine,int& rowIndex)
{
char tmp[256]={0}; //设定每行大小256
instr.getline(tmp,256);
//调整Pos位置
AdjustPos(istrBuffer,pos,tmp);
//读取失败,则跳过,否则读取成功行数加1
if (!instr.fail())
rowIndex++; //行数加1 只有在成功读入一行
else{
for(;istrBuffer[pos]!='\n';pos++)
;
while(istrBuffer[pos]=='\n'||istrBuffer[pos]=='\r')
{
pos++;
}
instr.ignore('\n');
instr.clear();
}
//调整当前指针
if (rowIndex>1)
preLine = curLine;
curLine = &istrBuffer[pos];
return string(tmp);
}
void AdjustPos(char* istrBuffer,int & pos,char* tmp)
{
pos+=strlen(tmp);
while (istrBuffer[pos]=='\r'||istrBuffer[pos]=='\n')
pos++; //跳过\n\t
}
Line CreatLine( char*& istrBuffer, int & pos, istringstream& instr, char * &preLine, char * &curLine, int& rowIndex )
{
Line line ={NULL,0,0,0,0,0,0,0};
typedef enum dataType{X1=1,Y1,Z1,X2,Y2,Z2} DataType;
DataType dType = X1;
string s,strLine;
do{
strLine = ReadLine(instr,istrBuffer,pos,preLine,curLine,rowIndex);
s = TrimString(strLine);
}while(s!="10");
while(s!="0")
{
if (s=="10")
{
dType = X1;
//记录line的在buffer中的位置,注意需要减去10的位置
line.originalP=preLine;
}
if (s=="20")
dType = Y1;
if (s=="30")
dType = Z1;
if (s=="11")
dType=X2;
if(s=="21")
dType=Y2;
if(s=="31")
dType = Z2;
s = TrimString(ReadLine(instr,istrBuffer,pos,preLine,curLine,rowIndex));
switch(dType)
{
case X1: line.x1 = atof(s.c_str());break;
case Y1: line.y1 = atof(s.c_str()); break;
case Z1: line.z1 = atof(s.c_str()); break;
case X2: line.x2 = atof(s.c_str()); break;
case Y2: line.y2 = atof(s.c_str()); break;
case Z2: line.z2 = atof(s.c_str()); break;
default:;
}
s = TrimString(ReadLine(instr,istrBuffer,pos,preLine,curLine,rowIndex));
}
line.orgSize = (&istrBuffer[pos]-line.originalP)/sizeof(char); //字节大小
return line;
}
string swriter(Line& line)
{
CString s;
s.Format("10\r\n%f\r\n20\r\n%f\r\n30\r\n%f\r\n11\r\n%f\r\n21\r\n%f\r\n31\r\n%f\r\n0\r\n",line.x1,line.y1,line.z1,line.x2,line.y2,line.z2);
return s.GetString();
}
DXFReader
最新推荐文章于 2023-07-20 07:54:58 发布