bool TRI_PrintJson (int fd, TRI_json_t const* object) { TRI_string_buffer_t buffer; char const* p; size_t n; TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE); StringifyJson(buffer._memoryZone, &buffer, object, true); p = TRI_BeginStringBuffer(&buffer); n = TRI_LengthStringBuffer(&buffer); while (0 < n) { ssize_t m = TRI_WRITE(fd, p, n); if (m <= 0) { TRI_AnnihilateStringBuffer(&buffer); return false; } n -= m; p += m; } TRI_AnnihilateStringBuffer(&buffer); return true; }
static bool PrintVelocyPack(int fd, VPackSlice const& slice, bool appendNewline) { if (slice.isNone()) { // sanity check return false; } TRI_string_buffer_t buffer; TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE); arangodb::basics::VPackStringBufferAdapter bufferAdapter(&buffer); try { VPackDumper dumper(&bufferAdapter); dumper.dump(slice); } catch (...) { // Writing failed TRI_AnnihilateStringBuffer(&buffer); return false; } if (TRI_LengthStringBuffer(&buffer) == 0) { // should not happen return false; } if (appendNewline) { // add the newline here so we only need one write operation in the ideal // case TRI_AppendCharStringBuffer(&buffer, '\n'); } char const* p = TRI_BeginStringBuffer(&buffer); size_t n = TRI_LengthStringBuffer(&buffer); while (0 < n) { ssize_t m = TRI_WRITE(fd, p, (TRI_write_t)n); if (m <= 0) { TRI_AnnihilateStringBuffer(&buffer); return false; } n -= m; p += m; } TRI_AnnihilateStringBuffer(&buffer); return true; }
bool TRI_PrintJson (int fd, TRI_json_t const* object, bool appendNewline) { TRI_string_buffer_t buffer; char const* p; size_t n; if (object == NULL) { // sanity check return false; } TRI_InitStringBuffer(&buffer, TRI_UNKNOWN_MEM_ZONE); if (StringifyJson(buffer._memoryZone, &buffer, object, true) != TRI_ERROR_NO_ERROR) { TRI_AnnihilateStringBuffer(&buffer); return false; } if (TRI_LengthStringBuffer(&buffer) == 0) { // should not happen return false; } if (appendNewline) { // add the newline here so we only need one write operation in the ideal case TRI_AppendCharStringBuffer(&buffer, '\n'); } p = TRI_BeginStringBuffer(&buffer); n = TRI_LengthStringBuffer(&buffer); while (0 < n) { ssize_t m = TRI_WRITE(fd, p, (TRI_write_t) n); if (m <= 0) { TRI_AnnihilateStringBuffer(&buffer); return false; } n -= m; p += m; } TRI_AnnihilateStringBuffer(&buffer); return true; }
int TRI_DeflateStringBuffer (TRI_string_buffer_t* self, size_t bufferSize) { TRI_string_buffer_t deflated; const char* ptr; const char* end; char* buffer; int res; z_stream strm; strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; // initialise deflate procedure res = deflateInit(&strm, Z_DEFAULT_COMPRESSION); if (res != Z_OK) { return TRI_ERROR_OUT_OF_MEMORY; } buffer = (char*) TRI_Allocate(TRI_UNKNOWN_MEM_ZONE, bufferSize, false); if (buffer == NULL) { (void) deflateEnd(&strm); return TRI_ERROR_OUT_OF_MEMORY; } // we'll use this buffer for the output TRI_InitStringBuffer(&deflated, TRI_UNKNOWN_MEM_ZONE); ptr = TRI_BeginStringBuffer(self); end = ptr + TRI_LengthStringBuffer(self); while (ptr < end) { int flush; strm.next_in = (unsigned char*) ptr; if (end - ptr > (int) bufferSize) { strm.avail_in = (int) bufferSize; flush = Z_NO_FLUSH; } else { strm.avail_in = (uInt) (end - ptr); flush = Z_FINISH; } ptr += strm.avail_in; do { strm.avail_out = (int) bufferSize; strm.next_out = (unsigned char*) buffer; res = deflate(&strm, flush); if (res == Z_STREAM_ERROR) { (void) deflateEnd(&strm); TRI_Free(TRI_UNKNOWN_MEM_ZONE, buffer); TRI_DestroyStringBuffer(&deflated); return TRI_ERROR_INTERNAL; } if (TRI_AppendString2StringBuffer(&deflated, (char*) buffer, bufferSize - strm.avail_out) != TRI_ERROR_NO_ERROR) { (void) deflateEnd(&strm); TRI_Free(TRI_UNKNOWN_MEM_ZONE, buffer); TRI_DestroyStringBuffer(&deflated); return TRI_ERROR_OUT_OF_MEMORY; } } while (strm.avail_out == 0); } // deflate successful (void) deflateEnd(&strm); TRI_SwapStringBuffer(self, &deflated); TRI_DestroyStringBuffer(&deflated); TRI_Free(TRI_UNKNOWN_MEM_ZONE, buffer); return TRI_ERROR_NO_ERROR; }