读文件
spAttriStyleInfo AttributeFile::ReadPointAttrFile(const string& filename)
{
spAttriStyleInfo spVO(new AttriStyleInfo());
ifstream ifs;
/*ifs.exceptions ( ifstream::eofbit | ifstream::badbit | ifstream::failbit );*/
try {
ifs.open(filename.c_str());
if (!ifs.is_open()) {
throw ArdError(ArdError::FILE_OPEN_FAILURE);
}
typedef istream_iterator<string> string_input;
vector<string> dictionary;
copy(string_input(ifs), string_input(), back_inserter(dictionary));
vector<string>::iterator iter;
iter = std::find(dictionary.begin(), dictionary.end(), "[POINT]");
string strCmt = "";
while (iter != dictionary.end())
{
string strID = (++iter)->substr(3);
unsigned char cPrepresentId = atoi((++iter)->substr(6).c_str());
int iSize = atoi((++iter)->substr(5).c_str());
BYTE RValue = atoi((++iter)->substr(4).c_str());
BYTE GValue = atoi((++iter)->substr(6).c_str());
BYTE BValue = atoi((++iter)->substr(5).c_str());
//Gdiplus::Color color = RGB(RValue, GValue, BValue);
if(!(++iter)->empty())
strCmt = (iter)->substr(8);
AttriStyleDef pointAttr(strID, Gdiplus::Color(RValue, GValue, BValue), cPrepresentId, strCmt, iSize);
iter = find(iter, dictionary.end(), "[POINT]");
spVO->addPointAttriStyle(pointAttr);
}
}
catch (ios::failure e)
{
ios_base::iostate s = ifs.rdstate();
if ( s & ifstream::eofbit )
{
throw ArdError(ArdError::FILE_READ_FAILURE, &e);
}
if ( s & ifstream::badbit )
{
throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
}
if ( s & ifstream::failbit )
{
throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
}
}
if (ifs.is_open())
{
ifs.close();
}
return spVO;
}
写文件
/**
* write point AR/D attributes to .att file
*/
void AttributeFile::WritePointAttrFile(const string& filename, spAttriStyleInfo attributes)
{
ofstream outFile;
outFile.exceptions ( ifstream::eofbit | ifstream::badbit | ifstream::failbit );
try{
outFile.open(filename.c_str());
if (!outFile.is_open())
{
throw ArdError(ArdError::FILE_OPEN_FAILURE);
}
map<std::string, AttriStyleDef> pointAttributes = attributes->GetPointMap();
map<std::string, AttriStyleDef>::iterator iterPoint;
for (iterPoint = pointAttributes.begin(); iterPoint != pointAttributes.end(); ++iterPoint)
{
AttriStyleDef spvo = iterPoint->second;
string iID = spvo.GetId();
unsigned int cPrepresentId = spvo.GetRepresentId();
int iSize = spvo.GetSize();
int RValue = spvo.GetColor().GetR();
int GValue = spvo.GetColor().GetG();
int BValue = spvo.GetColor().GetB();
string strCmt = spvo.GetCmt();
outFile << "[POINT]" <<'/n'<< "ID=" << iID << '/n' <<
"STYLE=" << cPrepresentId << '/n' << "SIZE=" << iSize << '/n' <<
"Red=" << RValue << '/n' << "Green=" << GValue << '/n' <<
"Blue=" << BValue << '/n' << "COMMENT=" << strCmt << endl;
}
}
catch (ios::failure e)
{
ios_base::iostate s = outFile.rdstate();
if ( s & ifstream::eofbit )
{
throw ArdError(ArdError::FILE_READ_FAILURE, &e);
}
if ( s & ifstream::badbit )
{
throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
}
if ( s & ifstream::failbit )
{
throw ArdError(ArdError::FILE_FORMAT_ERROR, &e);
}
}
if (outFile.is_open())
{
outFile.close();
}
}