Ejemplo n.º 1
0
	BOOL GenerateHelper::WriteToFile( std::vector<std::string> vtStr , std::string strOutputFile )
	{
		if(IsExistFile(strOutputFile))
		{
			return FALSE;
		}
		if(!IsExistFile(GenerateHelper::GetFilePath(strOutputFile).c_str()))
		{
			int nResult = 0;
#ifdef WIN32
//			CreateDirectory(GenerateHelper::GetFilePath(strOutputFile).c_str() , NULL);
//			MakeSureDirectoryPathExists(GenerateHelper::GetFilePath(strOutputFile).c_str());
 			_mkdir(GenerateHelper::GetFilePath(strOutputFile).c_str()); 
#else
			nResult = mkdir(GenerateHelper::GetFilePath(strOutputFile).c_str(),S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
#endif  
			std::cout << "create director. " << strOutputFile << std::endl; 
		}

// #ifdef WIN32
// 		_unlink(strOutputFile.c_str());
// #else
// 		unlink(strOutputFile.c_str());
// #endif

		std::fstream f(strOutputFile.c_str(),std::ios::out);
		if(!f.good())
		{
			std::cout << "open file " << strOutputFile << " failed" << std::endl;
// 			f.open(strOutputFile.c_str());
		}

		for(UINT32 i = 0; i < vtStr.size(); ++i)
		{
			f.write(vtStr[i].c_str(),vtStr[i].length());
			f.write("\n",1);
		}

		f.close();
		return TRUE;
	}  
Ejemplo n.º 2
0
xpr_sint_t BatchCreate::runThread(xpr::Thread &aThread)
{
    CreateType sCreateType = getCreateType();

    xpr::string sPath;
    xpr::string sFileName;

    xpr_sint_t sInvalidItem = -1;
    xpr_bool_t sInvalid = XPR_FALSE;
    Status sStatus = StatusNone;

    xpr_size_t sFind;
    Item *sItem;
    Item *sItem2;
    NewDeque::iterator sIterator;

    typedef std::tr1::unordered_multimap<xpr::string, Item *> HashPathMap;
    typedef std::pair<HashPathMap::iterator, HashPathMap::iterator> HashPathPairIterator;
    HashPathMap sHashPathMap;
    HashPathMap::iterator sHashPathIterator;
    HashPathPairIterator sPairRangeIterator;

    sIterator = mNewDeque.begin();
    for (; sIterator != mNewDeque.end(); ++sIterator)
    {
        sItem = *sIterator;
        if (XPR_IS_NULL(sItem))
            continue;

        sPath = sItem->mPath;

        sPath.upper_case();

        sHashPathMap.insert(HashPathMap::value_type(sPath, sItem));

        {
            xpr::MutexGuard sLockGuard(mMutex);
            mPreparedCount++;
        }
    }

    {
        xpr::MutexGuard sLockGuard(mMutex);
        mStatus = StatusVerifying;
    }

    sIterator = mNewDeque.begin();
    for (; sIterator != mNewDeque.end(); ++sIterator)
    {
        sItem = *sIterator;

        XPR_ASSERT(sItem != XPR_NULL);

        if (mThread.isStop() == XPR_TRUE)
            break;

        sPath = sItem->mPath;
        if (sPath.empty() == XPR_TRUE)
            continue;

        sFileName.clear();

        sFind = sPath.rfind(XPR_STRING_LITERAL('\\'));
        if (sFind != xpr::string::npos)
            sFileName = sPath.substr(sFind+1);

        if (VerifyFileName(sFileName) == XPR_FALSE)
        {
            sItem->mResult = ResultInvalidName;
            sInvalidItem = (xpr_sint_t)std::distance(mNewDeque.begin(), sIterator);
            sInvalid = XPR_TRUE;
            break;
        }

        sPath.upper_case();
        sPairRangeIterator = sHashPathMap.equal_range(sPath);

        sHashPathIterator = sPairRangeIterator.first;
        for (; sHashPathIterator != sPairRangeIterator.second; ++sHashPathIterator)
        {
            sItem2 = sHashPathIterator->second;

            XPR_ASSERT(sItem2 != XPR_NULL);

            if (sItem != sItem2)
                break;
        }

        if (sHashPathIterator != sPairRangeIterator.second)
        {
            sItem->mResult = ResultEqualedName;
            sInvalidItem = (xpr_sint_t)std::distance(mNewDeque.begin(), sIterator);
            sInvalid = XPR_TRUE;
            break;
        }

        {
            xpr::MutexGuard sLockGuard(mMutex);
            mValidatedCount++;
        }
    }

    {
        xpr::MutexGuard sLockGuard(mMutex);
        mStatus = StatusCreating;
    }

    if (mThread.isStop() == XPR_FALSE && XPR_IS_FALSE(sInvalid))
    {
        xpr_rcode_t sRcode;
        xpr_sint_t sOpenMode = xpr::FileIo::OpenModeCreate | xpr::FileIo::OpenModeTruncate | xpr::FileIo::OpenModeWriteOnly;
        xpr::FileIo sFileIo;

        sIterator = mNewDeque.begin();
        for (; sIterator != mNewDeque.end(); ++sIterator)
        {
            sItem = *sIterator;

            XPR_ASSERT(sItem != XPR_NULL);

            if (mThread.isStop() == XPR_TRUE)
                break;

            sItem->mResult = ResultNone;

            if (IsExistFile(sItem->mPath) == XPR_FALSE)
            {
                switch (sCreateType)
                {
                case CreateTypeFolder:
                    {
                        ::CreateDirectory(sItem->mPath.c_str(), XPR_NULL);
                        break;
                    }

                case CreateTypeFile:
                case CreateTypeTextFile:
                    {
                        sRcode = sFileIo.open(sItem->mPath, sOpenMode);
                        if (XPR_RCODE_IS_SUCCESS(sRcode))
                            sFileIo.close();
                        break;
                    }
                }
            }

            if (sItem->mResult == ResultNone)
                sItem->mResult = ResultSucceeded;

            {
                xpr::MutexGuard sLockGuard(mMutex);
                mCreatedCount++;
            }
        }
    }

    sHashPathMap.clear();

    if (mThread.isStop() == XPR_TRUE)
    {
        sStatus = StatusStopped;
    }
    else
    {
        if (XPR_IS_TRUE(sInvalid))
            sStatus = StatusInvalid;
        else
            sStatus = StatusCreateCompleted;
    }

    {
        xpr::MutexGuard sLockGuard(mMutex);
        mInvalidItem = sInvalidItem;
        mStatus = sStatus;
    }

    ::PostMessage(mHwnd, mMsg, (WPARAM)XPR_NULL, (LPARAM)XPR_NULL);

    return 0;
}
Ejemplo n.º 3
0
/**
 * @ingroup TestWebRtc
 * @brief HTTP 요청 수신 이벤트 callback
 * @param pclsRequest		HTTP 요청 메시지
 * @param pclsResponse	HTTP 응답 메시지 - 응용에서 저장한다.
 * @returns 응용에서 HTTP 응답 메시지를 정상적으로 생성하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
 */
bool CHttpCallBack::RecvHttpRequest( CHttpMessage * pclsRequest, CHttpMessage * pclsResponse )
{
	std::string strPath = m_strDocumentRoot;
	std::string strExt;

	//CLog::Print( LOG_DEBUG, "req uri[%s]", pclsRequest->m_strReqUri.c_str() );

	// 보안상 .. 을 포함한 URL 을 무시한다.
	if( strstr( pclsRequest->m_strReqUri.c_str(), ".." ) )
	{
		pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
		return true;
	}

#ifdef _DEBUG
	// 메모리 누수 검사를 위해서 exit.html 을 수신하면 프로그램을 종료한다.
	if( !strcmp( pclsRequest->m_strReqUri.c_str(), "/exit.html" ) )
	{
		pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
		m_bStop = true;
		return true;
	}
#endif

	if( !strcmp( pclsRequest->m_strReqUri.c_str(), "/" ) )
	{
		CDirectory::AppendName( strPath, "index.html" );
	}
	else
	{
#ifdef WIN32
		ReplaceString( pclsRequest->m_strReqUri, "/", "\\" );
#endif

		strPath.append( pclsRequest->m_strReqUri );
	}

	if( IsExistFile( strPath.c_str() ) == false )
	{
		pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
		return true;
	}

	// 파일별 Content-Type 을 설정한다.
	GetFileExt( strPath.c_str(), strExt );
	const char * pszExt = strExt.c_str();
	
	if( !strcmp( pszExt, "html" ) || !strcmp( pszExt, "htm" ) )
	{
		pclsResponse->m_strContentType = "text/html";
	}
	else if( !strcmp( pszExt, "css" ) )
	{
		pclsResponse->m_strContentType = "text/css";
	}
	else if( !strcmp( pszExt, "js" ) )
	{
		pclsResponse->m_strContentType = "text/javascript";
	}
	else if( !strcmp( pszExt, "png" ) || !strcmp( pszExt, "gif" ) )
	{
		pclsResponse->m_strContentType = "image/";
		pclsResponse->m_strContentType.append( pszExt );
	}
	else if( !strcmp( pszExt, "jpg" ) || !strcmp( pszExt, "jpeg" ) )
	{
		pclsResponse->m_strContentType = "image/jpeg";
	}
	else
	{
		pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
		return true;
	}

	// 파일을 읽어서 HTTP body 에 저장한다.
	FILE * fd = fopen( strPath.c_str(), "rb" );
	if( fd == NULL )
	{
		pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
		return true;
	}

	int n;
	char szBuf[8192];

	while( ( n = fread( szBuf, 1, sizeof(szBuf), fd ) ) > 0 )
	{
		pclsResponse->m_strBody.append( szBuf, n );
	}

	fclose( fd );

	pclsResponse->m_iStatusCode = HTTP_OK;

	return true;
}