int omc_write_csv(OMC_WRITE_CSV* csvData, const void* csvLine){ size_t dest_size; unsigned char buffer[CSV_BUFFER_SIZE] = ""; dest_size = csv_write(&buffer, CSV_BUFFER_SIZE, csvLine, strlen(csvLine)); if (dest_size > CSV_BUFFER_SIZE){ unsigned char* newbuffer = (unsigned char*) malloc(dest_size*sizeof(char)); dest_size = csv_write(&newbuffer, dest_size, csvLine, strlen(csvLine)); fprintf(csvData->handle, "%s", newbuffer); }else{ fprintf(csvData->handle, "%s", buffer); } return 0; }
///////////////////////////////////////////////////////////////////////////////// // // Function: // // Purpose: // // Parameters: // // Return value: // // Author: Komatsu Yuji(Zheng Chuyu) // ///////////////////////////////////////////////////////////////////////////////// char *jhk_csv(const char *str) { char *csv; apr_size_t len; if (str == NULL) { csv = strdup(""); return csv; } len = strlen(str); csv = (char *) malloc(2 * len + 3); memset(csv, 0, 2 * len + 3); csv_write(csv, 2 * len + 2, str, strlen(str)); return csv; }
/** * Column callback */ static inline void cb_col(void *s, size_t len, void *data) { struct csv_context *ctx = (struct csv_context *)data; size_t cnt; // Put a comma if we should if(ctx->put_comma) { ctx->csv_buf = cbuf_putc(ctx->csv_buf, ','); } ctx->put_comma = 1; // If we are keeping same columns together see if we're on one if(ctx->gcol > -1 && ctx->col == ctx->gcol) { // Don't treat header columns as a group column if(!ctx->use_header || ctx->header_len) { // If we have a last column value and we're in overflow, check // the new row's value against the last one if(ctx->gcol_buf && ctx->opos && memcmp(ctx->gcol_buf, s, len) != 0) { // Flush the data we have! flush_file(ctx, 1); } else if(!ctx->gcol_buf) { // Initialize a new group column buffer ctx->gcol_buf = cbuf_init(len); } // Update our last group column value ctx->gcol_buf = cbuf_setlen(ctx->gcol_buf, (const char*)s, len); } } // Make sure we can write all the data while((cnt = csv_write(CBUF_PTR(ctx->csv_buf), CBUF_REM(ctx->csv_buf), s, len)) > CBUF_REM(ctx->csv_buf)) { // We didn't have room, reallocate ctx->csv_buf = cbuf_double(ctx->csv_buf); } // Increment where we are in our buffer CBUF_POS(ctx->csv_buf)+=cnt; // Increment our column ctx->col++; }