bool process_file(const char* file_name, bool is_std_file,
	Array* file_stack, Array* file_records,
	DoubleList* lines, DoubleList* file_binarys)
{
#if MG_PLATFORM_WINDOWS
	const char* header_search_dirs[] = {
		"D:/Microsoft Visual Studio 12.0/VC/crt/src",
		"D:/Microsoft Visual Studio 12.0/VC/include",
		"D:/Microsoft Visual Studio 12.0/VC/atlmfc/include",
		"C:/Program Files (x86)/Windows Kits/8.1/Include/um",
		"C:/Program Files (x86)/Windows Kits/8.1/Include/shared",
		"C:/Program Files (x86)/Windows Kits/8.1/Include/winrt",
	};
#else
	const char* header_search_dirs[] = {
		"/usr/include",
		"/usr/include/x86_64-linux-gnu",
		"/usr/include/linux",
		"/usr/local/include",
		"/usr/lib/gcc/x86_64-linux-gnu/4.8/include",
	};
#endif
	char* bytes = NULL;
	long byte_num = 0;
	size_t std_search_path_num = sizeof(header_search_dirs) / sizeof(char*);
	size_t index = 0;
	size_t file_path_len;
	char* p_file_path;
	char** pp_file_path;
	char file_path[MG_MAX_PATH];
	bool is_file_exist = false;

	// check file name length
	if (strlen(file_name) == 0)
	{
		MLOG("file name is null\n");
		MASSERT_MSG(0, "file name is null!\n");
		return false;
	}

	// ensure file is exist
	memset(file_path, 0, MG_MAX_PATH);
	sprintf_s(file_path, MG_MAX_PATH - 1, "%s", file_name);
	is_file_exist = File_IsExist(file_path);
	if (!is_file_exist)
	{
		if (is_std_file)
		{
			// try absolute path in std folder
			is_file_exist = SearchInStdFolder(file_name, std_search_path_num, header_search_dirs, file_path);

			// try absolute path in current process path
			if (!is_file_exist)
			{
				File_GetAbsolutePath(file_name, file_path);
				is_file_exist = File_IsExist(file_path);
			}
		}
		else
		{
			// try absolute path in current process path
			File_GetAbsolutePath(file_name, file_path);
			is_file_exist = File_IsExist(file_path);

			// try absolute path in std folder
			if (!is_file_exist)
			{
				is_file_exist = SearchInStdFolder(file_name, std_search_path_num, header_search_dirs, file_path);
			}
		}

		if (!is_file_exist)
		{
			MLOG("file %s is not exsit\n", file_name);
			MASSERT_MSG(0, "file is not exsit\n");
			return false;
		}
	}

	// detect file repeat in one step
	for (index = 0; index < ArrayUsed(file_stack); ++index)
	{
		char** p_address = (char**)ArrayGet(file_stack, index);
		if (strncmp(*p_address, file_path, MG_MAX_PATH) == 0)
		{
			return false;
		}
	}

	// detect file repeat in all step
	for (index = 0; index < ArrayUsed(file_records); ++index)
	{
		char** p_address = (char**)ArrayGet(file_records, index);
		if (strncmp(*p_address, file_path, MG_MAX_PATH) == 0)
		{
			return true;
		}
	}

	// read file
	if (!File_Read(file_path, &bytes, &byte_num))
	{
		MLOG("file %s is not exsit\n", file_path);
		MASSERT_MSG(0, "failed in read file\n");
		return false;
	}

	// push file path into file stack
	file_path_len = strlen(file_path) + 1;
	p_file_path = (char*)malloc(sizeof(char) * file_path_len);
	memcpy(p_file_path, file_path, file_path_len);
	ArrayPush(file_stack, &p_file_path);

	// push file path into file records
	p_file_path = (char*)malloc(sizeof(char) * file_path_len);
	memcpy(p_file_path, file_path, file_path_len);
	ArrayPush(file_records, &p_file_path);

	// save bytes
	DoubleListAdd(file_binarys, &bytes);
	if (byte_num == 0)
	{
		return true;
	}

	// split into lines
	SplitLines(bytes, lines);

	// parse each line
	if (!parse_lines(file_stack, file_records, lines, file_binarys))
	{
		MASSERT_MSG(0, "failed in parse lines");
		MLOG("failed in parse lines");
		return false;
	}

	// pop this file
	pp_file_path = ArrayGet(file_stack, ArrayUsed(file_stack) - 1);
	MASSERT(strncmp(*pp_file_path, p_file_path, MG_MAX_PATH) == 0);
	if (strncmp(*pp_file_path, p_file_path, MG_MAX_PATH) != 0)
	{
		return false;
	}
	free(*pp_file_path);
	ArrayPop(file_stack);

	return true;
}
Ejemplo n.º 2
0
int LS_jam_parse(ls_lua_State *L)
{
    int numParams = ls_lua_gettop(L);
    if (numParams < 1  ||  numParams > 1)
        return 0;

    if (!ls_lua_isstring(L, 1))
        return 0;

    {
        const char* src = ls_lua_tostring(L, 1);
        const char* ptr = src;
        const char* startPtr;
        char** lines;
        int numberOfLines = 1;
        int status;

        while (*ptr) {
            if (*ptr == '\n') {
                ++numberOfLines;
            }
            ++ptr;
        }
        lines = malloc(sizeof(char*) * (numberOfLines + 1));
        numberOfLines = 0;
        startPtr = ptr = src;
        while (1) {
            if (*ptr == '\n'  ||  *ptr == 0) {
                char* line;
                if (*ptr == '\n')
                    ++ptr;
                line = malloc(ptr - startPtr + 1);
                memcpy(line, startPtr, ptr - startPtr);
                line[ptr - startPtr] = 0;
                startPtr = ptr;
                lines[numberOfLines++] = line;
                if (*ptr == 0)
                    break;
            } else {
                ++ptr;
            }
        }
        lines[numberOfLines] = 0;
        parse_lines(lines[0], lines);

        status = yyanyerrors();

        while (numberOfLines > 0) {
            free(lines[--numberOfLines]);
        }
        free(lines);

        if (status) {
            struct ls_lua_Debug ar;
            if (ls_lua_getstack(L, 1, &ar)) {
                ls_lua_getinfo(L, "nSl", &ar);
            }

            printf("jam: Error parsing Jam code near %s[%d].\n", ar.short_src, ar.currentline);
            exit(EXITBAD);
        }
    }

    return 0;
}