diff --git a/libmobile.c b/libmobile.c index d131a3d004473eadc8b4d616cf31bbcd8f56a8d5..9354f83f14d7f24cf62386624a92288e5656ad3b 100644 --- a/libmobile.c +++ b/libmobile.c @@ -18,7 +18,7 @@ void curl_cleaner(CURLcode *curl); size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp); int apn_profile_idx(struct json_object *apn_profiles, char *name); int get_apn_profiles_len(void); -struct json_object *parse_apn_profiles(char *apn_profiles); +char *lexer(char **input, char *delimiter); /** * Function: curl_cleaner @@ -139,6 +139,48 @@ success: return len; } +/** + * Function: lexer + * + * Take an input string and a delimiter which to split the string on. Returns all characters + * prior to the delimiter, and removes it from the input string. + * + * Parameters: + * input - A character pointer address to the pointer pointing at the string to split. + * delimiter - A character pointer pointing to the delimiter on which to split the string on. + * + * Returns: + * A character pointer to the tokens prior to the delimiter on success. + * NULL on failure, missing input, delimiter or when no delimiter is present. + * + * IMPORTANT NOTE + * Will alter the input string and allocate memory for the return value! + * The caller is responsible for freeing the return value. + */ +char *lexer(char **input, char *delimiter) { + if(strlen(*input) == 0) { + printf("empty input!\n"); + return NULL; + } + if(strlen(delimiter) == 0) { + printf("empty delimiter!\n"); + return NULL; + } + char *token = strstr(*input, delimiter); + + if (token) + *token = '\0'; + else { + printf("delimiter missing!\n"); + return NULL; + } + char *substr = strdup(*input); + + *input = token + strlen(delimiter); + + return substr; +} + /** * Function: parse_apn_profiles * @@ -147,6 +189,7 @@ success: * * Parameters: * apn_profiles - A character string containing the APN profiles. + * * Returns: * The newly generated JSON on success. * NULL on failure. @@ -154,18 +197,18 @@ success: struct json_object *parse_apn_profiles(char *apn_profiles) { if (strlen(apn_profiles) <= 0) { - DEBUG("No APN profiles provided!\n"); + printf("No APN profiles provided!\n"); goto fail; } struct json_object *apn_profiles_json = json_tokener_parse(apn_profiles); if (!apn_profiles_json) { - DEBUG("Not valid json!\n"); + printf("Not valid json!\n"); goto fail; } struct json_object *parsed_profiles = json_object_new_object(); int apn_counter = 0; - char field_names[13] = {"profile_name", "apn_name", "mode", "dunno", "authentication", "username?", "password?", "IPdunno", "ddnunno?", "dno", "DNS_mode", "IPv_username", "IPv_password"}; + char *field_names[13] = {"profile_name", "apn_name", "mode", "wan_dial", "ppp_auth_mode", "ppp_username", "ppp_passwd", "pdp", "pdpd_select", "pdp_addr", "dns_mode", "prefer_dns_manual", "standby_dns_manual"}; int rv; json_object_object_foreach(apn_profiles_json, key, val) { @@ -173,36 +216,30 @@ struct json_object *parse_apn_profiles(char *apn_profiles) if (strlen(apn_string) <= 0) goto finished; - int i; + int i = 0; struct json_object *apn_profile = json_object_new_object(); + char *field_val; - for (i = 0; i < 13; i++) { - char *field_val = strtok(apn_string, "($)"); - json_object_object_add(apn_profile, field_names[i], field_val); - } - rv = json_object_object_add(parsed_profiles, sprintf("%d", apn_counter), apn_profile); //be careful here, do we need json_object_get(apn_profile)? - json_object_put(apn_profile); - if (!rv) { - DEBUG("Error adding object!\n"); - goto free_objects; + while ((field_val = lexer(&apn_string, "($)")) != NULL) { + json_object_object_add(apn_profile, field_names[i], json_object_new_string(field_val)); + i++; + free(field_val); } + char name[1024]; + sprintf(name, "%d", apn_counter); + json_object_object_add(parsed_profiles, name, apn_profile); //be careful here, do we need json_object_get(apn_profile)? apn_counter++; - //json_put what?? - printf("FREE THEM CORRECTLY HERE!\n"); - //best guess, free: apn_profile } goto finished; free_objects: - json_object_put(apn_profiles); + json_object_put(apn_profiles_json); json_object_put(parsed_profiles); fail: return NULL; finished: - printf("probably need to free apn_profiles_json!\n"); //best guess, free: apn_profiles_json - json_object_put(apn_profiles); - json_object_put(parsed_profiles); + json_object_put(apn_profiles_json); return parsed_profiles; } diff --git a/libmobile.h b/libmobile.h index 2c6531f2b77d4c3700b46aa50e80867869d67fe8..a35a92905708d1ae978afd843c8de74667a4a116 100644 --- a/libmobile.h +++ b/libmobile.h @@ -40,7 +40,7 @@ * the caller is then responsible for freeing this memory by calling free(returned_pointer). * ***************************************************/ - +struct json_object *parse_apn_profiles(char *apn_profiles); /** * Function: mobile_connect_network *