示例#1
0
	char* Workspace::ReadFileToBuffer(const char* file, uint32_t& bufferSize)
    {
        IFile* fp = behaviac::CFileManager::GetInstance()->FileOpen(file, CFileSystem::EOpenAccess_Read);

        if (!fp)
        {
            return 0;
        }

        //fp->Seek(0, CFileSystem::ESeekMoveMode_End);
        uint32_t fileSize = (uint32_t)fp->GetSize();

		bufferSize = fileSize + 1;

		char* pBuffer = 0;

		for (int i = 0; i < kFileBuffers; ++i) {
			FileBuffer_t& fileBuffer = this->m_fileBuffers[i];
			BEHAVIAC_ASSERT(fileBuffer.offset == 0 || fileBuffer.offset < fileBuffer.length);

			if (fileBuffer.start == 0) {
				//to allocate extra 10k
				int fileBufferLength = bufferSize + 10 * 1024;

				const int kBufferLength = 100 * 1024;

				if (fileBufferLength < kBufferLength) {
					fileBufferLength = kBufferLength;
				}

				fileBuffer.start = (char*)BEHAVIAC_MALLOC(fileBufferLength);
				fileBuffer.length = fileBufferLength;
				BEHAVIAC_ASSERT(fileBuffer.offset == 0);

				pBuffer = fileBuffer.start;
				fileBuffer.offset += bufferSize;
				BEHAVIAC_ASSERT(fileBuffer.offset < fileBuffer.length);

				break;
			}
			else if (bufferSize  < fileBuffer.length - fileBuffer.offset) {
				pBuffer = fileBuffer.start + fileBuffer.offset;
				fileBuffer.offset += bufferSize;
				BEHAVIAC_ASSERT(fileBuffer.offset < fileBuffer.length);

				break;
			}
		}

		BEHAVIAC_ASSERT(pBuffer);

        fp->Read(pBuffer, sizeof(char) * fileSize);
        pBuffer[fileSize] = 0;

        behaviac::CFileManager::GetInstance()->FileClose(fp);

        return pBuffer;
    }
示例#2
0
	void CStringID::SetContent(const char* content, bool noCase, bool resolve)
	{
		BEHAVIAC_UNUSED_VAR(resolve);

#ifdef BEHAVIAC_STRINGID_DEBUG_CASE
		m_noCase = noCase;
#endif
#if BEHAVIAC_STRINGID_USESTRINGCONTENT
		static behaviac::Mutex ms_critialsection;
#ifdef BEHAVIAC_STRINGID_RESOLVE_CONTENT

		if (resolve)
		{
			ms_critialsection.Lock();
			StringIdDictionary::iterator it = GetContentDictionary().find(m_value);

			if (it != GetContentDictionary().end())
			{
				m_content = (*it).second;
			}

			ms_critialsection.Unlock();
			return;
		}

#endif
#endif

		if (content == NULL || content[0] == '\0')
		{
			m_value = InvalidID;
#if BEHAVIAC_STRINGID_USESTRINGCONTENT
			m_content = NULL;
#endif

		}
		else
		{
			if (noCase)
			{
				BEHAVIAC_ASSERT(false);
				m_value = CRC32::CalcCRCNoCase(content);

			}
			else
			{
				m_value = CRC32::CalcCRC(content);
			}

#if BEHAVIAC_STRINGID_USESTRINGCONTENT
			ms_critialsection.Lock();
			StringIdDictionary::iterator it = GetContentDictionary().find(m_value);

			if (it != GetContentDictionary().end())
			{
				if (noCase)
				{
					if (string_icmp(content, (*it).second))
					{
						BEHAVIAC_LOG3(BEHAVIAC_LOG_INFO, "There is a conflict between two StringID for the CRC 0x%08X.\n%s\n%s\n\nThe result of that is unpredictable but will probably crash.", m_value, content, (*it).second);
					}
				}
				else
				{
					if (strcmp(content, (*it).second))
					{
						BEHAVIAC_LOG3(BEHAVIAC_LOG_INFO, "There is a conflict between two StringID for the CRC 0x%08X.\n%s\n%s\n\nThe result of that is unpredictable but will probably crash.", m_value, content, (*it).second);
					}
				}

				m_content = (*it).second;

			}
			else
			{
				uint32_t len = strlen(content);
				char* str = (char*)BEHAVIAC_MALLOC(sizeof(char) * (len + 1));
				string_cpy(str, content);

				if (noCase)
				{
					for (uint32_t i = 0; i < len; ++i)
					{
						str[i] = (char)tolower(str[i]);
					}
				}

				GetContentDictionary()[m_value] = str;
				m_content = str;
			}

			ms_critialsection.Unlock();
#endif
		}
	}