コード例 #1
0
ファイル: fileopen.c プロジェクト: justid/Zoolu
int ReadFile( char *folder )
{
	char str[FILENAMEMAX];
	FONTCHARACTER find_path[50];
	FONTCHARACTER find_name[50];
	FILE_INFO file_info;
	int find_h;
	int size, i;

	int iError=0;
	int iFirst=0;

	size = 0;
	FilePath( folder, find_path);

	//	Get File count	
	do
	{
	  if(iFirst==0) {
        iError=Bfile_FindFirst(find_path,&find_h,find_name,&file_info);
        iFirst=1;
      } 
	  else {
        iError=Bfile_FindNext(find_h,find_name,&file_info);
      }
	  if( iError == 0 && ( file_info.type == DT_DIRECTORY ||  IsCsvFile( find_name ) ) )
		++size ;
	}
	while (iError == 0);
	Bfile_FindClose(find_h);
	//	Get File name	
	files = (Files *)calloc(1,size*FILENAMEMAX);
	i = 0;
	iError=0;
	iFirst=0;
	do
	{
	  if(iFirst==0) {
        iError=Bfile_FindFirst(find_path,&find_h,find_name,&file_info);
        iFirst=1;
      } 
	  else {
        iError=Bfile_FindNext(find_h,find_name,&file_info);
      }
	  if( iError == 0 && ( file_info.type == DT_DIRECTORY ||  IsCsvFile( find_name ) ) ){
			FontToChar(find_name,str);
			strncpy( files[i].filename, str, FILENAMEMAX);
			files[i].filesize = (file_info.type == DT_DIRECTORY ? -1 : file_info.dsize);
			++i;
		}
	} while (iError == 0);

	Bfile_FindClose(find_h);
	return size;
}
コード例 #2
0
const int CXCFileStream::WriteCsvData(LPCTSTR lpszFilename, const vector<list<string> > &vlStr)
{
	/// 1、判断是否是CSV文件
	if (! IsCsvFile(lpszFilename)) 
		return XC_ERR_INVALID_FILE_NAME;
	/// 2、打开CSV文件
	ofstream _streamToFile(lpszFilename);
	///    判断打开文件是否成功
	if (NULL == _streamToFile) 
		return (-errno);
	/// 本地变量
	static TCHAR chQuote = '"';
	static TCHAR chComma = ',';
	/// Loop through each list of string in vector 
	for (vector<list<string> >::const_iterator vIt = vlStr.begin(); vIt != vlStr.end(); vIt ++) {
		/// Loop through each string in list 
		for (list<string>::const_iterator lIt = vIt->begin(); lIt != vIt->end(); lIt ++) {
			/// Separate this value from previous
			if (vIt->begin() != lIt) 
				_streamToFile.put(chComma);
			/// 考虑string中可能有,或"的情况,这就要特殊包装。
			bool bComma = (lIt->find(chComma) != lIt->npos);
			bool bQuote = (lIt->find(chQuote) != lIt->npos);
			/// 真的含有,或"的情况
			if (bComma || bQuote) {
				_streamToFile.put(chQuote);


				if (bQuote) {
					for (string::const_iterator chIt = lIt->begin(); chIt != lIt->end(); chIt ++ ) {
						// Pairs of quotes interpreted as single quote
						if (chQuote == *chIt) 
							_streamToFile.put(chQuote);


						_streamToFile.put(*chIt);
					}
				}
				else 
					_streamToFile << *lIt;


				_streamToFile.put(chQuote);
			}
			else
				_streamToFile << *lIt;
		}
		/// 换行
		_streamToFile << endl;
	}
	/// 
	return XC_ERR_NONE;
}
コード例 #3
0
const int CXCFileStream::ReadCsvData(LPCTSTR lpszFilename, vector<list<string> > &vlStr)
{
	/// 1、判断是否是CSV文件
	if (! IsCsvFile(lpszFilename)) 
		return XC_ERR_INVALID_FILE_NAME;
	/// 2、打开CSV文件
	ifstream _streamFromFile(lpszFilename);
	///    判断打开文件是否成功
	if (NULL == _streamFromFile) 
		return (-errno);
	/// 存储读取的文件内容
	string _strIn("");
	/// 3、读取一行
	while (getline(_streamFromFile, _strIn)) {
		/// 每行的源字符串
		LPCTSTR _pcSrc = _strIn.c_str();
		/// 存储一行‘,'分隔解析后的各个元素
		list<string> _ltStr;
		/// Parse values in this line
		while (*_pcSrc != '\0') {
			/// string to hold this value
			string _strElem(""); 
			/// 针对每个字符分析
			if (*_pcSrc == '"') {
				/// Bump past opening quote
				_pcSrc++;
				/// Parse quoted value
				while (*_pcSrc != '\0') {
					/// Test for quote character
					if (*_pcSrc == '"') {
						/// Found one quote
						_pcSrc++;
						// If pair of quotes, keep one
						// Else interpret as end of value
						if (*_pcSrc != '"') {
							_pcSrc++;
							break;
						}
					}
					/// Add this character to value
					_strElem.push_back(*_pcSrc++);
				}
			}
			else {
				// Parse unquoted value
				while (*_pcSrc != '\0' && *_pcSrc != ',') 
					_strElem.push_back(*_pcSrc++);
				// Advance to next character (if not already end of string)
				if (*_pcSrc != '\0')
					_pcSrc++;
			}
			/// Add this string to container
			_ltStr.push_back(_strElem);
		}
		/// 分析后的一行文件内容所得的元素列表添加到容器中
		vlStr.push_back(_ltStr);
		/// 归零,防止下次分析旧的数据。
		_strIn.assign("");
	}


	return XC_ERR_NONE;
}