Esempio n. 1
0
def get_string_int_map(map_func, hash_func, string_map, case_sensitive=True):
    strings = list(string_map.iterkeys())
    writer = CodeWriter()
    if strings:
        hash_data = get_hash_function(hash_func, strings, case_sensitive)
        writer.putlnc(hash_data.code)
        hashes = dict((v, k) for (k, v) in hash_data.strings.iteritems())

    writer.putmeth('int %s' % map_func, 'const std::string & in')
    writer.putln('if (in.empty())')
    writer.indent()
    writer.putln('return -1;')
    writer.dedent()

    if strings:
        writer.putlnc('unsigned int hash = %s(&in[0], in.size());', hash_func)
        writer.putlnc('switch (hash) {')
        writer.indent()
        for i in xrange(hash_data.max_hash_value + 1):
            value = hashes.get(i, None)
            if value is None:
                continue
            else:
                value = string_map[value]
            writer.putlnc('case %s: return %s;', i, value)
        writer.end_brace()
    writer.putlnc('return -1;')
    writer.end_brace()
    return writer.get_data()
Esempio n. 2
0
def get_string_int_map(map_func, hash_func, string_map, case_sensitive=True):
    strings = list(string_map.iterkeys())
    writer = CodeWriter()
    if strings:
        hash_data = get_hash_function(hash_func, strings, case_sensitive)
        writer.putlnc(hash_data.code)
        hashes = dict((v, k) for (k, v) in hash_data.strings.iteritems())

    writer.putmeth('int %s' % map_func, 'const std::string & in')
    writer.putln('if (in.empty())')
    writer.indent()
    writer.putln('return -1;')
    writer.dedent()

    if strings:
        writer.putlnc('unsigned int hash = %s(&in[0], in.size());',
                      hash_func)
        writer.putlnc('switch (hash) {')
        writer.indent()
        for i in xrange(hash_data.max_hash_value + 1):
            value = hashes.get(i, None)
            if value is None:
                continue
            else:
                value = string_map[value]
            writer.putlnc('case %s: return %s;', i, value)
        writer.end_brace()
    writer.putlnc('return -1;')
    writer.end_brace()
    return writer.get_data()
Esempio n. 3
0
def main():
    import sys
    sys.path.append('..')
    sys.path.append('../..')
    from chowdren.codewriter import CodeWriter
    writer = CodeWriter('buttonmap.cpp')
    writer.putmeth('int remap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', f, t)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()

    writer.putmeth('int unremap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', t, f)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()
    writer.close()
Esempio n. 4
0
def main():
    import sys
    sys.path.append('..')
    sys.path.append('../..')
    from chowdren.codewriter import CodeWriter
    writer = CodeWriter('buttonmap.cpp')
    writer.putmeth('int remap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', f, t)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()

    writer.putmeth('int unremap_button', 'int n')
    for k, v in maps.iteritems():
        writer.putraw('#ifdef %s' % k)
        writer.putlnc('switch (n) {')
        writer.indent()
        for f, t in v:
            writer.putlnc('case %s: return %s;', t, f)
        writer.end_brace()
        writer.putraw('#endif')
    writer.putlnc('return n;')
    writer.end_brace()
    writer.close()
Esempio n. 5
0
def main():
    # generate key definition files
    import sys
    sys.path.append('..')
    from chowdren.codewriter import CodeWriter
    from chowdren.common import get_base_path
    from chowdren.stringhash import get_string_int_map
    import os

    writer = CodeWriter(os.path.join(get_base_path(), 'keyconv.cpp'))

    # write keys file
    writer.putln('#include <string>')
    writer.putln('#include "keydef.h"')
    writer.putln('#include "stringcommon.h"')
    writer.putln('')

    writer.putmeth('int translate_vk_to_key', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        writer.putln('case %s: return %s;' % (vk, name[0]))
    writer.end_brace()
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    string_map = {}
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        string_map[string_name] = name[0]

    writer.putln(
        get_string_int_map('translate_string_to_key', 'get_key_string_hash',
                           string_map, False))

    writer.putmeth('std::string translate_vk_to_string', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        writer.putlnc('case %s: return %r;', vk, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('std::string translate_key_to_string', 'int key')
    writer.putln('switch (key) {')
    writer.indent()
    for name, string_name in KEY_TO_NAME.iteritems():
        writer.putlnc('case %s: return %r;', name, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()

    # keys = {}
    # for name, value in SDL_KEYS.iteritems():
    #     if isinstance(value, str):
    #         value = ord(value)
    #     keys[value] = name

    # writer.putmeth('int translate_key_to_index', 'int key')
    # writer.putln('switch (key) {')
    # writer.indent()
    # for index, value in enumerate(sorted(keys)):
    #     name = keys[value]
    #     writer.putlnc('case %s: return %r;', name, index)
    # writer.end_brace()
    # writer.putln('return -1;')
    # writer.end_brace()

    writer.close()
Esempio n. 6
0
def main():
    # generate key definition files
    import sys
    sys.path.append('..')
    from chowdren.codewriter import CodeWriter
    from chowdren.common import get_base_path
    from chowdren.stringhash import get_string_int_map
    import os

    writer = CodeWriter(os.path.join(get_base_path(), 'keyconv.cpp'))

    # write keys file
    writer.putln('#include "chowstring.h"')
    writer.putln('#include "keydef.h"')
    writer.putln('#include "stringcommon.h"')
    writer.putln('')

    writer.putmeth('int translate_vk_to_key', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        writer.putln('case %s: return %s;' % (vk, name[0]))
    writer.end_brace()
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    string_map = {}
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        string_map[string_name] = name[0]

    writer.putln(get_string_int_map('translate_string_to_key',
                                    'get_key_string_hash',
                                    string_map, False))

    writer.putmeth('chowstring translate_vk_to_string',
                   'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        writer.putlnc('case %s: return %r;', vk, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('chowstring translate_key_to_string',
                   'int key')
    writer.putln('switch (key) {')
    writer.indent()
    for name, string_name in KEY_TO_NAME.iteritems():
        writer.putlnc('case %s: return %r;', name, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()

    # keys = {}
    # for name, value in SDL_KEYS.iteritems():
    #     if isinstance(value, str):
    #         value = ord(value)
    #     keys[value] = name

    # writer.putmeth('int translate_key_to_index', 'int key')
    # writer.putln('switch (key) {')
    # writer.indent()
    # for index, value in enumerate(sorted(keys)):
    #     name = keys[value]
    #     writer.putlnc('case %s: return %r;', name, index)
    # writer.end_brace()
    # writer.putln('return -1;')
    # writer.end_brace()

    writer.close()
Esempio n. 7
0
def main():
    # generate key definition files
    from chowdren.codewriter import CodeWriter
    from chowdren.common import get_base_path
    import os

    writer = CodeWriter(os.path.join(get_base_path(), 'keyconv.cpp'))

    # write keys file
    writer.putln('#include <string>')
    writer.putln('#include "keydef.h"')
    writer.putln('#include "stringcommon.h"')
    writer.putln('')

    writer.putmeth('int translate_vk_to_key', 'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        writer.putln('case %s: return %s;' % (vk, name[0]))
    writer.end_brace()
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('int translate_string_to_key',
                   'const std::string & in_name')
    writer.putln('std::string name = in_name;')
    writer.putln('to_lower(name);')
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        string_name = string_name.lower()
        writer.putlnc('if (name.compare(0, %s, %r) == 0) return %s;',
                      len(string_name), string_name, name[0], cpp=False)
    writer.putln('return -1;')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('std::string translate_vk_to_string',
                   'int vk')
    writer.putln('switch (vk) {')
    writer.indent()
    for vk, name in VK_TO_SDL.iteritems():
        string_name = VK_TO_NAME.get(vk, None)
        if string_name is None:
            continue
        writer.putlnc('case %s: return %r;', vk, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()
    writer.putln('')

    writer.putmeth('std::string translate_key_to_string',
                   'int key')
    writer.putln('switch (key) {')
    writer.indent()
    for name, string_name in KEY_TO_NAME.iteritems():
        writer.putlnc('case %s: return %r;', name, string_name)
    writer.end_brace()
    writer.putln('return "";')
    writer.end_brace()

    writer.close()