cescapingdouble-quotesgracenote

Escape double quotes in Gracenote GNSDK script


I use a script with the SDK Gracenote (GNSDK) for audio recognition of a file (fingerprint).

The script works correctly, I just want to escape the double quotes in a variable.

I found this function:

void str_replace(char *target, const char *needle, const char *replacement)
{
    char buffer[1024] = { 0 };
    char *insert_point = &buffer[0];
    const char *tmp = target;
    size_t needle_len = strlen(needle);
    size_t repl_len = strlen(replacement);

    while (1) {
        const char *p = strstr(tmp, needle);

        // walked past last occurrence of needle; copy remaining part
        if (p == NULL) {
            strcpy(insert_point, tmp);
            break;
        }

        // copy part before needle
        memcpy(insert_point, tmp, p - tmp);
        insert_point += p - tmp;

        // copy replacement string
        memcpy(insert_point, replacement, repl_len);
        insert_point += repl_len;

        // adjust pointers, move on
        tmp = p + needle_len;
    }

    // write altered string back to target
    strcpy(target, buffer);
}

I want to use here, I have the impression that the variable type is different but I do not know how (gnsdk_cstr_t at the start and char in function str_replace). Here is the part of the code or want to escape the double quotes of "VALUE" :

static void
_display_track_gdo(
    gnsdk_gdo_handle_t track_gdo
    )
{
    gnsdk_error_t      error = GNSDK_SUCCESS;
    gnsdk_gdo_handle_t title_gdo = GNSDK_NULL;
    gnsdk_cstr_t       value = GNSDK_NULL;


    /* Track Title */
    error = gnsdk_manager_gdo_child_get(track_gdo, GNSDK_GDO_CHILD_TITLE_OFFICIAL, 1, &title_gdo);
    if (GNSDK_SUCCESS == error)
    {
        error = gnsdk_manager_gdo_value_get(title_gdo, GNSDK_GDO_VALUE_DISPLAY, 1, &value);
        if (GNSDK_SUCCESS == error)
        {

            char s[1024] = value;       
            str_replace(s, "\"", "\\\"");
            printf("\"%s\": \"%s\"", "track", s);
        }
        else
        {
            _display_last_error(__LINE__);
        }
        gnsdk_manager_gdo_release(title_gdo);
    }
    else
    {
        _display_last_error(__LINE__);
    }

}

During compilation , the error is:

main.c: In function '_display_track_gdo':
main.c:756:4: error: invalid initializer
     char s[1024] = value;

How can I solve my problem? Or use another method to escape the double quotes ?

Thank you in advance.

Cordially.

PS: I do not know much the langage C, so it is possible that I make mistakes in how to code .


Solution

  • value is of type gnsdk_cstr_t, you cannot use it to initialize a char array. Use the API the library provides, likely you can copy or convert it into a char array. But you cannot assign to an array.

    Looking the source of the library, in fact, gnsdk_cstr_t is defined as typedef const gnsdk_char_t* gnsdk_cstr_t;, and gnsdk_char_t is defined as typedef char gnsdk_char_t;, this means, gnsdk_cstr_t is the same as null-terminated strings, can just call standard string functions on it:

    char s[1024];
    strcpy(s, value);
    

    reference:

    https://github.com/tingled/jambox/blob/master/rpi_gnsdk/include/gnsdk/gnsdk_defines.h