Ejemplo n.º 1
0
static inline void print_output(char *address, normalize_options_t options, bool use_json) {
    size_t num_expansions;

    char **strings = expand_address(address, options, &num_expansions);

    char *normalized;

    if (!use_json) {
        for (size_t i = 0; i < num_expansions; i++) {
            normalized = strings[i];
            printf("%s\n", normalized);        
            free(normalized);
        }
    } else {
        printf("{\"expansions\": [");
        for (size_t i = 0; i < num_expansions; i++) {
            normalized = strings[i];
            char *json_string = json_encode_string(normalized);
            printf("%s%s", json_string, i < num_expansions - 1 ? ", ": "");
            free(normalized);
            free(json_string);
        }
        printf("]}\n");

    }

    free(strings);

}
Ejemplo n.º 2
0
// Generates JSON message containing all variables
void digit_json_encode_vars(char *str)
{
    char sSubStr1[ENCODE_STR1_SIZE];
    char sSubStr2[ENCODE_STR2_SIZE];
    char sSubStr3[ENCODE_STR3_SIZE];    
    
    strcpy(sSubStr1, "");
    strcpy(str, "{");   

    for (int i = 0; i < NUM_OF_DIGIT_VARS; i++)
    {
        strcpy(sSubStr2, "");
    
        g_digit_vars[i].encodeFunPtr(g_digit_vars[i].u8Value, sSubStr3);
        json_encode_string(sSubStr2,
                           "value",
                           sSubStr3);
                           
        strncat(sSubStr2, ",", 1);
        json_encode_integer(sSubStr2,
                            "ts",
                            g_digit_vars[i].tTimestamp);
                            
        json_encode_object(sSubStr1,
                           g_digit_vars[i].sNameStr,
                           sSubStr2);
                           
        if (i != (NUM_OF_DIGIT_VARS - 1))
        {
            strncat(sSubStr1, ",", 1);
        }
    }   
    json_encode_object(str,
                       DIGIT_VARS,
                       sSubStr1);
    strncat(str, "}", 1);                   
}
Ejemplo n.º 3
0
int main(int argc, char **argv) {
    char *address_parser_dir = LIBPOSTAL_ADDRESS_PARSER_DIR;
    char *history_file = "address_parser.history";

    if (argc > 1) {
        address_parser_dir = argv[1];
    }

    printf("Loading models...\n");

    if (!libpostal_setup() || !libpostal_setup_parser()) {
        exit(EXIT_FAILURE);
    }

    printf("\n");

    printf("Welcome to libpostal's address parser.\n\n");
    printf("Type in any address to parse and print the result.\n\n");
    printf("Special commands:\n\n");
    printf(".language [code] to specify a language\n");
    printf(".country [code] to specify a country\n");
    printf(".exit to quit the program\n\n");

    char *language = NULL;
    char *country = NULL;

    char *input = NULL;

    while((input = linenoise("> ")) != NULL) {

        if (input[0] != '\0') {
            linenoiseHistoryAdd(input); /* Add to the history. */
            linenoiseHistorySave(history_file); /* Save the history on disk. */
        }

        if (strcmp(input, ".exit") == 0) {
            printf("Fin!\n");
            free(input);
            break;
        } else if (strncmp(input, ".language", 9) == 0) {
            size_t num_tokens = 0;
            cstring_array *command = cstring_array_split(input, " ", 1, &num_tokens);
            if (num_tokens > 1) {
                if (language != NULL) {
                    free(language);
                }
                language = strdup(cstring_array_get_string(command, 1));
            } else {
                printf("Must specify language code\n");
            }

            cstring_array_destroy(command);
            goto next_input;
        } else if (strncmp(input, ".country", 8) == 0) {
            size_t num_tokens = 0;
            cstring_array *command = cstring_array_split(input, " ", 1, &num_tokens);
            if (cstring_array_num_strings(command) > 1) {
                if (country != NULL) {
                    free(country);
                }
                country = strdup(cstring_array_get_string(command, 1));
            } else {
                printf("Must specify country code\n");
            }

            cstring_array_destroy(command);
            goto next_input;
        } else if (strlen(input) == 0) {
            goto next_input;
        }

        address_parser_response_t *parsed;
        address_parser_options_t options = get_libpostal_address_parser_default_options();

        if ((parsed = parse_address(input, options))) {
            printf("\n");
            printf("Result:\n\n");
            printf("{\n");
            for (int i = 0; i < parsed->num_components; i++) {
                char *json_string = json_encode_string(parsed->components[i]);
                printf("  \"%s\": %s%s\n", parsed->labels[i], json_string, i < parsed->num_components - 1 ? "," : "");
            }
            printf("}\n");
            printf("\n");

            address_parser_response_destroy(parsed);
        } else {
            printf("Error parsing address\n");
        }

next_input:
        free(input);
    }

    libpostal_teardown();
    libpostal_teardown_parser();
}