Main.cpp revision 9065504a63d6bf37bf621191fda1d1fe4da76ee3
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ui/KeyCharacterMap.h>
18#include <ui/KeyLayoutMap.h>
19#include <ui/VirtualKeyMap.h>
20#include <utils/String8.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26using namespace android;
27
28static const char* gProgName = "validatekeymaps";
29
30enum FileType {
31    FILETYPE_UNKNOWN,
32    FILETYPE_KEYLAYOUT,
33    FILETYPE_KEYCHARACTERMAP,
34    FILETYPE_VIRTUALKEYDEFINITION,
35};
36
37
38static void usage() {
39    fprintf(stderr, "Keymap Validation Tool\n\n");
40    fprintf(stderr, "Usage:\n");
41    fprintf(stderr,
42        " %s [*.kl] [*.kcm] [virtualkeys.*] [...]\n"
43        "   Validates the specified key layouts, key character maps \n"
44        "   or virtual key definitions.\n\n",
45        gProgName);
46}
47
48static FileType getFileType(const char* filename) {
49    const char *extension = strrchr(filename, '.');
50    if (extension) {
51        if (strcmp(extension, ".kl") == 0) {
52            return FILETYPE_KEYLAYOUT;
53        }
54        if (strcmp(extension, ".kcm") == 0) {
55            return FILETYPE_KEYCHARACTERMAP;
56        }
57    }
58
59    if (strstr(filename, "virtualkeys.")) {
60        return FILETYPE_VIRTUALKEYDEFINITION;
61    }
62
63    return FILETYPE_UNKNOWN;
64}
65
66static bool validateFile(const char* filename) {
67    fprintf(stdout, "Validating file '%s'...\n", filename);
68
69    FileType fileType = getFileType(filename);
70    switch (fileType) {
71    case FILETYPE_UNKNOWN:
72        fprintf(stderr, "Supported file types: *.kl, *.kcm, virtualkeys.*\n\n");
73        return false;
74
75    case FILETYPE_KEYLAYOUT: {
76        KeyLayoutMap* map;
77        status_t status = KeyLayoutMap::load(String8(filename), &map);
78        if (status) {
79            fprintf(stderr, "Error %d parsing key layout file.\n\n", status);
80            return false;
81        }
82        break;
83    }
84
85    case FILETYPE_KEYCHARACTERMAP: {
86        KeyCharacterMap* map;
87        status_t status = KeyCharacterMap::load(String8(filename), &map);
88        if (status) {
89            fprintf(stderr, "Error %d parsing key character map file.\n\n", status);
90            return false;
91        }
92        break;
93    }
94
95    case FILETYPE_VIRTUALKEYDEFINITION: {
96        VirtualKeyMap* map;
97        status_t status = VirtualKeyMap::load(String8(filename), &map);
98        if (status) {
99            fprintf(stderr, "Error %d parsing virtual key definition file.\n\n", status);
100            return false;
101        }
102        break;
103    }
104    }
105
106    fputs("No errors.\n\n", stdout);
107    return true;
108}
109
110int main(int argc, const char** argv) {
111    if (argc < 2) {
112        usage();
113        return 1;
114    }
115
116    int result = 0;
117    for (int i = 1; i < argc; i++) {
118        if (!validateFile(argv[i])) {
119            result = 1;
120        }
121    }
122
123    if (result) {
124        fputs("Failed!\n", stderr);
125    } else {
126        fputs("Success.\n", stdout);
127    }
128    return result;
129}
130