[Interest] [SOLVED] Binary file embedded in Resources to std::istream

Thiago Macieira thiago.macieira at intel.com
Mon Aug 5 21:25:13 CEST 2013


On segunda-feira, 5 de agosto de 2013 21:08:45, Rollastre Prostrit wrote:
>      QByteArray bytes = f.readAll();
> 
>      char* bb = bytes.data();
>      std::string str;
>      for (int i=0; i<f.size(); ++i)
>      {
>          str += bb[i];
>      }
> 
>      std::stringstream stream;
>      stream << str;

That seems wrong or, at least, weird. You're appending byte-by-byte from the 
file into the string. That can't be fast...

Doesn't std::string have a method that lets you set a full array of bytes 
(pointer and size)? Or, for that matter, doesn't stringstream have it? I 
confess I don't know: the lack of such convenience API is what keeps me from 
using the Standard Library.

Another alternative is for you to derive from std::istream and create your own 
QFile-based stream. Or a QByteArray-based one, to match std::stringstream.

At the very least, to make it more efficient, you should try to use 
QFile::map(). For uncompressed resources, this will save you a memory 
allocation into the QByteArray.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130805/c2e141e6/attachment.sig>


More information about the Interest mailing list