📄 app_utils.c
字号:
}
icpy++;
} while ( icpy < isize );
xCopy.SetSize(idst);
yCopy.SetSize(idst);
zCopy.SetSize(idst);
for (int ii = 0; ii < idst; ii++)
{
xCopy[ii] = xx[ii];
yCopy[ii] = yy[ii];
zCopy[ii] = zz[ii];
}
return isize-ii;
}
////////////////////////////////////////////////////////////////////////////////////
// File related functions
////////////////////////////////////////////////////////////////////////////////////
/**
Get the file extension from a string containing a filename.
Example:
string strFile("c:\\somefolder\\myfile.txt");
string strExt = GetFileExtension(strFile);
ASSERT(0 == strExt.Compare("txt"));
Parameters:
lpcszFile=Input string containing a file name
Return:
Returns a string containing the file extension found in the input string.
If the input string does not contain an extension then an empty string is returned.
*/
string GetFileExtension(LPCSTR lpcszFile)
{
string str(lpcszFile);
int nPeriod = str.ReverseFind('.');
int nSlash = str.ReverseFind('\\');
if( nPeriod > nSlash )
str.Delete(0, nPeriod + 1);
else
str.Empty();
return str;
}
/**
Get the path and/or just filename without extension from a string containing a path and/or
just filename.
Example:
string strFilenameWithoutExt, strFilenameWithExt;
strFilenameWithExt = "Default.oaf";
strFilenameWithoutExt = GetFilenameWithoutExt( strFilenameWithExt );
ASSERT( strFilenameWithoutExt.Compare( "Default" ) == 0 );
Parameters:
strFilenameWithExt=Input string containing an optional path and a filename with filetype extension
Return:
Returns a filename without filetype extension (but with path if in original string) from an optional
path and filename with extension.
*/
string GetFilenameWithoutExt( string strPathAndFilename )
{
return strPathAndFilename.Left( strPathAndFilename.ReverseFind( '.' ) );
}
/**
Search a windows folder adding any filenames that match a specified file filter to an output string.
Options specify whether or not subfolders are to be recursively searched and whether or not path
and file extension are to be included with filename.
Example:
string strTemplateFileList;
GetFilenamesInFolder(strTemplateFileList, "", "*.otw", FALSE, 3);
Parameters:
strListOfFilenames=Output list of formatted filenames found in folder
lpcstrPath=Input path to start searching, default "" is path to Origin software folder
lpcstrFileFilter=Input file filter which may include DOS wild card characters, default is "*.*"
bRecursive=Input option to recursively search subfolders, default is FALSE
wDisplayOptions=Input bitwise flag specifying display options, default value APP_UTILS_FILE_WITH_PATH
displays path, filename and filetype, APP_UTILS_FILE_NO_PATH displays filename and filetype without
path, APP_UTILS_FILE_NO_EXT displays path and filename with out filetype extension, and
APP_UTILS_FILE_NO_PATH | APP_UTILS_FILE_NO_EXT displays file name without path or filetype extension
Return:
Returns APP_UTILS_NO_ERROR on success or an APP_UTILS warning or error code on failure.
*/
int GetFilenamesInFolder(
string& strListOfFilenames,
LPCSTR lpcstrPath,
LPCSTR lpcstrFileFilter,
BOOL bRecursive,
UINT wDisplayOptions) // lpcstrPath = "", lpcstrFileFilter = "", bRecursive = FALSE, wDisplayOptions = APP_UTILS_FILE_WITH_PATH
{
string strPath = lpcstrPath;
if( strPath.IsEmpty() )
strPath = GetAppPath(TRUE);
string strFileFilter = lpcstrFileFilter;
if( strFileFilter.IsEmpty() )
strFileFilter = "*.*";
string strSearchPathFilter;
strSearchPathFilter.Format( "%s*.*", strPath ); // Build search start path\file name
WIN32_FIND_DATAA find;
HANDLE hFile = FindFirstFile(strSearchPathFilter, &find); // Find first file/folder that matches
if( hFile != INVALID_HANDLE_VALUE ) // If valid handle...
{
string strFileFound;
do // Do while next file/folder is found...
{
if( find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) // If folder is next...
{
if( bRecursive ) // If user wants to recurse...
{
strFileFound = find.cFileName;
if( strFileFound.Compare(".") && strFileFound.Compare("..") ) // If folder is not "." or ".."
{
strFileFound.Format("%s%s\\", strPath, find.cFileName);
GetFilenamesInFolder(strListOfFilenames, strFileFound,
strFileFilter, TRUE, wDisplayOptions); // Recurse into subfolder
}
}
}
else // Else file is next...
{
strFileFound = find.cFileName;
if( strFileFound.Match(strFileFilter) ) // If file name matches file filter...
{
if( !(wDisplayOptions & APP_UTILS_FILE_NO_PATH) ) // If user wants path...
strFileFound.Format("%s%s", strPath, strFileFound); // Else use path and filename
if( wDisplayOptions & APP_UTILS_FILE_NO_EXT ) // If user does not want file extension...
strFileFound = GetFilenameWithoutExt(strFileFound); // Strip off file extension from filename
if( !strListOfFilenames.IsEmpty() ) // If list already has item...
strListOfFilenames += "|"; // Append token separator
strListOfFilenames += strFileFound; // Append newly found filename to list
}
}
} while( FindNextFile( hFile, &find ) ); // While next file is found...
FindClose( hFile ); // Close find
return APP_UTILS_NO_ERROR;
}
return APP_UTILS_NO_FILES_FOUND_ERROR;
}
/**
Get the file size of a file from an input string containing a path
and filename.
Example:
string strFileSize, strDataFile = "C:\\Origin80\\Origin.ini";
strFileSize = GetFileSize(strDataFile);
Parameters:
strPathAndFilename=Input string containing a path and filename with filetype extension
Return:
Returns a Windows like file size formatted for display.
*/
string GetFileSize(string strPathAndFilename)
{
string strFileSize;
WIN32_FILE_ATTRIBUTE_DATA fileInfo;
if( GetFileAttributesEx(strPathAndFilename, 0, &fileInfo) )
{
if( fileInfo.nFileSizeLow < 1024 )
strFileSize.Format("%d bytes", fileInfo.nFileSizeLow);
else
{
int nSize = fileInfo.nFileSizeLow/1024.0 + 0.5;
if( nSize < 1024 )
strFileSize.Format("%d KB", nSize);
else
{
nSize = nSize/1024.0 + 0.5;
strFileSize.Format("%d MB", nSize);
}
}
}
return strFileSize;
}
////////////////////////////////////////////////////////////////////////////////////
// Graph related functions
////////////////////////////////////////////////////////////////////////////////////
//
/**
Builds a list of the layer numbers in the active graph. The list is returned
as a series of tokens separated by the | character. The active layer number
is also returned.
Example:
string str;
int iActive;
iActive = GetLayerNumbers(str);
Return:
Returns a list of the layer numbers in the active graph as a series of tokens
separated by the | character. The layer number of the active layer is returned
on success and -1 is returned on failure.
*/
int GetLayerNumbers(string& strLayerNumbers)
{
int iActive = -1;
GraphPage gp;
gp = (GraphPage) Project.Pages();
if( gp.IsValid() )
{
for(int ii = 1; ii <= gp.Layers.Count(); ii++)
{
if( ii == 1 )
strLayerNumbers.Format("%d", ii);
else
strLayerNumbers.Format("%s|%d", strLayerNumbers, ii);
}
GraphLayer glLayer = gp.Layers();
if( glLayer.IsValid() )
iActive = glLayer.GetIndex();
}
return iActive;
}
////////////////////////////////////////////////////////////////////////////////////
// File related functions
////////////////////////////////////////////////////////////////////////////////////
//
/**
List all files of a specified file type found in the Origin
App Data folder and/or in the Origin software folder.
Example:
int iRetCode;
StringArray saResult;
iRetCode = GetSystemFiles(saResult, "otw");
Parameters:
saResult = the referrence of string array will receive the finding results
lpcszExt = the file extension name to matach file finding.
Return:
Returns TRUE for success, otherwise failure.
*/
BOOL GetSystemFiles(StringArray& saResult, LPCSTR lpcszExt)
{
// first, the application data path
string strAppDataPath = GetAppPath();
BOOL bRetApp = FindFiles(saResult, strAppDataPath, lpcszExt);
// Second, the Origin Exe path
string strOrgExePath = GetAppPath(TRUE);
// if either of tow seach succeed will return true.
BOOL bRetExe = FindFiles(saResult, strOrgExePath, lpcszExt, true);// 2nd time will need to check already in list or not
return ( bRetApp || bRetExe );
}
/**
Get the contents of a text file.
Example:
string strText;
int iError = LoadTextFile(strText, "C:\\Origin80\\Origin.ini");
Parameters:
lpcszFile=Input full path and file name to read
iNumLines=Input number of lines to read not counting skipped lines, default 0 reads all lines (after iFirstLine)
iFirstLine=Input first line to read, default 0 reads first line
iSkipLines=Input number of lines to skip for every one line read, default 0 skips no lines
Return:
Returns the contents of a text file on success and "" on failure.
*/
int LoadTextFile(string &strText, LPCSTR lpcszFile, uint iNumLines, uint iFirstLine, uint iSkipLines) // iNumLines = 0, iFirstLine = 0, iSkipLines = 0
{
StringArray saLines;
int iError = ReadFileLines(saLines, lpcszFile, iNumLines, iFirstLine, iSkipLines);
if( 0 == iError )
{
strText.SetTokens(saLines, '\n');
strText += "\n"; // EOL for last line
}
return iError;
}
/**
Read specified lines from a text file into an array of strings.
Example:
StringArray saLines;
int iError = ReadFileLines(saLines, "C:\\Origin80\\Origin.ini");
Parameters:
saLines=Reference to a string array that will hold the lines
lpcszFile=Name of file to read
iNumLines=Number of lines to read into the string array. If 0 then all lines are read.
iFirstLine=Zero based index of the first line to read into the string array.
iSkipLines=Number of lines to skip after every one line read.
Return:
Returns zero for success or non-zero to indicate error.
*/
int ReadFileLines(StringArray &saLines, LPCSTR lpcszFile, uint iNumLines, uint iFirstLine, uint iSkipLines)
{
if( NULL == lpcszFile )
return 1; // file name required
if( 0 == iNumLines )
return 0; // no lines to read
stdioFile sf;
if( sf.Open(lpcszFile, file::modeRead | file::shareDenyRead | file::typeText) )
{
string strLine;
BOOL bCont = TRUE;
while( iFirstLine-- && bCont )
bCont = sf.ReadString(strLine);
if( bCont ) // If can continue reading
{
int iLine = 0, iSkip;
while( sf.ReadString(strLine) )
{
saLines.Add(strLine);
if( iNumLines && ++iLine == iNumLines )
break;
iSkip = iSkipLines;
while( iSkip-- && bCont )
bCont = sf.ReadString(strLine);
}
}
sf.Close();
return 0; // success
}
return 1; // failed to open file
}
////////////////////////////////////////////////////////////////////////////////////
// Error redirection functions
////////////////////////////////////////////////////////////////////////////////////
//
/**
Get the LabTalk error redirection variable.
Example:
See related function OutputErrorMessage in App_Utils.c.
Return:
Returns the LabTalk error redirection variable which may have any of the following
values:
APP_UTILS_NO_ERROR_OUTPUT
WRITE_SCRIPT_WINDOW
WRITE_STATUS_BAR
WRITE_OUTPUT_LOG
WRITE_MESSAGE_BOX
WRITE_COMPILER_OUTPUT
*/
int GetErrOut()
{
int iErrOut;
double dErrOut;
LT_get_var("ErrOut", &dErrOut);
if( NANUM == dErrOut )
iErrOut = WRITE_SCRIPT_WINDOW;
else
iErrOut = (int) dErrOut;
if( iErrOut < APP_UTILS_NO_ERROR_OUTPUT || iErrOut > WRITE_COMPILER_OUTPUT )
iErrOut = WRITE_SCRIPT_WINDOW;
return iErrOut;
}
/**
Output an error message redirected by the LabTalk error redirection variable.
Example:
if( PopulateTreeNodeWithNonContiguousSelection(trStat.Data) )
{
OutputErrorMessage(SOC_WKS_DATA_SEL_ERROR_MSG);
return false;
}
*/
void OutputErrorMessage(LPCSTR lpcszMessage)
{
string strMsg = lpcszMessage;
int iErrOut = GetErrOut();
if( strMsg.IsEmpty() || APP_UTILS_NO_ERROR_OUTPUT == iErrOut )
return;
if( WRITE_MESSAGE_BOX == iErrOut )
strMsg.Write(iErrOut);
else
strMsg.WriteLine(iErrOut);
return;
}
/**
Set the LabTalk error redirection variable.
Example:
SetErrOut(WRITE_MESSAGE_BOX);
Parameters:
iErrOut=Input LabTalk error redirection variable which may have any of the following
values:
APP_UTILS_NO_ERROR_OUTPUT
WRITE_SCRIPT_WINDOW (default)
WRITE_STATUS_BAR
WRITE_OUTPUT_LOG
WRITE_MESSAGE_BOX
WRITE_COMPILER_OUTPUT
Return:
Returns the previous value of the LabTalk error redirection variable.
*/
int SetErrOut(int iErrOut)
{
double dErrOut;
int iPreviousErrOut;
iPreviousErrOut = GetErrOut();
if( iErrOut < APP_UTILS_NO_ERROR_OUTPUT || iErrOut > WRITE_COMPILER_OUTPUT )
iErrOut = WRITE_SCRIPT_WINDOW;
dErrOut = (double) iErrOut;
LT_set_var("ErrOut", dErrOut);
return iPreviousErrOut;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -