MemoryOutputStream data;
if (ScopedPointer<InputStream> in = zipFile.createStreamForEntry (xyz))
data << *in;
You’ll find with library authors everywhere that you’ll get a lot of resistance if you ask for features that can be written very trivially using the functionality that they already provide.
Only when features are very very commonly used or difficult/error-prone to write does it make sense to add them to a library class.
Things like the C++ standard library take this principle to an extreme - JUCE takes a much more lenient and practical approach, but this one doesn’t sound like it’d be worth adding.
You see, this is something that’s annoying about developing libraries… People always give us very specific requests, for particular tiny bits of functionality. Then you dig a bit deeper into what they’re trying to do, and it turns out that what they’re asking for is not what they really need.
e.g. why not just write yourself a free function like this?
static var getJSONFileFromZip (const ZipFile& zf, StringRef name)
{
if (auto e = zf.getEntry (name, true))
if (ScopedPointer<InputStream> in = zf.createStreamForEntry (e))
return JSON::parse (in->readEntireStreamAsString());
return {};
}
OK, but that’s still a clunky way of writing it. I’d write myself a couple of helpers:
static String loadString (const ZipFile& zf, StringRef name)
{
if (auto e = zf.getEntry (name, true))
if (ScopedPointer<InputStream> in = zf.createStreamForEntry (e))
return in->readEntireStreamAsString();
return {};
}
static Image loadImage (const ZipFile& zf, StringRef name)
{
if (auto e = zf.getEntry (name, true))
if (ScopedPointer<InputStream> in = zf.createStreamForEntry (e))
return ImageFileFormat::loadFrom (*in);
return {};
}
It’d make no sense to copy an image into a MemoryOutputStream and then stream that into an image decoder. Nor would it be very elegant to have multiple places where you convert a MemoryOutputStream into a string and then use it for different things.
But I don’t convert the stream into a string and then into an image. Only two of those convert to a string. The JSON and the SVG. Both are strings. The others are converted directly from the stream into the images.
lambda function allows to have the helper function Jules talks about with an std::function as second argument and have different way to react to the output
easily using one liner.