Exemple #1
0
void test_post_web_page() {
    const char url[] = "http://localhost:3000/sprinklers/1/sensors/1/create_reading";
    const char req[] = "{\"sensor_reading\":{\"sensor_value\":10,\"read_time\":1349015174}}";
    StringBuffer* request = string_buffer_create();
    StringBuffer* response = string_buffer_create();
    string_buffer_write(request, req, strlen(req));
    
    bool result = post_web_page(url, request, response);
    assert(result);
    
    assert(strcmp(response->memory, ACK_STRING)==0);
    
    string_buffer_delete(request);
    string_buffer_delete(response);
    
}
char *
str_replace (const char *haystack, const char *needle, const char *replacement)
{
    assert (haystack != NULL);
    assert (needle != NULL);
    assert (replacement != NULL);

    string_buffer_t *sb = string_buffer_create ();
    size_t haystack_len = strlen (haystack);
    size_t needle_len = strlen (needle);

    int pos = 0;
    while (pos < haystack_len) {
        if (needle_len > 0 && str_starts_with (&haystack[pos], needle)) {
            string_buffer_append_string (sb, replacement);
            pos += needle_len;
        }
        else {
            string_buffer_append (sb, haystack[pos]);
            pos++;
        }
    }
    if (needle_len == 0 && haystack_len == 0)
        string_buffer_append_string (sb, replacement);

    char *res = string_buffer_to_string (sb);
    string_buffer_destroy (sb);
    return res;
}
Exemple #3
0
void test_get_web_page() {
    char url[] = SENSORS_CONFIGURATION_URL;
    StringBuffer* response = string_buffer_create();
    bool result = get_web_page(url, response);
    assert(result);
    string_buffer_delete(response);
}
zarray_t *
str_split (const char *str, const char *delim)
{
    assert (str != NULL);
    assert (delim != NULL);

    zarray_t *parts = zarray_create (sizeof(char*));
    string_buffer_t *sb = string_buffer_create ();

    size_t delim_len = strlen (delim);
    size_t len = strlen (str);
    size_t pos = 0;

    while (pos < len) {
        if (str_starts_with (&str[pos], delim) && delim_len > 0) {
            pos += delim_len;
            // never add empty strings (repeated tokens)
            if (string_buffer_size (sb) > 0) {
                char *part = string_buffer_to_string (sb);
                zarray_add (parts, &part);
            }
            string_buffer_reset (sb);
        }
        else {
            string_buffer_append (sb, str[pos]);
            pos++;
        }
    }

    if (string_buffer_size(sb) > 0) {
        char *part = string_buffer_to_string (sb);
        zarray_add (parts, &part);
    }

    string_buffer_destroy (sb);
    return parts;
}