src/joystick/SDL_gamecontroller.c
changeset 8042 7863121cb7d5
parent 8009 c185ac7fb771
child 8046 e7c89b29932a
     1.1 --- a/src/joystick/SDL_gamecontroller.c	Mon Dec 02 10:08:57 2013 -0300
     1.2 +++ b/src/joystick/SDL_gamecontroller.c	Mon Dec 02 19:34:08 2013 -0300
     1.3 @@ -33,6 +33,7 @@
     1.4  #endif
     1.5  #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
     1.6  
     1.7 +#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
     1.8  
     1.9  /* a list of currently opened game controllers */
    1.10  static SDL_GameController *SDL_gamecontrollers = NULL;
    1.11 @@ -657,6 +658,73 @@
    1.12   * Add or update an entry into the Mappings Database
    1.13   */
    1.14  int
    1.15 +SDL_GameControllerAddMappingsFromFile( const char* mapDB )
    1.16 +{
    1.17 +    const char *platform = SDL_GetPlatform();
    1.18 +    SDL_RWops *rw;
    1.19 +    int controllers = 0;
    1.20 +    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
    1.21 +    size_t db_size, platform_len;
    1.22 +    
    1.23 +    rw = SDL_RWFromFile(mapDB, "rb");
    1.24 +    if (rw == NULL) {
    1.25 +        return SDL_SetError("Could not open %s", mapDB);
    1.26 +    }
    1.27 +    db_size = SDL_RWsize(rw);
    1.28 +    
    1.29 +    buf = (char *) SDL_malloc(db_size + 1);
    1.30 +    if (buf == NULL) {
    1.31 +        SDL_RWclose(rw);
    1.32 +        return SDL_SetError("Could allocate space to not read DB into memory");
    1.33 +    }
    1.34 +    
    1.35 +    if (SDL_RWread(rw, buf, db_size, 1) != 1) {
    1.36 +        SDL_RWclose(rw);
    1.37 +        SDL_free(buf);
    1.38 +        return SDL_SetError("Could not read DB");
    1.39 +    }
    1.40 +    SDL_RWclose(rw);
    1.41 +    
    1.42 +    buf[db_size] = '\0';
    1.43 +    line = buf;
    1.44 +    
    1.45 +    while (line < buf + db_size) {
    1.46 +        line_end = SDL_strchr( line, '\n' );
    1.47 +        if (line_end != NULL) {
    1.48 +            *line_end = '\0';
    1.49 +        }
    1.50 +        else {
    1.51 +            line_end = buf + db_size;
    1.52 +        }
    1.53 +        
    1.54 +        /* Extract and verify the platform */
    1.55 +        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
    1.56 +        if ( tmp != NULL ) {
    1.57 +            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
    1.58 +            comma = SDL_strchr(tmp, ',');
    1.59 +            if (comma != NULL) {
    1.60 +                platform_len = comma - tmp + 1;
    1.61 +                if (platform_len + 1 < SDL_arraysize(line_platform)) {
    1.62 +                    SDL_strlcpy(line_platform, tmp, platform_len);
    1.63 +                    if(SDL_strncasecmp(line_platform, platform, platform_len) == 0
    1.64 +                        && SDL_GameControllerAddMapping(line) > 0) {
    1.65 +                        controllers++;
    1.66 +                    }
    1.67 +                }
    1.68 +            }
    1.69 +        }
    1.70 +        
    1.71 +        line = line_end + 1;
    1.72 +    }
    1.73 +
    1.74 +    SDL_free(buf);
    1.75 +    return controllers;
    1.76 +}
    1.77 +
    1.78 +/*
    1.79 + * Add or update an entry into the Mappings Database
    1.80 + */
    1.81 +int
    1.82  SDL_GameControllerAddMapping( const char *mappingString )
    1.83  {
    1.84      char *pchGUID;