Quick Links: |
Provide platform-independent file input for stack trace resolvers. More...
balst::StackTraceResolver_FileHelper | file input for stack trace resolvers |
readBytes
, which attempts to read a number of bytes into a buffer, and does a partial read if it can't read that many; readExact
, which either reads an exact number of bytes or fails, and loadString
, which reads a 0 terminated string from the file, copies it to a buffer it allocates, and returns a pointer to the copy. bslma::TestAllocator ta; char fileNameBuffer[100]; sprintf(fileNameBuffer, "/tmp/balst_StackTraceResolver_FileHelper.usage.%d.txt", getProcessId());
bdls::FilesystemUtil::remove(fileNameBuffer);
FdType fd = FilesystemUtil::open(fileNameBuffer, true, // writable false); // doesn't already exist assert(FilesystemUtil::k_INVALID_FD != fd);
const char *testString64 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789+-";
int rc; for (int i = 0; i < 20; ++i) { rc = FilesystemUtil::write(fd, testString64, 64); assert(64 == rc); } enum { OFFSET_OF_ZERO_BYTE = 7 * 64 }; rc = (int) FilesystemUtil::seek(fd, OFFSET_OF_ZERO_BYTE, FilesystemUtil::e_SEEK_FROM_BEGINNING); assert(OFFSET_OF_ZERO_BYTE == rc); rc = FilesystemUtil::write(fd, "", 1); // write the zero byte assert(1 == rc); rc = FilesystemUtil::close(fd); assert(0 == rc); { balst::StackTraceResolver_FileHelper helper(fileNameBuffer); char buf[100]; memset(buf, 0, sizeof(buf)); rc = helper.readExact(buf, 6, // # chars to read 128); // offset assert(0 == rc); assert(!strcmp(buf, "abcdef"));
readExact
past EOF fails rc = helper.readExact(buf, 6, // # chars to read 64 * 40); // offset assert(0 != rc);
loadString
will read a zero terminated string at a given offset, using a buffer passed in, and allocating memory for a new copy of the string. memset(buf, 'a', sizeof(buf)); char *result = helper.loadString(OFFSET_OF_ZERO_BYTE - 12, buf, sizeof(buf), &ta); assert(12 == bsl::strlen(result)); assert(!bsl::strcmp("0123456789+-", result));
ta.deallocate(result); } bdls::FilesystemUtil::remove(fileNameBuffer);