# Use string to load BinaryData files?

**URL:** <https://forum.juce.com/t/use-string-to-load-binarydata-files/45933>\
**Category:** Audio Plugins\
**Created:** [May 9, 2021, 9:37pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933 "2021-05-09T21:37:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmcdubs](https://avatars.discourse-cdn.com/v4/letter/g/c57346/32.png) [@gmcdubs](https://forum.juce.com/u/gmcdubs)\
**Post date:** [May 9, 2021, 9:37pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/1 "2021-05-09T21:37:20Z")

</div>

Is it possible to convert a string to a type that might be able to directly load BinaryData files?

I’ve built a binaural panner but, due to some assertion errors while reading in WAV files, decided binary is a better option.

It’s working now when hard-coded with a particular file (BinaryData::thisFile), but I’m looking for a way of converting the existing string file names “H-10e000a.wav” into something that can produce the same result (“H-10e000a.wav” → BinaryData::H-10e000a\_wav).

Hoping I don’t have to completely rework the switch statements I currently have. Thanks for your help!

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 9, 2021, 10:05pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/2 "2021-05-09T22:05:46Z")

</div>

Switch statements usually don’t work with strings, only integral types.

However there is a method in BinaryData that allows to retrieve the filenames, so you can iterate through the available files.

```auto
for (int i = 0; i < BinaryData::namedResourceListSize; ++i)
    if (juce::String::fromUTF8 (BinaryData::namedResourceList [i])) == name)
        // ...

```

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [May 9, 2021, 10:13pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/3 "2021-05-09T22:13:34Z")

</div>

I use something like this:

```auto
//Represents a binary data object:
struct RawData
{
    explicit RawData(const char* fileToFind)
    {
        using namespace BinaryData;

        for (int index = 0; index < namedResourceListSize; ++index)
        {
            auto binaryName = namedResourceList[index];
            auto fileName = getNamedResourceOriginalFilename(binaryName);

            if (fileName == fileToFind)
            {
                data = BinaryData::getNamedResource(binaryName, size);
                break;
            }
        }

        jassert(data != nullptr); //File not found
    }

    const char* data = nullptr;
    int size = 0;
};

```

Then when you need something:

```auto
RawData binary ("File.wav");
doSomethingWithData(binary.data, binary.size);

```

---

<div class="post-metadata">

**Author:** ![gmcdubs](https://avatars.discourse-cdn.com/v4/letter/g/c57346/32.png) [@gmcdubs](https://forum.juce.com/u/gmcdubs)\
**Post date:** [May 9, 2021, 11:50pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/4 "2021-05-09T23:50:51Z")

</div>

Thanks @Daniel and @eyalamir - I’ll look into those now!

I’ve been passing the file path, determined by GUI parameters, as a string previously. This makes a lot more sense.

---

<div class="post-metadata">

**Author:** ![gmcdubs](https://avatars.discourse-cdn.com/v4/letter/g/c57346/32.png) [@gmcdubs](https://forum.juce.com/u/gmcdubs)\
**Post date:** [May 10, 2021, 2:35am UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/5 "2021-05-10T02:35:28Z")

</div>

All sorted - works really well now. Thanks again!

---

<div class="post-metadata">

**Author:** ![bayu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bayu/32/10757_2.png) [@bayu](https://forum.juce.com/u/bayu)\
**Post date:** [October 9, 2022, 6:02am UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/6 "2022-10-09T06:02:16Z")

</div>

> [@eyalamir](#):
>
> ```auto
> RawData binary ("File.wav");
> doSomethingWithData(binary.data, binary.size);
> 
> ```

I have an xml file that holds the original file name

```auto
auto xml { juce::parseXML (juce::File (“Attribute.xml”) };

auto filename { static_cast<const char*> (xml->getStringAttribute (“file”).toUTF8() };

```

DBG prints the filename correctly.

```auto
DBG (filename);

```

But passing filename as an argument always returns nullptr.

```auto
RawData binary (filename);

```

I also have tried

```auto
auto filename { static_cast<const char*> (xml->getStringAttribute (“file”).toUTF8().getAddress() };

```

But still to no avail.

Any idea how to make it work?

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [October 9, 2022, 6:58pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/7 "2022-10-09T18:58:20Z")

</div>

> [@bayu](#):
>
> Any idea how to make it work?

A few things to try:

1. Make sure that where the `RawData` constructor is defined, is also where you include “BinaryData.h”

2. Perhaps step into that function and see why it doesn’t find it? Maybe there’s an issue with the case not matching or something like that?

---

<div class="post-metadata">

**Author:** ![bayu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bayu/32/10757_2.png) [@bayu](https://forum.juce.com/u/bayu)\
**Post date:** [October 10, 2022, 8:08am UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/8 "2022-10-10T08:08:24Z")

</div>

> [@eyalamir](#):
>
> A few things to try:
> 
> 1. Make sure that where the `RawData` constructor is defined, is also where you include “BinaryData.h”
> 2. Perhaps step into that function and see why it doesn’t find it? Maybe there’s an issue with the case not matching or something like that?

1. I put RawData constructor definition in one of my JUCE modules cpp, which includes \<JuceHeader.h\>
2. The case is perfectly matched. I can confirmed it by printing via DBG the String object from XmlElement::getStringAttribute, and the const char\* fileToFind inside RawData constructor.

```auto
            DBGV (fileName);
            DBGV (fileToFind);
            
            if (fileName == fileToFind)
            {
                data = BinaryData::getNamedResource(binaryName, size);
                break;
            }

```

fileName and fileToFind printed identically in DBG console output. But fileName == fileToFind comparison always return false, hence it always return nullptr.

EDIT: I managed to solve the issue, by changing the condition using strcmp.

```auto
struct RawData
{
    explicit RawData (const char* fileToFind)
    {
        using namespace BinaryData;

        for (int index = 0; index < namedResourceListSize; ++index)
        {
            auto binaryName = namedResourceList[index];
            auto fileName = getNamedResourceOriginalFilename (binaryName);

            if (strcmp (fileName, fileToFind) == 0)
            {
                data = getNamedResource (binaryName, size);
                break;
            }
        }

        jassert (data != nullptr); //File not found
    }

    const char* data = nullptr;
    int size = 0;
};

```

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [October 10, 2022, 3:34pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/9 "2022-10-10T15:34:45Z")

</div>

Ah, thank you for debugging that @bayu!

I think we probably need to change that code so it accepts a `String` or a `std::string` in the constructor instead of a `const char *`.

---

<div class="post-metadata">

**Author:** ![bayu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bayu/32/10757_2.png) [@bayu](https://forum.juce.com/u/bayu)\
**Post date:** [October 10, 2022, 4:42pm UTC](https://forum.juce.com/t/use-string-to-load-binarydata-files/45933/10 "2022-10-10T16:42:22Z")

</div>

Sure.

```auto
struct RawData
{
    explicit RawData (const char* fileToFind)
    {
        using namespace BinaryData;

        for (int index = 0; index < namedResourceListSize; ++index)
        {
            auto binaryName = namedResourceList[index];
            auto fileName = getNamedResourceOriginalFilename (binaryName);

            if (strcmp (fileName, fileToFind) == 0)
            {
                data = getNamedResource (binaryName, size);
                break;
            }
        }

        jassert (data != nullptr); // File not found
    }

    explicit RawData (const juce::String& fileToFind) : RawData (static_cast<const char*> (fileToFind.toUTF8())) {}
    explicit RawData (const std::string& fileToFind) : RawData (fileToFind.c_str()) {}
    const char* data = nullptr;
    int size = 0;
};

```
