Main.cpp revision 061cf758841dfc972be3f0ec4857762fafe49aa6
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 <utils/String8.h>
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25using namespace android;
26
27static const char* gProgName = "validatekeymaps";
28
29enum FileType {
30    FILETYPE_UNKNOWN,
31    FILETYPE_KEYLAYOUT,
32    FILETYPE_KEYCHARACTERMAP,
33};
34
35
36static void usage() {
37    fprintf(stderr, "Keymap Validation Tool\n\n");
38    fprintf(stderr, "Usage:\n");
39    fprintf(stderr,
40        " %s [FILENAME.kl] [FILENAME.kcm] [...]\n"
41        "   Validates the specified key layout and/or key character map files.\n\n", gProgName);
42}
43
44static FileType getFileType(const char* filename) {
45    char *extension = strrchr(filename, '.');
46    if (extension) {
47        if (strcmp(extension, ".kl") == 0) {
48            return FILETYPE_KEYLAYOUT;
49        }
50        if (strcmp(extension, ".kcm") == 0) {
51            return FILETYPE_KEYCHARACTERMAP;
52        }
53    }
54    return FILETYPE_UNKNOWN;
55}
56
57static bool validateFile(const char* filename) {
58    fprintf(stdout, "Validating file '%s'...\n", filename);
59
60    FileType fileType = getFileType(filename);
61    switch (fileType) {
62    case FILETYPE_UNKNOWN:
63        fprintf(stderr, "File extension must be .kl or .kcm.\n\n");
64        return false;
65
66    case FILETYPE_KEYLAYOUT: {
67        KeyLayoutMap* map;
68        status_t status = KeyLayoutMap::load(String8(filename), &map);
69        if (status) {
70            fprintf(stderr, "Error %d parsing key layout file.\n\n", status);
71            return false;
72        }
73        break;
74    }
75
76    case FILETYPE_KEYCHARACTERMAP: {
77        KeyCharacterMap* map;
78        status_t status = KeyCharacterMap::load(String8(filename), &map);
79        if (status) {
80            fprintf(stderr, "Error %d parsing key character map file.\n\n", status);
81            return false;
82        }
83        break;
84    }
85    }
86
87    fputs("No errors.\n\n", stdout);
88    return true;
89}
90
91int main(int argc, const char** argv) {
92    if (argc < 2) {
93        usage();
94        return 1;
95    }
96
97    int result = 0;
98    for (int i = 1; i < argc; i++) {
99        if (!validateFile(argv[i])) {
100            result = 1;
101        }
102    }
103
104    if (result) {
105        fputs("Failed!\n", stderr);
106    } else {
107        fputs("Success.\n", stdout);
108    }
109    return result;
110}
111