Question

I have used MapVirtualFile to map a file under Window using C++ VS2010. The void is

  void*                   m_pVoiceData;

I would now like to fill a vector of structs with the data.

The struct is

struct udtPitchmark
{
    unsigned long ByteStart;
    unsigned long ByteCount;
    unsigned long F0;
};

struct udtPitchmarks
{
    vector<udtPitchmark>Pitchmarks;
};

I have been using

void clsMapping::FeedPitchmarksFromMap(udtPitchmarks &uAudioInfo,unsigned long int uBytePos)
{

    unsigned int iCountPM;
    memcpy(&iCountPM, &((char*)(m_pVoiceData))[uBytePos],sizeof(unsigned long));

    uAudioInfo.Pitchmarks.resize(iCountPM);

    for (unsigned int i=0;i<iCountPM;i++)
    {
        iBytePos+=sizeof(unsigned long);
        unsigned long int iByteStart;
        memcpy(&iByteStart, &((char*)(m_pVoiceData))[iBytePos],4);

        iBytePos+=sizeof(unsigned long);
        unsigned long int iByteCount;
        memcpy(&iByteCount, &((char*)(m_pVoiceData))[iBytePos],4);

        iBytePos+=sizeof(unsigned long);
        unsigned int iF0;
        memcpy(&iF0, &((char*)(m_pVoiceData))[iBytePos],4);

        uAudioInfo.Pitchmarks[i].ByteStart =iByteStart;
        uAudioInfo.Pitchmarks[i].ByteCount =iByteCount;
        uAudioInfo.Pitchmarks[i].F0 =iF0;
    }
}

There are 3 memcpy's involved for every pitchmark. But I hope that I can read ALL the pitchmarks at once. However I am not sure how to do that.

The structs where stored like this:

vector<udtPitchmarks>::iterator it = nPitchmarks.Content().begin();
for (;it != nPitchmarks.Content().end(); ++it)
{
    unsigned int iCountPMs = it->Pitchmarks.size();
    fwrite(&iCountPMs,sizeof(unsigned int),1,outfile);

    vector<udtPitchmark>::iterator it2 = it->Pitchmarks.begin();
    for(;it2 != it->Pitchmarks.end(); ++it2)
    {
        fwrite(&it2->ByteStart,sizeof(unsigned long),1,outfile);
        fwrite(&it2->ByteCount,sizeof(unsigned long),1,outfile);
        fwrite(&it2->F0,sizeof(unsigned long),1,outfile);
    }
}

Can somebody help?

Was it helpful?

Solution

you can read whole udtPitchmark structure at once

m_pVoiceData = (char *)m_pVoiceData + sizeof(unsigned int); // size field

memcpy( &uAudioInfo.Pitchmarks[i], (udtPitchmark *)m_pVoiceData + i, sizeof udtPitchmark );

or whole vector:

memcpy( &uAudioInfo.Pitchmarks[0], (udtPitchmark *)m_pVoiceData, sizeof(udtPitchmark ) * iCountPM);

OTHER TIPS

If you lay your data out as an array, you can write to it in one go. Accessor functions could help you maintain a readable interface:

struct Pitchmark
{
    unsigned long data[3];

    unsigned long & start() { return data[0]; }
    unsigned long & count() { return data[1]; }
    unsigned long & f0()    { return data[2]; }

    unsigned char * ptr() { return static_cast<unsigned char *>(data); }
};

Pitchmark pm;
memcpy(pm.ptr(), m_pVoiceData, sizeof pm.data);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top