⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileimport.c

📁 图像处理的压缩算法
💻 C
📖 第 1 页 / 共 3 页
字号:
			int i;
			string strName, strValue;
			
			for( int iLine = 0; iLine < saLines.GetSize(); iLine++ )
			{
				i = saLines[iLine].Find(iSeparator);
				if( i > 0 )
				{
					strName = saLines[iLine].Left(i);
					strName.TrimLeft();
					strName.TrimRight();
					strName.MakeValidCName('_');
					if( !strName.IsEmpty() )
					{
						strValue = saLines[iLine].Mid(i + 1);
						strValue.TrimLeft();
						strValue.TrimRight();
						
						saNames.Add(strName);
						saValues.Add(strValue);
					}
				}
			}
		}
		*/
		get_ascii_file_header_variables(saNames, saValues, lpcszFile, trFilter);
		/// end IMPROVE_HDR_VAR_SCAN
	}
	else // FILTER_TYPE_BINARY
	{
		fuGetHeaderParamNames(trFilter, saNames);

		file fil;
		if( fil.Open(lpcszFile, file::modeRead|file::typeBinary) )
		{
			BINIMP binimp;
			fuGetBINIMP(trFilter, binimp);
			
			int nType, nOffset, nSize;
			for( int n = 0; n < saNames.GetSize(); n++ )
			{
				if( !fuGetBinHeaderParam(trFilter, saNames[n], nType, nOffset, nSize) )
					continue; // failed to get param
				if( ReadBinaryHeaderParam(str, fil, nType, nOffset, nSize, !binimp.iBigEndian) )
					saValues.Add(str);
			}
			fil.Close();
		}
	}
	return 0;
}

//--------------------------------------------------------------------------
/// EJP 07-28-2003 v7.0637 QA70-4911 IMPROVE_HDR_VAR_SCAN
static void check_and_add_header_variable(StringArray &saNames, StringArray &saValues, LPCSTR lpcszName, LPCSTR lpcszValue)
{
	string strName(lpcszName);
	strName.TrimLeft();
	strName.TrimRight();
	strName.MakeValidCName('_');
	if( !strName.IsEmpty() )
	{
		string strValue(lpcszValue);
		strValue.TrimLeft();
		strValue.TrimRight();

		saNames.Add(strName);
		saValues.Add(strValue);
	}
}

static void get_tokens_ignoring_quotes(StringArray &saTokens, LPCSTR lpcszTokens, int iSeparator)
{
	string strToken, strTokens = lpcszTokens;

	int i = strTokens.Find(iSeparator);
	while( i != -1 )
	{
		saTokens.Add(strTokens.Left(i)); // add everything before the separator into the string array
		strTokens.Delete(0, i + 1); // delete the separator and everything before it
		i = strTokens.Find(iSeparator);
	}
	saTokens.Add(strTokens);
}

void get_ascii_file_header_variables(StringArray &saNames, StringArray &saValues, LPCSTR lpcszFile, TreeNode &trFilter)
{
	int iFirstLine, iLastLine, iSeparator;
	if( !fuGetHdrVarScan(trFilter, iFirstLine, iLastLine, iSeparator) )
		return;
	
	StringArray saLines;
	ReadFileLines(saLines, lpcszFile, iLastLine - iFirstLine + 1, iFirstLine);

	int i, iLine;

	// First loop assumes each line is formatted as: <name> <separator> <value>
	for( iLine = 0; iLine < saLines.GetSize(); iLine++ )
	{
		i = saLines[iLine].Find(iSeparator);
		if( i > 0 )
			check_and_add_header_variable(saNames, saValues, saLines[iLine].Left(i), saLines[iLine].Mid(i + 1));
	}

	// Second loop assumes each line is formatted as: <value1> <separator> <value2> <separator> ... <valueN>
	string strName;
	StringArray saTokens;
	for( iLine = 0; iLine < saLines.GetSize(); iLine++ )
	{
		try
		{
			saLines[iLine].GetTokens(saTokens, iSeparator);
		}
		catch(int nErr)
		{
			///printf("Runtime error %d calling string::GetTokens while scanning ASCII header line %d.\n", nErr, iLine + 1);
			///continue;
			saTokens.SetSize(0);
			get_tokens_ignoring_quotes(saTokens, saLines[iLine], iSeparator);
		}
		for( int iToken = 0; iToken < saTokens.GetSize(); iToken++ )
		{
			strName.Format("L%dV%d", iFirstLine + iLine + 1, iToken + 1);
			check_and_add_header_variable(saNames, saValues, strName, saTokens[iToken]);
		}
	}
}
/// end IMPROVE_HDR_VAR_SCAN

//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
int ImportASCII(Page &pgTarget, TreeNode &trFilter, LPCSTR lpcszFile, int nFile)
{
	if( trFilter.Type.nVal != FILTER_TYPE_ASCII )
		return 1;

	string str = pgTarget.GetName();
	Worksheet wks(str);
	if( !wks )
		return 1;
	
	ASCIMP ascimp;
	fuGetASCIMP(trFilter, ascimp);

	// An internal bug can appear when partial import is off and user specifies
	// a number of columns that differs from the number auto-detected.
	// A fix is to do a partial import but set the partial import settings
	// to import all the data.
	if( ascimp.iPartial == 0 )
	{
		ascimp.iPartial=1;
		ascimp.iPartialC1 = 0;
		ascimp.iPartialC2 = ascimp.iNumColumns - 1;
		ascimp.iPartialR1 = 0;
		ascimp.iPartialR2 = -1;
	}

	int nErr = wks.ImportASCII(lpcszFile, ascimp);
	if( nErr )
		return nErr;

	/// EJP 11-12-2003 v7.5753 QA70-5211.11 FIX_SETTING_OF_DESIG_AND_FORMAT
	/* This is being done in ImportFile which knows about appending new cols
	str = fuGetDesignations(trFilter);
	if( !str.IsEmpty() )
		wks.SetColDesignations(str, fuGetRepetitive(trFilter));
	str = fuGetFormats(trFilter);
	if( !str.IsEmpty() )
		wks.SetColFormats(str, fuGetRepetitive(trFilter));
	*/
	/// end FIX_SETTING_OF_DESIG_AND_FORMAT

	return 0; // success
}

//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
int ImportBinary(Page &pgTarget, TreeNode &trFilter, LPCSTR lpcszFile, int nFile)
{
	if( trFilter.Type.nVal != FILTER_TYPE_BINARY )
		return 1;

	string str = pgTarget.GetName();
	Worksheet wks(str);
	if( !wks )
		return 1;
	
	BINIMP binimp;
	fuGetBINIMP(trFilter, binimp);

	int nErr = wuImportBinary(wks, lpcszFile, &binimp);
	if( nErr )
		return nErr;
	
	/// EJP 11-12-2003 v7.5753 QA70-5211.11 FIX_SETTING_OF_DESIG_AND_FORMAT
	/* This is being done in ImportFile which knows about appending new cols
	str = fuGetDesignations(trFilter);
	if( !str.IsEmpty() )
		wks.SetColDesignations(str, fuGetRepetitive(trFilter));
	str = fuGetFormats(trFilter);
	if( !str.IsEmpty() )
		wks.SetColFormats(str, fuGetRepetitive(trFilter));
	*/
	/// end FIX_SETTING_OF_DESIG_AND_FORMAT

	return 0; // success
}

#ifdef _OPERATION_H_WAIT_FIX
bool BatchProcessing()
{
	PageBase pbActive;
	pbActive = Project.Pages();
	if( !pbActive || EXIST_WKS != pbActive.GetType() )
		return false;

	if( false ) // if wks is not setup for batch processing
	{
		// these strings are just temporary.  they will be localized later when checking for batch processing is implemented
		MessageBox(GetWindow(), "The current worksheet is not setup for Batch Processing.", "Batch Processing");
		return false;
	}

	string strPgName = pbActive.GetName();
	Page pgTarget(strPgName);
	if( !pgTarget )
		return false;
	
	FDLogInit();

	// I'm doing all the FDlog stuff here because I need control that I can
	// not get with the functions in sys_utils.
	
	using fdlog = LabTalk.FDlog;

	fdlog.NumTypes = 0; // remove all file types
	fdlog.UseGroup("ASCII");
	fdlog.AddUserTypes(); // add file types from filters
	
	fdlog.MultiOpen.ColView = 12; // 4(file size) + 8(date)
	fdlog.MultiOpen.Sort = 0; // 0=standard sorting, 1=group sorting
	fdlog.MultiOpen.ComboName$ = "Import Filter";

	// Get filters from Origin and UserFiles folders
	StringArray saFilterFiles;
	fuGetFilterFiles(saFilterFiles);

	// Get a list of filters for displaying to users
	StringArray saFilterList;
	fuGetFilterList(saFilterList, saFilterFiles);

	// Put the display list in the file dialog's combobox
	string str;
	str.SetTokens(saFilterList, '|');
	fdlog.MultiOpen.ComboList$ = str;

	// Prompt the user with a file dialog
	if( NANUM == fdlog.MultiOpen() )
		return false; // no files selected
	
	int i = fdlog.MultiOpen.ComboSel - 1; // -1 for LabTalk's 1-based index
	Tree trFilter;
	if( !trFilter.Load(saFilterFiles[i]) )
		return false; // load filter error
	pgTarget.Info.System.Import.Filter$ = saFilterFiles[i];

	Project.Operations.SetBatchProcessing();

	char sz[MAXFULLPATH];
	for( i = 0; i < fdlog.MultiOpen.Count; i++ )
	{
		// Get data file
		fdlog.Get("A", i + 1); // +1 for LabTalk's 1-based index
		LT_get_str("%A", sz, MAXFULLPATH);
		
		// Import data file
		ImportFile(pgTarget, trFilter, sz, i);
		Project.Operations.Run();
	}

	Project.Operations.SetBatchProcessing(FALSE);
	return true;
}
#endif //_OPERATION_H

/// EJP 09-24-2003 v7.5706 QA70-5230 NOTIFY_USER_OF_IMPORT_ERROR
int ImportErrorMsgBox(int iErr, LPCSTR lpcszFilter)
{
	string str, strErr;
	bool bAppendFilterName = false;

	switch( iErr )
	{
	case IMPERR_NO_FILTER:
		strErr = _L("No filter specified.");
		break;
	case IMPERR_LOAD_FILTER:
		str = _L("Error loading import filter:\n%s");
		strErr.Format(str, lpcszFilter);
		break;
	case IMPERR_NO_TARGET_PAGE:
		strErr = _L("No target page for data.");
		bAppendFilterName = true;
		break;
	case IMPERR_GET_IMPORT_FUNC:
		string strFilterType;
		Tree trFilter;
		if( lpcszFilter && trFilter.Load(lpcszFilter) )
		{
			fuGetFilterType(trFilter, strFilterType);
			strErr.Format(_L("Failed to get pointer to %s import function."), strFilterType);
		}
		else
			strErr = _L("Failed to get pointer to import function.");
		break;
	case IMPERR_IMPORT_FUNC_ERR:
		string strFilterType;
		Tree trFilter;
		if( lpcszFilter && trFilter.Load(lpcszFilter) )
		{
			fuGetFilterType(trFilter, strFilterType);
			strErr.Format(_L("%s import function returned error."), strFilterType);
		}
		else
			strErr = _L("The import function returned error.");
		bAppendFilterName = true;
		break;
	case IMPERR_PREPARE_WKS:
		strErr = _L("Error preparing worksheet for import.");
		bAppendFilterName = true;
		break;
	case IMPERR_FILTER_TYPE:
		strErr = _L("Unknown filter type.");
		bAppendFilterName = true;
		break;
	case IMPERR_NONE:
	case IMPERR_CANCEL_ON_LOCKED_COL:
		return 0; // no error to report
	default:
		strErr = _L("Unknown error.");
		bAppendFilterName = true;
		break;
	}

	string strMsg;
	if( bAppendFilterName && lpcszFilter )
		strMsg.Format(_L("%s\n\nUsing filter:\n%s"), strErr, lpcszFilter);
	else
		strMsg = strErr;
	MessageBox(GetWindow(), strMsg, _L("File Import Error"));

	return 0;
}
/// end NOTIFY_USER_OF_IMPORT_ERROR

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -