Exemple #1
0
    int function_dispatcher(lua_State* L)
    {
        function_rep* rep = static_cast<function_rep*>(
            lua_touserdata(L, lua_upvalueindex(1))
        );

        bool ambiguous = false;
        int min_match = std::numeric_limits<int>::max();
        int match_index = -1;
        bool ret;

#ifdef LUABIND_NO_ERROR_CHECKING2
        if (rep->overloads().size() == 1)
        {
            match_index = 0;
        }
        else
        {
#endif
            int num_params = lua_gettop(L);
            ret = find_best_match(
                L
              , &rep->overloads().front()
              , (int)rep->overloads().size()
              , sizeof(overload_rep)
              , ambiguous
              , min_match
              , match_index
              , num_params
            );
#ifdef LUABIND_NO_ERROR_CHECKING2
        }
#else
        if (!ret)
        {
            // this bock is needed to make sure the string_class is destructed
            {
                string_class msg = "no match for function call '";
                msg += rep->name();
                msg += "' with the parameters (";
                msg += stack_content_by_name(L, 1);
                msg += ")\ncandidates are:\n";

                msg += get_overload_signatures(
                    L
                  , rep->overloads().begin()
                  , rep->overloads().end()
                  , rep->name()
                );

                lua_pushstring(L, msg.c_str());
            }

            lua_error(L);
        }

        if (ambiguous)
        {
            // this bock is needed to make sure the string_class is destructed
            {
                string_class msg = "call of overloaded function '";
                msg += rep->name();
                msg += "(";
                msg += stack_content_by_name(L, 1);
                msg += ") is ambiguous\nnone of the overloads "
                       "have a best conversion:";

                std::vector<overload_rep_base const*> candidates;
                find_exact_match(
                    L
                  , &rep->overloads().front()
                  , (int)rep->overloads().size()
                  , sizeof(overload_rep)
                  , min_match
                  , num_params
                  , candidates
                );

                msg += get_overload_signatures_candidates(
                    L
                  , candidates.begin()
                  , candidates.end()
                  , rep->name()
                );

                lua_pushstring(L, msg.c_str());
            }
            lua_error(L);
        }
#endif
        overload_rep const& ov_rep = rep->overloads()[match_index];

#ifndef LUABIND_NO_EXCEPTIONS
        try
        {
#endif
            return ov_rep.call(L, ov_rep.fun);
#ifndef LUABIND_NO_EXCEPTIONS
        }
        catch(const luabind::error&)
        {
        }
        catch(const std::exception& e)
        {
            lua_pushstring(L, e.what());
        }
        catch (const char* s)
        {
            lua_pushstring(L, s);
        }
        catch(...)
        {
            string_class msg = rep->name();
            msg += "() threw an exception";
            lua_pushstring(L, msg.c_str());
        }
        // we can only reach this line if an exception was thrown
        lua_error(L);
        return 0; // will never be reached
#endif
    }
//
// Supported commands are:
//	<dict.file> e <word> - find exact match, if nothing found, dump list
//	<dict.file> l <word> - dump list
//  <dict.file> n <offset> - dump next word list starting from <offset>
//  <dict.file> p <offset> - dump previous word list, ending at offset <offset>
//  <dict.file> x <offset> - dump article at address x
//
// Output format:
//	list result
//		"list"
//		<word>\t<short translation>\n
//		<word>\t<short translation>\n
//		...
//		<starting_offset>\t<ending_offset>\n
//
//	exact result
//		"match"\n
//		<translation>
//			
int main(int argc, char* argv[])
{
	// Set console codepage to UTF8 on windows
	#ifdef _MSC_VER
		SetConsoleOutputCP(CP_UTF8);
	#endif


	// Expecting <exec name> <dictionary file> <word>
	if (argc < 4) {
		print_usage();
		return ERR_INVALID_ARGUMENT;
	}

	char* command = (char*) argv[2];

	// dictionary file name, fancy names aren't supported
	char* filename = (char*) argv[1];

	// UTF 16 version of the search string (assuming input is UTF8)
	int search_len;
	uint16_t* searchUTF16 = utf8to16((uint8_t*) argv[3], search_len);
	
	// Open dictionary file
	f = fopen(filename, "rb");
	if (!f) {
		printf("Failed to open file: %s", filename);
		return ERR_FILE_NOT_FOUND;
	}

	// Read header
	doread(&header, sizeof(header));

	// check magic (PRSPDICT ascii)
	int MAGIC[8] = {0x50, 0x52, 0x53, 0x50, 0x44, 0x49, 0x43, 0x54};
	for (int i = 0; i < 8; i++) {
		if (header.magic[i] != MAGIC[i]) {
			printf("Invalid file magic");
			return ERR_INVALID_MAGIC;
		}
	}

	// Check dicitonary version
	if (header.version_lo != 0 || header.version_hi != 1) {
		printf("Unsupported dictionary version: %d.%d", header.version_hi, header.version_lo);
		return ERR_UNSUPPORTED_VERSION;
	}

	switch (command[0]) {
		case 'e':
			//	e <word> - find exact match, if nothing found, dump list
			if (!find_exact_match(header.offset_radix, searchUTF16, search_len)) {
				find_best_match(searchUTF16, search_len);
			}
			break;
		//	l <word> - dump list
		case 'l':
			find_best_match(searchUTF16, search_len);
			break;
		//  n <offset> - dump next word list starting from <offset>
		case 'n':
			dump_word_list(atoi(argv[3]), NEXT);
			break;
		//  p <offset> - dump previous word list, ending at offset <offset>
		case 'p':
			dump_word_list(atoi(argv[3]), PREV);
			break;
		//  x <offset> - dump article at address x
		case 'x':
			dump_article(atoi(argv[3]));
			break;
		default:
			print_usage();
	}

	
	fclose(f);
	return 0;
}