Streaming from memory mapped files in Qt - c++

Is there any way to create a data stream that is located at the beginning of a memory mapped file in Qt?
Once i use QFile::map method, i get a uchar*. So is there any way to initialize a data stream with it? Thanks in advance!

I've not tried it but there's a QDataStream::readRawData() function that accepts a char * and a length which you should be able to pipe that uchar * into. That'll give you your stream.

Related

Is there a way to read from a file and fill the message buff without using set_buff()?

Using grpc to transfer file.
The implementation I'm using now is:
1: read from file to a char string buff
2: use set_buff to make the message for grpc
I find that there may be a lot overhead because it copys the data twice.
So is that possible to read from file and dirctly save into the message buff for gprc? Thank you for any help.
You can use mutable_buff method to get access to the pointer of the variable and then use it to assign it to the memory where you have data loaded from the file, or even more straightforward you call set_allocated_buff.
std::string data;
// load file content to data variable
response.set_allocated_buff(&data);
More info here: https://developers.google.com/protocol-buffers/docs/reference/cpp-generated

How to convert a string into a Standard C File ?

Hi I have an image in my memory and I want to sent it through an external FTP library.
This FTP library accepts only and only standard C FILE and the sample codes provided by this library reads data only from hard disk. In my application I don't want to store my images in the hard disk and then read them using FILE variable, instead I want to do the conversion in my memory so it's faster and more professional.
My image is in the form of uchar * but I can change it to std::String or QByteArray or any other type of string. Now I want to know how can I have a file which is filled by my image data so I will get rid of storing it into the hard disk and read it again.
My pseudo code:
uchar * image = readImage();
FILE * New_Image = String2FileConverter(image); //I need this function
FTP_Upload(New_Image);
On Posix systems, you can use fmemopen to create a memory-backed file handle.

Writing into a variable without knowing the total size in advance

I need to modify an existing function, which is creating a file according to the configuration of the system. The total size of the created file is not known in advance. Thus, the original function simply creates a file and dumps everything into it. It even keeps some offsets in memory and updates the file during the process.
I am not allowed to create a file on the disk. I need to keep everything in one single variable. However, I don't know the total size in advance.
Can you suggest me a data structure for this case?
Just to correct #Raxvan's answer. You can write binary to a std::stringstream. For example if you want to read and write to a stringstream declare it as:
std::stringstream buffer(std::iostream::in | std::iostream::out | std::iostream::binary);
If you want to write binary data to it:
int integer = 1234;
buffer.write(reinterpret_cast<char*>(&integer), sizeof(int));
If you want to read from it later:
int integer = 0;
buffer.read(reinterpret_cast<char*>(&integer), sizeof(int));
You don't need to specify the length beforehand. If you need the length afterwards, you can use something like:
buffer.str().size();
Hope this helps..
Yes, for sure.
There are two pointers on the data buffer. A put and a get pointer. So easiest way to explain it is to say that the put is for writing and the get is for reading.
So finding the position of the put pointer can be done like this:
Try something like this:
//Get the offset where the write pointer is..
std::streamoff offset = buffer.tellp();
//Write 'dummy' value
buffer.write(reinterpret_cast<char*>(&integer), sizeof(int));
.
.
.
(do other writing)
.
.
.
//Scroll to offset (from start)
buffer.seekp(offset);
//Write real value
buffer.write(reinterpret_cast<char*>(&integer), sizeof(int));
Okay, so that is if you want to scroll to an offset from the start.
If you want to scroll from you current position look at things like:
buffer.seekp(offset, std::ios_base::cur);

Converting FILE * to CMemFile (or retrieving void* from FILE *)

I'm currently working with a function call that uses FILE *, but I need to get data from the FILE * into a buffer without letting the data touch the HDD. My first idea was to convert FILE * to a CMemFile, but I cannot find a way to do this. Any ideas?
Using MFC...
This question comes up from using the JPEG library by IJG whose homepage can be found here: http://www.ijg.org/
You can use setvbuf to make a FILE pointer use a predefined (and filled) buffer.

TinyXML: how to parse a file pointer

I'm trying to connect the output of popen, a file pointer, to the input of TinyXML.
According to the main page, the best way to do it is using the parse method:
C style input:
* based on FILE*
* the Parse() and LoadFile() methods
I believe I need to use the TIXML_USE_STL to get to this. How do I go about finding examples and import it?
A reply since deleted pointed me in the right direction on the docs
http://www.grinninglizard.com/tinyxmldocs/index.html
Thanks.
Now, i just need to figure out how to link and import it.
I'm not hugely familiar with TinyXML, but does LoadFile() not work in its overloaded version which takes a FILE *?
http://www.grinninglizard.com/tinyxmldocs/classTiXmlDocument.html#a12
EDIT: Ah, the problem is that TinyXML doesn't support reading from a stream (see the link above). Your only choice then is to read the stream manually into a buffer and pass it to TinyXML's Parse().
You can read the file data into some buffer (say SomeCharBuffer), append null terminator to it and do
TiXmlDocument doc;
doc.Parse(SomeCharBuffer);