void CSafeStream::close()
{
	m_bNeedToClose = FALSE;
	if(m_pOut)	// they may have used this class without using the stream function openStream()
	{
		m_pOut->close();
		delete m_pOut;
		m_pOut=NULL;
	}

	// rename
	if(m_bMakeBackup)
	{
		CString s(m_sPath.Left(m_sPath.GetLength()-4)); // everything but the ext
		CString sBackPath;
		sBackPath.Format("%s-%s.bak", s, m_sPath.Right(3));
		if(_access(sBackPath, 0) != -1)
		{
			if(!DeleteFile(sBackPath))	// delete old backup
			{
				checkForFileError(sBackPath);
				return;
			}
		}
		if(_access(m_sPath, 0) != -1)
		{
			if(! MoveFile (m_sPath, sBackPath)) // make the old file the backup
			{
				checkForFileError(m_sPath);
				return;
			}
		}
	}
	else	// no backup file wanted
	{
		if(_access(m_sPath, 0) != -1)
		{
			if(!DeleteFile(m_sPath))	// delete old file
			{
				checkForFileError(m_sPath);
				return;
			}
		}
	}
	if(! MoveFile (m_sTempPath, m_sPath)) // make our file the real one
	{
		checkForFileError(m_sTempPath);
		return;
	}
}
// currently can only add the last part of the directory
// i.e.  given d:\a\b\c\      a and b must already exist
BOOL ensureDirExists(LPCTSTR lpszDir)
{
	int error =_mkdir(lpszDir);
	if(error != 0 && error !=EACCES)
	{	checkForFileError(lpszDir);
		return FALSE;
	}
	return TRUE;
}
BOOL CPathDescriptor::copyTo(LPCTSTR lspzToPath, BOOL bFailIfExists)
{
	if(!CopyFile(getFullPath(), lspzToPath, bFailIfExists))
	{
		checkForFileError(getFullPath(), lspzToPath);
		return FALSE;
	}
	return TRUE;
}
CTime getModifiedTime(LPCTSTR lpszPath)
{
	struct _stat file_stat;
	if(_stat( lpszPath, &file_stat ) !=0)
	{
		checkForFileError(lpszPath);	// should be pretty rare
		return CTime();
	}

	return CTime(file_stat.st_mtime);
}
// jdh 3/14/2000 changed to throw exception if file is not found
CTime CPathDescriptor::getModifiedTime() const
{
	struct _stat file_stat;
	if(_stat( getFullPath(), &file_stat ) !=0)
	{
		checkForFileError(getFullPath(), NULL, TRUE, TRUE);
		return CTime();
	}

	return CTime(file_stat.st_mtime);
}
// you can call getTempPath(), instead of this, when you don't want a stream, just a path
ofstream& CSafeStream::openStream()
{
	m_pOut = new ofstream(m_sTempPath, ios::out);
	if(m_pOut->is_open())
	{
		m_bNeedToClose = TRUE;
		return *m_pOut;
	}
	checkForFileError(m_sTempPath);
	ASSERTX(FALSE);
	return *m_pOut;	// not a good choice, but highly unlikely to get this far
}
BOOL CPathDescriptor::fileIsReadOnly(void)
{
  DWORD dwResult = GetFileAttributes(getFullPath());
  if (dwResult == 0xFFFFFFFF)
	{
	  checkForFileError(getFullPath(), "");
	  return FALSE;
	}
  if (dwResult & FILE_ATTRIBUTE_READONLY)
	return TRUE;

  return FALSE;
}
BOOL CPathDescriptor::forceFileWritable(void)
{
  LPCTSTR lpszPath = getFullPath();
  if (!fileIsReadOnly())
	return TRUE;		// nothing to do

  DWORD dwResult = GetFileAttributes(lpszPath);
  DWORD dwReadOnlyMask = FILE_ATTRIBUTE_READONLY ^ 0xFFFFFFFF;
  dwResult &= dwReadOnlyMask;	// turn off read only attribute
  if (!SetFileAttributes(lpszPath, dwResult))
	{
	  checkForFileError(lpszPath, "");
	  return FALSE;
	}
  return TRUE;
}
// called by CProjectDoc::OnOpenDocument() for each \+Lang field it encounters
// This STATIC method will read the parameters and then try to open the language
// file.
CLangModelsDoc* CLangModelsDoc::loadDocument(LPCTSTR lpszField,  LPCTSTR sProjectPath)
{
	CString sPath(lpszField); // sAbrev,
//	CParseStream stream(lpszField);
//	stream.word(sAbrev, FALSE);	// i'm  thinking the abrev appearing in the project file is not used anymore
//	stream.getQuotedString(sPath);
	//CCarlaLanguage *pLang = new CCarlaLanguage(sAbrev);
	CFileStatus status;
	// is the file there?
	if(!CFile::GetStatus(sPath, status))
	{
	// if not, look in the same directory as this project
		//CString sPossiblePath = getLang()->getFilePath(getLang()->getLanguageDirectory(sProjectPath, FALSE));
		CString sPossiblePath = ::getDirectory(sProjectPath);
		sPossiblePath += getFileName(sPath);
		sPossiblePath += " control files\\";
		sPossiblePath += getFullFileName(sPath);
		// if it's there, switch the path and don't bother the user
		if(CFile::GetStatus(sPossiblePath, status))
			sPath = sPossiblePath;
		else
		{	// ask the user to find it

			if(!theApp.askUserToFindFile(sPath, CLangModelsDoc::getRegFileTypeID()))
			{
				THROWSTRING3("The language file", getFullFileName(sPath), " could not be found.");
			}
		}
	}

	CLangModelsDoc *pLangDoc = (CLangModelsDoc *)theApp.internalOpenDocumentFile(sPath);
	if(!pLangDoc)
	{
		checkForFileError(sPath);
		THROWSTRING3("The language file ", sPath, " could not be opened.");
	}
	else if(!pLangDoc->IsKindOf(RUNTIME_CLASS(CLangModelsDoc)))
	{
		ASSERTX(FALSE); // to do notify user or something
	}

	return pLangDoc;
}
// 3/14/2000 jdh added bChangeDescriptor option
// 3/15/2000 jdh fixed bChangeDescriptor option
BOOL CPathDescriptor::moveTo(LPCTSTR lspzToPath, BOOL bFailIfExists, BOOL bChangeDescriptor)
{
#ifdef ORIG
	if(!MoveFileEx(	getFullPath(),
					lspzToPath,
					MOVEFILE_COPY_ALLOWED | (bFailIfExists)?NULL:MOVEFILE_REPLACE_EXISTING
					)	)
#else // ORIG
	if(!MoveFile(getFullPath(), lspzToPath)	)
#endif // ORIG
	{
		checkForFileError(getFullPath(), lspzToPath);
		return FALSE;
	}
	 if(bChangeDescriptor)
		*this = lspzToPath;
	//parsePath();
	return TRUE;

}
示例#11
0
void CQuickParseView::OnQuickParseGo()
{
	CWaitCursor wc;

	//---- BRING THE CONTROL FILES UP TO DATE ON THE DISK (added feb 8, 99)
	theApp.getProject()->synchronizeExternals();


	CQuickParseDoc* pDoc = (CQuickParseDoc*)GetDocument();
	UpdateData(TRUE); // get data out of form and into our members
	m_sInput.TrimLeft();
	if(m_sInput.IsEmpty())
		return;

	try
	{
//		sndPlaySound("QuickParse", SND_ASYNC );

		CCarlaLanguage* pSourceLang = pDoc->m_pSourceLang;
		ASSERTX(pSourceLang);
		pSourceLang->prepareMFSForProcessors(); // among other things, get the comment character into the MFS

		CSrcTextProcessingPrefs procPrefs(pSourceLang,
									NULL,
									CProcessingPrefs::kSourceAna);


		CProcessStatus status(	&procPrefs,
								m_sInput,
								pSourceLang,
								pSourceLang->getMFS(),
								NULL,	// output lang
								NULL); // output mfs

		// jdh 3/14/2000 changed to this from some custom code
		if (!status.setupTempDir(pSourceLang->getName()))
			return;	// assumes the user has already been informed

	// added jdh 3/13/2000
		m_bDidRefresh = FALSE; // will be set to TRUE if we refresh
		BOOL bDidGetPreprocessedDicts = status.loadPreprocessedSrcDictsArrayFromLang();
		if(!bDidGetPreprocessedDicts || m_bManualRefreshPending || getNeedDictPreprocessing(status))
			if(!this->preprocessDicts(&status))
				return;

		prepareAmpleProcess();
		loadAmpleOptions();
		m_sOutput = ""; // in case of an exception
		CString sTraceOutput;


		CString sPath;
		sPath.Format(_T("%s\\QP-Ample-Trace.log"), (LPCTSTR)status.getTempDirectory());


#ifndef hab218
		if (m_bTrace && m_bManualParse)
		  {
			m_sTraceMorphs = _T("");
			loadAmpleOptions();
			getManualParse(status, sPath, pSourceLang);
			status.m_sRAWString = m_sInput;
			loadAmpleOptions();
		  }
#endif // hab218
	// DO THE PARSING
		if(!m_bTrace)
		{	// !!! this path is needed if we want to check for errors
			m_pAmpleProcess->processRAWTextString(status, sPath);
		}
		else
		{
			m_pAmpleProcess->processRAWTextString(status, sPath);
		//	m_sOutput = status.m_sRAWString;

			TRY
			{
				CFile log(sPath, CFile::modeRead);
				DWORD sz = log.GetLength();
				if(sz)
				{
					/* If the buffer is ridiculously big (e.g. > 1MB) then the likelyhood
					 * a stack overflow increases - so we truncate early rather than later
					 * let's say that 3 bytes for one UTF8 char is a good guess
					 */
					if (sz > MAX_TRACEOUTPUT * 3)
						sz = MAX_TRACEOUTPUT * 3;
					// on the heap rather than on the stack - stack overflows are easily created
					char* buf = (char *) malloc(sz+2);
					ASSERTX(buf);
					int iReadBytes = log.Read(buf, sz);
					buf[iReadBytes] = '\0'; //terminate it
					USES_CONVERSION_U8;
					sTraceOutput = U82CT(buf);
					free(buf);
				}
				log.Close();
			}
			CATCH( CFileException, e )
			{
#ifdef Before1_04
				checkForFileError(PATH);
#else  // hab 1.04
				checkForFileError(sPath);
#endif // Before1_04
			}
			END_CATCH
		}

		// build the parse part of the answer
		CAmpleResult* pAResult = CAmpleResult::buildResult(status.m_sRAWString);
		if(pAResult)
		{
#ifndef hab218
			m_sOutput += pAResult->getStringRepresentation();
#else //hab218
			m_sOutput = pAResult->getStringRepresentation();
#endif // hab218
			delete pAResult;
		}
		else
			m_sOutput.Format (_T("You have encountered a bug in either CarlaStudio or the Ample DLL.  CarlaStudio could not understand the Ample DLL's answer, which was: %s"), (LPCTSTR)status.m_sRAWString);

		// add the trace part of the answer
		if(m_bTrace)
			m_sOutput += sTraceOutput;

		// do we need to truncate the result?
		TRACE(_T("m_sOutput.GetLength() =%d"), m_sOutput.GetLength());
		if(m_sOutput.GetLength() > MAX_TRACEOUTPUT)
		{
			m_sOutput = m_sOutput.Left(MAX_TRACEOUTPUT);
			m_sOutput += _T("\r\n--Ample's output is too long to fit here.  It has been truncated. ");
		}

		// jdh 3/14/2000 store this set of dictionaries.  we do it only now in so that an error in processing them will be in the user's face
		status.storePreprocessedSrcDictsArrayInLang();

	}
// returns an XML document as a string
CString CPhraseParser::parseString(CString sPhrase, BOOL bTrace, CString sTraceMorphs, BOOL bForceRefresh)
{
	CString sResult;
	try
	{
		//---- BRING THE CONTROL FILES UP TO DATE ON THE DISK
		//jdh 10-june-2000
		if (theApp.getProject()->IsModified())
		{
			throw "Sorry, you have modified the CarlaStudio project and must save it before parsing.  This is a bug which I have not been able to squash.";
			/*			//the following synchronizeExternals() will throw an exception
			//this happens when the MFC code tries to set a waitcursor
			::SetForegroundWindow(theApp.GetMainWnd()->m_hWnd);
			theApp.getProject()->synchronizeExternals();
*/ 		}

		if(bForceRefresh)
			m_bManualRefreshPending = TRUE;

		sPhrase.TrimLeft();
		if(sPhrase.IsEmpty())
			throw "Phrase was empty";


		ASSERTX(m_pSourceLang);
		m_pSourceLang->prepareMFSForProcessors(); // among other things, get the comment character into the MFS

		CSrcTextProcessingPrefs procPrefs(m_pSourceLang,
									NULL,
									CProcessingPrefs::kSourceAna);


		CProcessStatus status(	&procPrefs,
								sPhrase,
								m_pSourceLang,
								m_pSourceLang->getMFS(),
								NULL,	// output lang
								NULL); // output mfs

		// jdh 3/14/2000 changed to this from some custom code
		if (!status.setupTempDir(m_pSourceLang->getName()))
			throw "CS Couldn't setup the temp directory";	// assumes the user has already been informed

	// added jdh 3/13/2000
		m_bDidRefresh = FALSE; // will be set to TRUE if we refresh
		BOOL bDidGetPreprocessedDicts = status.loadPreprocessedSrcDictsArrayFromLang();
		if(!bDidGetPreprocessedDicts || m_bManualRefreshPending || getNeedDictPreprocessing(status))
			if(!this->preprocessDicts(&status))
				throw "CS had problems preprocessing the dictionaries";

		prepareAmpleProcess();
		loadAmpleOptions(bTrace, sTraceMorphs);
		sResult = ""; // in case of an exception
		CString sTraceOutput;


		CString sPath;
		sPath.Format("%s\\QP-Ample-Trace.log", status.getTempDirectory());


	// DO THE PARSING
		if(!bTrace)
		{	// !!! this path is needed if we want to check for errors
			m_pAmpleProcess->processRAWTextString(status, sPath);
		}
		else
		{
			m_pAmpleProcess->processRAWTextString(status, sPath);

			TRY
			{
				CFile log(sPath, CFile::modeRead);
				DWORD sz = log.GetLength();
				if(sz)
				{
					char* buf = new char[sz+2];
					ASSERTX(buf);
					int iReadBytes = log.Read(buf, sz);
					buf[iReadBytes] = '\0'; //terminate it
					sTraceOutput = buf;
					delete buf;
				}
				log.Close();
			}
			CATCH( CFileException, e )
			{
				checkForFileError(sPath);
			}
			END_CATCH
		}

		// jdh 3/14/2000 store this set of dictionaries.  we do it only now in so that an error in processing them will be in the user's face
		status.storePreprocessedSrcDictsArrayInLang();
		sResult = status.m_sRAWString;
	}