I resolved this issue by slightly adjusting my logic, and changing the
way in which i deal with file handling.

Now I just have to get it to honor my append parameter so I can expand
the file, and eventually truncate it

Here is the current code:

Birch

void FileLogger::write(const string &location, const string &data,
                const bool append) {
        int perms = O_WRONLY | O_APPEND;
        if (append) {
                perms |= O_APPEND;
        } else {
                perms |= O_CREAT;
        }
        FILE* fd = fopen(location.c_str(), "w+");
        go::FileOutputStream* zos = new go::FileOutputStream(fileno(fd));
        go::CodedOutputStream* cos = new go::CodedOutputStream(zos);
        const char* carr = data.c_str();
        cos->WriteVarint32(1);
        cos->WriteVarint32(strlen(carr));
        cos->WriteRaw(carr, strlen(carr));
        delete cos;
        delete zos;
        close(fileno(fd));
}

void FileLogger::write(const string &location, const vector<string>
&data,
                const bool append) {
        int perms = O_WRONLY | O_APPEND;
        if (append) {
                perms |= O_APPEND;
        } else {
                perms |= O_CREAT;
        }
        FILE* fd = fopen(location.c_str(), "w+");
        go::FileOutputStream* zos = new go::FileOutputStream(fileno(fd));
        go::CodedOutputStream* cos = new go::CodedOutputStream(zos);

        int ds = data.size();
        cos->WriteVarint32(ds);
        for (int i = 0; i < ds; ++i) {
                string st = data[i];
                const char* carr = st.c_str();
                cos->WriteVarint32(strlen(carr));
                cos->WriteRaw(carr, strlen(carr));
        }
        delete cos;
        delete zos;
        close(fileno(fd));
}

vector<string> FileLogger::read(const string &location, const bool
purge) {
        FILE* fd = fopen(location.c_str(), "r");
        go::FileInputStream* zis = new go::FileInputStream(fileno(fd));
        go::CodedInputStream* cis = new go::CodedInputStream(zis);
        bool flag = true;
        vector < string > ret;
        uint32_t num;
        cis->ReadVarint32(&num);
        for (int i = 0; i < num; i++) {
                uint32_t size;
                cis->ReadVarint32(&size);
                char* text = new char[size];
                flag = cis->ReadRaw(text, size);
                ret.push_back(string(text));
                free(text);
        }
        delete cis;
        delete zis;
        close(fileno(fd));
        return ret;

}

On Aug 29, 1:30 pm, Birch <moa...@gmail.com> wrote:
> I am having some issues with the following three methods.  I need the
> ability to read and write any arbitrary protobuf message, that has
> already been deserialized.  It is assumed that I will always only read
> or write the same type.  So I have three methods; write(location,
> data), write(location, vector<data>), vector<data>read(location).
>
> Below is the code.  If I call it with a single string, the first write
> method, it seems to work ok.  But when I call it with a vector, then
> it failes saying the first readVarInt returns 2000000000+.
>
> Am I missing something obvious?
>
> Birch
>
> void FileLogger::write(const string &location, const string &data,
>                 const bool append) {
>         int perms = O_WRONLY;
>         if (append) {
>                 perms |= O_APPEND;
>         } else {
>                 perms |= O_CREAT;
>         }
>         int fd = open(location.c_str(), perms);
>         go::FileOutputStream* zos = new go::FileOutputStream(fd);
>         go::CodedOutputStream* cos = new go::CodedOutputStream(zos);
>         const char* carr = data.c_str();
>         cos->WriteVarint32(strlen(carr));
>         cos->WriteRaw(carr, strlen(carr));
>         delete cos;
>         delete zos;
>         close(fd);
>
> }
>
> void FileLogger::write(const string &location, const vector<string>
> &data,
>                 const bool append) {
>         int perms = O_WRONLY;
>         if (append) {
>                 perms |= O_APPEND;
>         } else {
>                 perms |= O_CREAT;
>         }
>         int fd = open(location.c_str(), perms);
>         go::FileOutputStream* zos = new go::FileOutputStream(fd);
>         go::CodedOutputStream* cos = new go::CodedOutputStream(zos);
>
>         int ds = data.size();
>         for (int i = 0; i < ds; ++i) {
>                 string st = data[i];
>                 const char* carr = st.c_str();
>                 cos->WriteVarint32(strlen(carr));
>                 cos->WriteRaw(carr, strlen(carr));
>         }
>         delete cos;
>         delete zos;
>         close(fd);
>
> }
>
> vector<string> FileLogger::read(const string &location, const bool
> purge) {
>         int fd = open(location.c_str(), O_RDONLY);
>         go::FileInputStream* zis = new go::FileInputStream(fd);
>         go::CodedInputStream* cis = new go::CodedInputStream(zis);
>         bool flag = true;
>         vector < string > ret;
>         while (flag) {
>                 uint32_t size;
>                 cis->ReadVarint32(&size);
>                 if(0 == size) {
>                         flag = false;
>                         continue;
>                 }
>                 if(200000000 < size) {
>                         flag = false;
>                         continue;
>                 }
>                 char* text = new char[size + 1];
>                 flag = cis->ReadRaw(text, size);
>                 text[size] = '\0';
>                 ret.push_back(string(text));
>                 free(text);
>         }
>         delete cis;
>         delete zis;
>         close(fd);
>         return ret;
>
>
>
> }

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to