BinaryInfo* readFile(char* fileName) {
    FILE *fp = fopen(fileName, "rb");
    if (fp == NULL) {
        LOGE("\treadFile() file open fail");
        return NULL;
    }

    fseek(fp, 0, SEEK_END);
    int length = ftell(fp);
#ifdef DEBUG
    LOGI("\treadFile() length=%d", length);
#endif
    fseek(fp, 0, SEEK_SET);

    void* binary = (GLvoid*) malloc(length);
    if (binary == NULL) {
        LOGE("\treadFile() malloc fail");
        return NULL;
    }

    ssize_t size = fread(binary, 1, length, fp);

    if (size == -1) {
        LOGE("\treadFile() file read fail %d", size);
        free(binary);
        return NULL;
    }

    fclose(fp);

    char* name = copyFileName(fileName);

    BinaryInfo* binaryInfo = (BinaryInfo*) malloc(sizeof(BinaryInfo));
    if (binaryInfo != NULL) {
        binaryInfo->length = length;
        binaryInfo->binary = binary;
        binaryInfo->name = name;
    }

    return binaryInfo;
}
示例#2
0
文件: indexer.c 项目: rshnn/indexer
/*
	process Token will add the (token, filename) tuple to the TokenList object.
		Duplicate (token, filename) will increment frequency.
		New tuples will create new objects to be added to the nested sorted-lists.

	@param word 	: Token to be added
	@param filename : Filename associated with word
*/
void processToken(char* word, char* filename)
{
	Token 		*searchToken;
	fileRecord 	*searchRecord;
	Token   	*newToken 	= malloc(sizeof(Token));
	fileRecord  *newRecord 	= malloc(sizeof(fileRecord));
	
	if (newRecord == NULL || newToken == NULL) {
		fprintf(stderr, "errno %d\n", errno);
		fprintf(stderr, "mesg: %s\n", strerror(errno));
		exit(-1);
	}

	newToken->word    	 	= word;
	newToken->fileList   	= NULL;
	newRecord->filename 	= copyFileName(filename);
	newRecord->frequency	= 1;

	if( (searchToken = SLSearch(TokenList, newToken, compareRecordName)) != NULL ){

		if( (searchRecord = SLSearch(searchToken->fileList, newRecord, compareRecordName)) != NULL ){
			
			int freq = searchRecord->frequency;
			newRecord->frequency = freq+1;
			SLInsert(searchToken->fileList, newRecord);
			SLRemove(searchToken->fileList, searchRecord);			

		}
		else{
			SLInsert(searchToken->fileList, newRecord);
		}
		destroyTokenStruct(newToken);
	}
	else{
		newToken->fileList = SLCreate(compareRecordStructs, destroyFileRecord);
		SLInsert(newToken->fileList, newRecord);
		SLInsert(TokenList, newToken);
	}

}
// To handle servers with customized mpq files, try to read Patch_D2.mpq using Stormlib
// (http://www.zezula.net/en/mpq/stormlib.html). We load the StormLib dll with LoadLibrary
// to avoid imposing any run- or compile-time dependencies on the user. If we can't load
// the dll or read the mpq, we will fall back on a hard-coded list of the standard items.
//
// We do all this in the injector and write the info to a temp file because of problems
// calling LoadLibrary in the injected dll.
// Update: Can now load the dll from BH.dll, so no need to write to external files anymore
bool ReadMPQFiles(std::string fileName) {
	int successfulFileCount = 0, desiredFileCount = 0;
	HMODULE dllHandle = LoadLibrary((BH::path + "StormLib.dll").c_str());
	if (dllHandle) {
		SFileOpenArchive = (MPQOpenArchive)GetProcAddress(dllHandle, "SFileOpenArchive");
		SFileCloseArchive = (MPQCloseArchive)GetProcAddress(dllHandle, "SFileCloseArchive");
		SFileOpenFileEx = (MPQOpenFile)GetProcAddress(dllHandle, "SFileOpenFileEx");
		SFileGetFileSize = (MPQGetSize)GetProcAddress(dllHandle, "SFileGetFileSize");
		SFileReadFile = (MPQReadFile)GetProcAddress(dllHandle, "SFileReadFile");
		SFileCloseFile = (MPQCloseFile)GetProcAddress(dllHandle, "SFileCloseFile");

		HANDLE pMutex = CreateMutex(NULL, true, "Global\\BH_PATCH_D2_MPQ_MUTEX");
		WaitForSingleObject(
			pMutex,    // handle to mutex
			INFINITE);  // no time-out interval

		if (SFileOpenArchive && SFileCloseArchive && SFileOpenFileEx && SFileCloseFile && SFileGetFileSize && SFileReadFile) {
			// Copy the MPQ file to avoid sharing access violations
			std::string copyFileName(fileName);
			size_t start_pos = copyFileName.find("Patch_D2.mpq");
			if (start_pos != std::string::npos) {
				copyFileName.replace(start_pos, 12, "Patch_D2.copy.mpq");
			}

			std::ifstream src(fileName.c_str(), std::ios::binary);
			std::ofstream dst(copyFileName.c_str(), std::ios::binary);
			dst << src.rdbuf();
			dst.close();
			src.close();

			MPQArchive archive(copyFileName.c_str());

			const int NUM_MPQS = 16;
			std::string mpqFiles[NUM_MPQS] = {
				"UniqueItems",
				"Armor",
				"Weapons",
				"Misc",
				"ItemTypes",
				"ItemStatCost",
				"Inventory",
				"Properties",
				"Runes",
				"SetItems",
				"skills",
				"MagicPrefix",
				"MagicSuffix",
				"RarePrefix",
				"RareSuffix",
				"CharStats"
			};
			if (archive.error == ERROR_SUCCESS) {
				for (int i = 0; i < NUM_MPQS; i++){
					std::string path = "data\\global\\excel\\" + mpqFiles[i] + ".txt";
					MPQFile mpqFile(&archive, path.c_str()); desiredFileCount++;
					if (mpqFile.error == ERROR_SUCCESS) {
						successfulFileCount++;
						std::string key = mpqFiles[i];
						std::transform(key.begin(), key.end(), key.begin(), ::tolower);
						MpqDataMap[key] = new MPQData(&mpqFile);
					}
				}
			}
			// read mpq version
			std::string path = "data\\version.txt";
			MPQFile mpqFile(&archive, path.c_str());
			if (mpqFile.error == ERROR_SUCCESS) {
				MPQData mpqversion(&mpqFile);
				MpqVersion = mpqversion.fields[0];
			}
		}
		FreeLibrary(dllHandle);

		ReleaseMutex(pMutex);
		CloseHandle(pMutex);
	}
	return true;
}
int JNICALL Java_com_gomdev_gles_GLESShader_nRetrieveProgramBinary
(JNIEnv * env, jobject obj, jint program, jstring str)
{
    GLint binaryLength;
    GLvoid* binary;
    FILE* outfile;
    GLenum binaryFormat;

    const char* fileName = env->GetStringUTFChars(str, NULL);
    if(fileName != NULL)
    {
        glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, &binaryLength);

        checkGLError("retrieve() glGetProgramiv");

#ifdef DEBUG
        LOGI("retrieve() binaryLength=%d", binaryLength);
#endif
        binary = (GLvoid*)malloc(binaryLength);
        if(binary == NULL)
        {
            LOGE("nRetrieveProgramBinary() malloc fail");
        }

//        if (glGetProgramBinaryOES == NULL) {
//            glGetProgramBinaryOES = (PFNGLGETPROGRAMBINARYOESPROC) eglGetProcAddress("glGetProgramBinaryOES");
//        }
        glGetProgramBinary(program, binaryLength, NULL, &binaryFormat, binary);

        checkGLError("retrieve() glGetProgramBinaryOES");

        outfile = fopen(fileName, "wb");
        if(outfile == NULL)
        {
            LOGE("nRetrieveProgramBinary() fileName=%s", fileName);
            LOGE("nRetrieveProgramBinary() fopen error");
            free(binary);
            return 0;
        }
        fwrite(binary, binaryLength, 1, outfile);
        fclose(outfile);

        // if binary is already cached, remove cached binary
        char* name = copyFileName(fileName);

        BinaryInfo* info = find(name);
        if (info != NULL) {
            removeBinaryFromList(info, true);
        }

        BinaryInfo* binaryInfo = (BinaryInfo*)malloc(sizeof(BinaryInfo));
        if (binaryInfo != NULL ) {
            binaryInfo->name = name;
            binaryInfo->length = binaryLength;
            binaryInfo->binary = binary;

            add(binaryInfo);
        }

        env->ReleaseStringUTFChars(str, fileName);
    }

    sBinaryFormat = binaryFormat;

    return binaryFormat;
}