//----------------------------------------------------------------------------------------
// GetAllDirectoryNames
//----------------------------------------------------------------------------------------
void
ZMFileUtils::GetAllDirectoryNames(
	const PMString&						parentPath,
	ZMPMStringSList &					oDirectoryNames) const
{
	SDKFileHelper parentFileHelper(parentPath);
	IDFile parIDFile = parentFileHelper.GetIDFile();
	PlatformFileSystemIterator iter;
	if(!iter.IsDirectory(parIDFile))
		return;

	iter.SetStartingPath(parIDFile);
	
	IDFile idFile;
	PMString filter("*.*");
	filter.InsertW(FileUtils::GetDirectorySeperator(), 0);
	bool16 hasNext= iter.FindFirstFile(idFile, filter);

	while(hasNext) {
		if (iter.IsDirectory (idFile))
		{
			SDKFileHelper fileHelper(idFile);
			
			PMString fileNamePath = idFile.GetFileName();
			if(ValidPath(fileNamePath))
			{
				oDirectoryNames.push_back(new PMString(fileNamePath));
			}
		}

		hasNext= iter.FindNextFile(idFile);
	}
}
示例#2
0
文件: CVS.cpp 项目: VT-ATEx/Core-2014
CVS::log_err CVS::openCVS(const bool create /*= true*/, const string filename /*= "logfile"*/, const string directory /*= "../logs/"*/) {
	ostringstream oss;
	int32_t duplicate;

	mkdir(directory.c_str(), 0755);

	if (create) {
		
		if ((duplicate = fileHelper(directory, filename)) < 0)
			return LOG_ERR;

		if (duplicate > 0)
			oss << directory << filename << duplicate << ".csv";
		else
			oss << directory << filename << ".csv";
		path = oss.str();

		if ((file = fopen(path.c_str(), "w")) == NULL)
			return LOG_ERR;
	} else {
		oss << directory << filename << ".csv";
		path = oss.str();

		if ((file = fopen(path.c_str(), "a")) == NULL)
			return LOG_ERR;
	}
	open = true;

	return LOG_OK;
}
//----------------------------------------------------------------------------------------
// FetchAdsFileFromPath
//----------------------------------------------------------------------------------------
bool
ZMFileUtils::FetchAdsFileFromPath(
	const PMString&						parentPath,
	ZMPMStringSList &					oAdsList,
	const PMString&						inFilePrefix) const
{
	LogFunctionEnterExit;
	
	IZPLog_Str_( thisFileLA, enLT_DebugInfo, "From path : %s", parentPath.GrabCString());
	SDKFileHelper fileHelper(parentPath);
	IDFile fileId = fileHelper.GetIDFile();
	PlatformFileSystemIterator iter;
	if(!iter.IsDirectory(fileId))
		return false;

	iter.SetStartingPath(fileId);

	Utils<IZMAdFileFacade> adFileUtil;
	bool result = false;
	IDFile xmlIdFile;

	PMString filter(inFilePrefix);
	filter.Append("*.xml");
	filter.InsertW(FileUtils::GetDirectorySeperator(), 0);
	IZPLog_Str_( thisFileLA, enLT_DebugInfo, "File filter : %s", filter.GrabCString());

	bool16 hasNext = iter.FindFirstFile(xmlIdFile, filter);

	while(hasNext) {
		SDKFileHelper xmlFileHelper(xmlIdFile);
		
		PMString xmlFileName = xmlIdFile.GetFileName();
		if(ValidPath(xmlFileName))
		{
#ifdef MACINTOSH
			PMString extStr(".xml");
			extStr.SetTranslatable(kFalse);
			if (! VerifyPrefixAndSuffix(xmlFileName, inFilePrefix, extStr))
			{
				hasNext= iter.FindNextFile(xmlIdFile);
				continue;
			}
#endif
			
			PMString xmlFullPath (parentPath);
			xmlFullPath.Append(FileUtils::GetDirectorySeperator());
			xmlFullPath.Append(xmlFileName);

			if (adFileUtil->CanBeValidXMLFile(xmlFullPath))
			{
				oAdsList.push_back(new PMString(xmlIdFile.GetFileName()));
				result = true;
			}
		}

		hasNext= iter.FindNextFile(xmlIdFile);
	}

	return result;
}
//----------------------------------------------------------------------------------------
// FetchEditionsFromPath
//----------------------------------------------------------------------------------------
bool
ZMFileUtils::FetchEditionsFromPath(
	const PMString&						parentPath,
	ZMstEditionSList &					oEditionList) const
{
	LogFunctionEnterExit;
	
	IZPLog_Str_( thisFileLA, enLT_DebugInfo, "From path : %s", parentPath.GrabCString());
	SDKFileHelper fileHelper(parentPath);
	IDFile fileId = fileHelper.GetIDFile();
	PlatformFileSystemIterator iter;
	if(!iter.IsDirectory(fileId))
		return false;

	InterfacePtr<const IWorkspace> iWorkspace(gSession->QueryWorkspace());
	InterfacePtr<const IZMPrefs> workspacePrefs( iWorkspace, UseDefaultIID() );
	ASSERT( workspacePrefs );
	
	const PMString & filePrefixName = workspacePrefs->GetAdFileNamePrefix();
	IZPLog_Str_( thisFileLA, enLT_DebugInfo, "File prefix name : %s", filePrefixName.GrabCString());
	
	bool result = false;
	ZMPMStringSList editionList;
	this->GetAllDirectoryNames(parentPath, editionList);
	ZMPMStringSListCIter editionIter = editionList.begin();
	ZMPMStringSListCIter editionIterEnd = editionList.end();
	for(; editionIter != editionIterEnd; ++editionIter)
	{
		stEdition *currEdition = new stEdition();
		StPtrDeleter<stEdition> autoDel( currEdition, true );

		const PMString edition(**editionIter);
		PMString editionFullPath (parentPath);
		editionFullPath.Append(FileUtils::GetDirectorySeperator());
		editionFullPath.Append(edition);

		if (this->FetchAdsFileFromPath(editionFullPath, currEdition->mXmlFiles, filePrefixName))
		{
			result = true;
			currEdition->mName = edition;
			oEditionList.push_back(currEdition);
			autoDel.Forget();
		}
	}

	return result;
}
//----------------------------------------------------------------------------------------
// CanBeValidXMLFile
//----------------------------------------------------------------------------------------
bool16
ZMAdFileFacade::CanBeValidXMLFile(
	const PMString&						path) const
{
	SDKFileHelper fileHelper(path);
	IDFile fileId = fileHelper.GetIDFile();
	InterfacePtr<IPMStream> fileStream((IPMStream*)StreamUtil::CreateFileStreamRead(fileId));
	if (! fileStream)
		return kFalse;

	StStreamCloser autoReleaseStream(fileStream);
	const char *strToCompare = STRXMLTag_ClassWizardImposition;
	char str[105];
	fileStream->XferByte((uchar *)str, 100);
	str[104] = '\0';

	if (strstr(str, strToCompare))
		return kTrue;

	return kFalse;
}
//----------------------------------------------------------------------------------------
// FetchListFromPath
//----------------------------------------------------------------------------------------
void
ZMFileUtils::FetchListFromPath(
	const PMString&						parentPath,
	ZMstTitleSList &					oTitleList) const
{
	LogFunctionEnterExit;
	
	IZPLog_Str_( thisFileLA, enLT_DebugInfo, "From path : %s", parentPath.GrabCString());
	SDKFileHelper fileHelper(parentPath);
	IDFile fileId = fileHelper.GetIDFile();
	PlatformFileSystemIterator iter;
	if(!iter.IsDirectory(fileId))
		return;

	ZMPMStringSList titleList;
	this->GetAllDirectoryNames(parentPath, titleList);
	ZMPMStringSListCIter titleIter = titleList.begin();
	ZMPMStringSListCIter titleIterEnd = titleList.end();

	for(; titleIter != titleIterEnd; ++titleIter)
	{
		stTitle *currTitle = new stTitle();
		StPtrDeleter<stTitle> autoDel( currTitle, true);

		const PMString & title = (**titleIter);
		PMString titleFullPath(parentPath);
		titleFullPath.Append(FileUtils::GetDirectorySeperator());
		titleFullPath.Append(title);

		if ( this->FetchEditionsFromPath(titleFullPath, currTitle->mEditions) )
		{
			currTitle->mName = title;
			oTitleList.push_back(currTitle);
			autoDel.Forget();
		}
	}
}
//----------------------------------------------------------------------------------------
// ParseAdXMLFile
//----------------------------------------------------------------------------------------
bool
ZMAdFileFacade::ParseAdXMLFile(
	const PMString &					path,
	ZMstAdSList &						oAdsList,
	uint32 *							oLastUpdateDate) const
{
	if (oLastUpdateDate)
		*oLastUpdateDate = 0;

	bool result = false;
	SDKFileHelper fileHelper(path);
	IDFile fileId = fileHelper.GetIDFile();
	InterfacePtr<IPMStream> fileStream((IPMStream*)StreamUtil::CreateFileStreamRead(fileId));
	if (! fileStream)
		return result;

	//Set last update date
	if (oLastUpdateDate)
		FileUtils::GetModificationDate(fileId, oLastUpdateDate);

	InterfacePtr<IK2ServiceRegistry> serviceRegistry(gSession, UseDefaultIID());
	InterfacePtr<IK2ServiceProvider> xmlProvider(serviceRegistry->QueryServiceProviderByClassID(kXMLParserService, kXMLParserServiceBoss));
	InterfacePtr<ISAXServices> saxServices(xmlProvider, UseDefaultIID());

	InterfacePtr<ISAXContentHandler> contentHandler(static_cast<ISAXContentHandler*>(::CreateObject(kZMSAXAdContentHandlerBoss, ISAXContentHandler::kDefaultIID)));
	InterfacePtr<ISAXEntityResolver> entityResolver(static_cast<ISAXEntityResolver*>(::CreateObject(kXMLEmptyEntityResolverBoss, ISAXEntityResolver::kDefaultIID)));
	if (Utils<IShowAlertRegistryUtils>())
	{
		Utils<IShowAlertRegistryUtils>()->DoSetShouldShowCmd(kXMLEntityNotResolved, kFalse);
		Utils<IShowAlertRegistryUtils>()->DoSetShouldShowCmd(kXMLPublicIDsNotSupported, kFalse);
	}

	// set parser options
	InterfacePtr<ISAXParserOptions> parserOptions(saxServices, UseDefaultIID());
	parserOptions->SetNamespacesFeature(kTrue);
	parserOptions->SetNamespacePrefixesFeature(kTrue);
	parserOptions->SetWarnCaseXMLDECLFeature(kFalse);
	parserOptions->SetWarnCaseDTDFeature(kFalse);
	parserOptions->SetValidationFeature(kFalse);

	saxServices->ParseStream(fileStream, contentHandler, entityResolver);
	ErrorCode err = ErrorUtils::PMGetGlobalErrorCode();
	if (err == kIZMInvalidXMLFileError)
	//if (err == kFailure || err == kIZMInvalidXMLFileError)
	{
		PMString errStr(kIZMediaInvalidXMLFileErrorCodeStringKey);
		ErrorUtils::PMSetGlobalErrorCode(kSuccess);
		CAlert::ErrorAlert(errStr);
		//ErrorUtils::PMSetGlobalErrorCode(kFailure, &errStr);

		//ErrorUtils::PMSetGlobalErrorCode(kFailure, &errStr);
		//InterfacePtr<IXMLServicesError> serviceErr (saxServices, UseDefaultIID());
		//err = serviceErr->GetErrorCode ();
		//if (err == kIZMInvalidXMLFileError)
		//	int x =0;
		//PMString errStr("XML Error");
	}

	if (!err)
	{
		InterfacePtr<const IZMAdFileData> zmAdList(contentHandler, IZMAdFileData::kDefaultIID);
		if (zmAdList)
		{
			const ZMstAdSList & adsList= zmAdList->GetAdsList();
			oAdsList.Copy(adsList, true);	//This will take ownership of objects in adsList

			result = true;
			//Testing code for checking values.
			//ZMstAdSListCIter iter = adsList.begin();
			//ZMstAdSListCIter iterEnd = adsList.end();
			//for (; iter != iterEnd; ++iter)
			//{
			//	const stAd & ad = **iter;
			//	PMString customer = ad.mCustomer;
			//}
		}
	}

	return result;
}
/**
 * Generate scez file menu slot function.
 */
void CreateSceFile::generateCompressedFileSlot()
{
    bool success = false;
    QString sceFileName = ui->fileLineEdit->text() + "/" + QFileInfo(ui->fileLineEdit->text()).fileName() + ".sce";
    ScriptFile fileHelper(this, sceFileName, false);
    QStringList copiedFiles;

    if(!isExecutableScriptInFilesTable())
    {
        QMessageBox::information(this, "could not start generation", "No executable script defined.");
        return;
    }

    setEnabled(false);

    ui->progressBar->setValue(0);
    ui->progressBar->setMinimum(0);
    ui->progressBar->setMaximum(ui->filesTableWidget->rowCount() * 2);

    statusBar()->showMessage("generating scez file", 0);

    generateSlot(&copiedFiles, ui->progressBar, &success);
    if(success)
    {
        copiedFiles << sceFileName;
        QString zipFileName = ui->fileLineEdit->text() + ".scez";

        (void)fileHelper.deleteFile(zipFileName, false);

        QList<QStringList> fileList;
        QString rootDir = QDir(QFileInfo(sceFileName).path()).absolutePath();
        for(auto el : copiedFiles)
        {
            QStringList entry;
            entry << el;
            entry << QFileInfo(el).filePath().remove(0, rootDir.length() + 1);
            fileList << entry;
        }

        success = ScriptFile::zipFiles(zipFileName, fileList, ui->progressBar);
        if(!success)
        {
            (void)QFile::remove(zipFileName);
        }
        else
        {
            QFile inFile;
            inFile.setFileName(zipFileName);

            success = inFile.open(QIODevice::ReadOnly);
            if(success)
            {
                QCryptographicHash hashObject(QCryptographicHash::Sha512);
                success = hashObject.addData(&inFile);
                if(success)
                {   inFile.close();
                    success = inFile.open(QIODevice::Append);
                    if(success)
                    {
                        (void)inFile.seek(inFile.size());
                        QByteArray hash = hashObject.result();
                        success = (inFile.write(hash) == hash.length()) ? true : false;
                    }
                }

                inFile.close();
            }
        }
    }

    //Delete the sce folder.
    (void)QDir(ui->fileLineEdit->text()).removeRecursively();

    if(success)
    {
        QMessageBox::information(this, "information", "scez file creation succeeded");
        statusBar()->showMessage("scez file created", 5000);
    }
    else
    {
        statusBar()->showMessage("scez file creation failed", 5000);
    }

    setEnabled(true);
}
		void OggWriter::openResource() {
			FILE * fout = fileHelper( this->getFilePath() );
			this->fout_.reset( fout, std::fclose );
		}