/* * FUNCTION UTL_FILE.PUTF(file UTL_FILE.FILE_TYPE, * format text, * arg1 text, * arg2 text, * arg3 text, * arg4 text, * arg5 text) * RETURNS bool; * * Puts formated data to file. Allows %s like subst symbol. * * Exception: * INVALID_FILEHANDLE, INVALID_OPERATION, WRITE_ERROR */ Datum utl_file_putf(PG_FUNCTION_ARGS) { FILE *f; char *format; int max_linesize = 0; int encoding = 0; int format_length; char *fpt; int cur_par = 0; int cur_len = 0; CHECK_FILE_HANDLE(); f = get_stream(PG_GETARG_INT32(0), &max_linesize, &encoding); NOT_NULL_ARG(1); format = encode_text(encoding, PG_GETARG_TEXT_P(1), &format_length); for (fpt = format; format_length > 0; fpt++, format_length--) { if (format_length == 1) { /* last char */ CHECK_LENGTH(++cur_len); if (fputc(*fpt, f) == EOF) CHECK_ERRNO_PUT(); continue; } /* ansi compatible string */ if (fpt[0] == '\\' && fpt[1] == 'n') { CHECK_LENGTH(++cur_len); if (fputc('\n', f) == EOF) CHECK_ERRNO_PUT(); fpt++; format_length--; continue; } if (fpt[0] == '%') { if (fpt[1] == '%') { CHECK_LENGTH(++cur_len); if (fputc('%', f) == EOF) CHECK_ERRNO_PUT(); } else if (fpt[1] == 's' && ++cur_par <= 5 && !PG_ARGISNULL(cur_par + 1)) { cur_len += do_write(fcinfo, cur_par + 1, f, max_linesize - cur_len, encoding); } fpt++; format_length--; continue; } CHECK_LENGTH(++cur_len); if (fputc(fpt[0], f) == EOF) CHECK_ERRNO_PUT(); } PG_RETURN_BOOL(true); }
/* fwrite(encode(args[n], encoding), f) */ static int do_write(PG_FUNCTION_ARGS, int n, FILE *f, int max_linesize, int encoding) { text *arg = PG_GETARG_TEXT_P(n); char *str; int len; str = encode_text(encoding, arg, &len); CHECK_LENGTH(len); if (fwrite(str, 1, len, f) != len) CHECK_ERRNO_PUT(); if (VARDATA(arg) != str) pfree(str); PG_FREE_IF_COPY(arg, n); return len; }
int main(int argc, char **argv) { int p_d[P_LENGTH] = {0}; int decoded_fd; struct stat original_file_status; char * decoded_file; if(argc != 3) { printf("Usage : arithmetic_code <-e|-d> filepath\n"); return 1; } char * original_file_name = argv[2]; if(strcmp(argv[1], "-e") == 0) { char * original_file_name = argv[2]; if( (decoded_fd = open(original_file_name, O_RDONLY)) == -1 ) { printf("open %s failed\n", original_file_name); return 1; } if( (fstat(decoded_fd, &original_file_status)) == -1 ) { printf("fstat %s failed\n", original_file_name); return 1; } get_probability_from_file(decoded_fd, p_d); if(lseek(decoded_fd, 0, SEEK_SET) == -1) { printf("lseek failed\n"); return 1; } print_p(p_d, P_LENGTH); printf("\n"); Code encode; Text text; encode.len = INIT_LENGTH; encode.fill_len = 0; encode.bit_len = 0; if( (encode.content = (char *)malloc(sizeof(char) * INIT_LENGTH)) == NULL ) { printf("malloc memery failed\n"); exit(1); } memset(encode.content, 0, INIT_LENGTH); int sum = 0; for(int i = 0; i < P_LENGTH; i++) { if(p_d[i] != 0) { encode.f_d[i] = sum; sum += p_d[i]; } else { encode.f_d[i] = -1; } } encode.f_d[P_LENGTH] = sum; print_p(encode.f_d, P_LENGTH + 1); printf("\n"); text.len = original_file_status.st_size; encode.text_len = text.len; text.read_len = 0; text.original_fd = decoded_fd; if( (text.content = (char *)malloc(sizeof(char) * (INIT_LENGTH + 1))) == NULL ) { printf("text.content malloc memery failed\n"); exit(1); } memset(text.content, 0, INIT_LENGTH); char * compress_file_name; int compress_file_name_len; compress_file_name_len = strlen(original_file_name) + strlen(COMPRESS_SUFFIX) + 1; if( (compress_file_name = (char *)malloc(sizeof(char) * (compress_file_name_len))) == NULL ) { printf("text.compress_file_name malloc memery failed\n"); exit(1); } memset(compress_file_name, 0, compress_file_name_len); strcat(compress_file_name, original_file_name); strcat(compress_file_name, COMPRESS_SUFFIX); if( (text.encode_fd = open(compress_file_name, O_WRONLY | O_CREAT | O_TRUNC)) == -1 ) { printf("text.encode_fd open %s failed\n", argv[1]); return 1; } //-----------encode-text---------- encode_text(&text, &encode); } else if(strcmp(argv[1], "-d") == 0) { Code encode; char * compress_file_name; int encoded_fd; compress_file_name = argv[2]; encode.len = 0; encode.fill_len = 0; encode.bit_len = 0; if( (encode.content = (char *)malloc(sizeof(char) * INIT_LENGTH)) == NULL ) { printf("malloc memery failed\n"); exit(1); } memset(encode.content, 0, INIT_LENGTH); if( (encoded_fd = open(compress_file_name, O_RDONLY)) == -1 ) { printf("open %s failed\n", compress_file_name); return 1; } //-----------decode-text----------- decode_text(&encode, encoded_fd); } else { printf("Usage : arithmetic_code <-e|-d> filepath\n"); } return 0; }