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