[FIX] -Wunused-result warnings (#1269)

* Fix -Wunused-result warnings

* Wrap checked writes into a function

* In write_wrapped, continue writing in case of partial write

If a partial write occurs, it doesn't necessarily mean that something
failed, according to write(2). If this is the case, then the following
write will return -1.

* Fix build on MSVC

https://stackoverflow.com/questions/37460579/error-c2036-void-unknown-size
This commit is contained in:
Nils
2020-06-28 22:29:35 +01:00
committed by GitHub
parent 0db5b0c838
commit f486efbb57
14 changed files with 166 additions and 149 deletions

View File

@@ -191,13 +191,13 @@ void _dtvcc_write_row(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder *d
"conversion failed: %s\n",
strerror(errno));
write(fd, encoded_buf_start, encoded_buf - encoded_buf_start);
write_wrapped(fd, encoded_buf_start, encoded_buf - encoded_buf_start);
free(encoded_buf_start);
}
else
{
write(fd, buf, buf_len);
write_wrapped(fd, buf, buf_len);
}
}
@@ -221,19 +221,19 @@ void ccx_dtvcc_write_srt(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder
"%02u:%02u:%02u,%03u", buf + strlen(buf));
sprintf(buf + strlen(buf), "%s", (char *)encoder->encoded_crlf);
write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
for (int i = 0; i < CCX_DTVCC_SCREENGRID_ROWS; i++)
{
if (!_dtvcc_is_row_empty(tv, i))
{
_dtvcc_write_row(writer, decoder, i, encoder, 1);
write(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
}
}
write(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
}
void ccx_dtvcc_write_debug(dtvcc_tv_screen *tv)
@@ -289,12 +289,13 @@ void ccx_dtvcc_write_transcript(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_
if (encoder->transcript_settings->showMode)
sprintf(buf + strlen(buf), "POP|"); //TODO caption mode(pop, rollup, etc.)
if (strlen(buf))
write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
const size_t buf_len = strlen(buf);
if (buf_len != 0)
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, buf_len);
_dtvcc_write_row(writer, decoder, i, encoder, 0);
write(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
}
}
}
@@ -321,16 +322,16 @@ void _dtvcc_write_sami_header(dtvcc_tv_screen *tv, struct encoder_ctx *encoder)
buf_len += sprintf(buf + buf_len, "</head>%s%s", encoder->encoded_crlf, encoder->encoded_crlf);
buf_len += sprintf(buf + buf_len, "<body>%s", encoder->encoded_crlf);
write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, buf_len);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, buf_len);
}
void _dtvcc_write_sami_footer(dtvcc_tv_screen *tv, struct encoder_ctx *encoder)
{
char *buf = (char *)encoder->buffer;
sprintf(buf, "</body></sami>");
write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
write(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
}
void ccx_dtvcc_write_sami(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder *decoder, struct encoder_ctx *encoder)
@@ -351,24 +352,24 @@ void ccx_dtvcc_write_sami(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decode
sprintf(buf, "<sync start=%llu><p class=\"unknowncc\">%s",
(unsigned long long)tv->time_ms_show + encoder->subs_delay,
encoder->encoded_crlf);
write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
for (int i = 0; i < CCX_DTVCC_SCREENGRID_ROWS; i++)
{
if (!_dtvcc_is_row_empty(tv, i))
{
_dtvcc_write_row(writer, decoder, i, encoder, 1);
write(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_br, encoder->encoded_br_length);
write(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_br, encoder->encoded_br_length);
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd,
encoder->encoded_crlf, encoder->encoded_crlf_length);
}
}
sprintf(buf, "<sync start=%llu><p class=\"unknowncc\">&nbsp;</p></sync>%s%s",
(unsigned long long)tv->time_ms_hide + encoder->subs_delay,
encoder->encoded_crlf, encoder->encoded_crlf);
write(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
write_wrapped(encoder->dtvcc_writers[tv->service_number - 1].fd, buf, strlen(buf));
}
void _ccx_dtvcc_write(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_decoder *decoder, struct encoder_ctx *encoder)
@@ -488,7 +489,7 @@ void ccx_dtvcc_writer_output(ccx_dtvcc_writer_ctx *writer, ccx_dtvcc_service_dec
CCX_COMMON_EXIT_FILE_CREATION_FAILED, "[CEA-708] Failed to open a file\n");
}
if (!encoder->no_bom)
write(writer->fd, UTF8_BOM, sizeof(UTF8_BOM));
write_wrapped(writer->fd, UTF8_BOM, sizeof(UTF8_BOM));
}
_ccx_dtvcc_write(writer, decoder, encoder);

View File

@@ -16,25 +16,25 @@ int write_cc_buffer_as_g608(struct eia608_screen *data, struct encoder_ctx *cont
context->srt_counter++;
sprintf(timeline, "%u%s", context->srt_counter, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf(timeline, "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u%s",
h1, m1, s1, ms1, h2, m2, s2, ms2, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
for (int i = 0; i < 15; i++)
{
int length = get_line_encoded(context, context->subline, i, data);
write(context->out->fh, context->subline, length);
write_wrapped(context->out->fh, context->subline, length);
length = get_color_encoded(context, context->subline, i, data);
write(context->out->fh, context->subline, length);
write_wrapped(context->out->fh, context->subline, length);
length = get_font_encoded(context, context->subline, i, data);
write(context->out->fh, context->subline, length);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->subline, length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
wrote_something = 1;
}
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
return wrote_something;
}

View File

@@ -156,7 +156,7 @@ boolean mcc_encode_cc_data(struct encoder_ctx *enc_ctx, struct lib_cc_decode *de
strcat(compressed_data_buffer, "\n");
write(enc_ctx->out->fh, compressed_data_buffer, strlen(compressed_data_buffer));
write_wrapped(enc_ctx->out->fh, compressed_data_buffer, strlen(compressed_data_buffer));
free(compressed_data_buffer);
@@ -721,5 +721,5 @@ static void byte_to_ascii(uint8 hex_byte, uint8 *msn, uint8 *lsn)
static void write_string(int fh, char *string)
{
write(fh, string, strlen(string));
write_wrapped(fh, string, strlen(string));
}

View File

@@ -24,17 +24,14 @@ int write_stringz_as_sami(char *string, struct encoder_ctx *context, LLONG ms_st
used = encode_line(context, context->buffer, (unsigned char *)str);
ret = write(context->out->fh, context->buffer, used);
if (ret != used)
{
return ret;
}
len = strlen(string);
unescaped = (unsigned char *)malloc(len + 1);
if (!unescaped)
if (unescaped == NULL)
{
mprint("In write_stringz_as_sami() - not enough memory for len %d.\n", len);
ret = -1;
goto end;
return -1;
}
el = (unsigned char *)malloc(len * 3 + 1); // Be generous
@@ -132,7 +129,7 @@ int write_cc_bitmap_as_sami(struct cc_subtitle *sub, struct encoder_ctx *context
{
sprintf(buf,
"<SYNC start=%llu><P class=\"UNKNOWNCC\">\r\n", (unsigned long long)sub->start_time);
write(context->out->fh, buf, strlen(buf));
write_wrapped(context->out->fh, buf, strlen(buf));
for (int i = sub->nb_data - 1; i >= 0; i--)
{
if (rect[i].ocr_text && *(rect[i].ocr_text))
@@ -142,21 +139,21 @@ int write_cc_bitmap_as_sami(struct cc_subtitle *sub, struct encoder_ctx *context
token = strtok(rect[i].ocr_text, "\r\n");
sprintf(buf, "%s", token);
token = strtok(NULL, "\r\n");
write(context->out->fh, buf, strlen(buf));
write_wrapped(context->out->fh, buf, strlen(buf));
if (i != 0)
write(context->out->fh, context->encoded_br, context->encoded_br_length);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_br, context->encoded_br_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
}
}
sprintf(buf, "</P></SYNC>\r\n");
write(context->out->fh, buf, strlen(buf));
write_wrapped(context->out->fh, buf, strlen(buf));
}
else //we write an empty subtitle to clear the old one
{
sprintf(buf,
"<SYNC start=%llu><P class=\"UNKNOWNCC\">&nbsp;</P></SYNC>\r\n\r\n", (unsigned long long)sub->start_time);
write(context->out->fh, buf, strlen(buf));
write_wrapped(context->out->fh, buf, strlen(buf));
}
#endif
@@ -204,7 +201,7 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
for (int i = 0; i < 15; i++)
{
if (data->row_used[i])
@@ -215,11 +212,11 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont
dbg_print(CCX_DMT_DECODER_608, "\r");
dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline);
}
write(context->out->fh, context->subline, length);
write_wrapped(context->out->fh, context->subline, length);
wrote_something = 1;
if (i != 14)
write(context->out->fh, context->encoded_br, context->encoded_br_length);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_br, context->encoded_br_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
}
sprintf((char *)str, "</P></SYNC>\r\n");
@@ -228,7 +225,7 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf((char *)str,
"<SYNC start=%llu><P class=\"UNKNOWNCC\">&nbsp;</P></SYNC>\r\n\r\n",
(unsigned long long)data->end_time - 1); // - 1 to prevent overlap
@@ -237,6 +234,6 @@ int write_cc_buffer_as_sami(struct eia608_screen *data, struct encoder_ctx *cont
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
return wrote_something;
}

View File

@@ -419,11 +419,12 @@ void add_padding(int fd, const char disassemble)
if (disassemble)
{
write(fd, "_", 1);
write_wrapped(fd, "_", 1);
}
else
{
write(fd, "80", 2); // 0x80 == odd_parity(0x00)
// 0x80 == odd_parity(0x00)
write_wrapped(fd, "80", 2);
}
}
@@ -440,12 +441,12 @@ void write_character(const int fd, const unsigned char character, const bool dis
{
if (disassemble)
{
write(fd, &character, 1);
write_wrapped(fd, &character, 1);
}
else
{
if (*bytes_written % 2 == 0)
write(fd, " ", 1);
write_wrapped(fd, " ", 1);
fdprintf(fd, "%02x", odd_parity(character));
}
@@ -466,12 +467,12 @@ void write_control_code(const int fd, const unsigned char channel, const enum co
{
unsigned int length;
const char *assembly_code = disassemble_code(code, &length);
write(fd, assembly_code, length);
write_wrapped(fd, assembly_code, length);
}
else
{
if (*bytes_written % 2 == 0)
write(fd, " ", 1);
write_wrapped(fd, " ", 1);
fdprintf(fd, "%02x%02x", odd_parity(get_first_byte(channel, code)), odd_parity(get_second_byte(code)));
}
@@ -520,9 +521,9 @@ enum control_code get_font_code(enum font_bits font, enum ccx_decoder_608_color_
void add_timestamp(const struct encoder_ctx *context, LLONG time, const bool disassemble)
{
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
if (!disassemble)
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
unsigned hour, minute, second, milli;
millis_to_time(time, &hour, &minute, &second, &milli);

View File

@@ -53,7 +53,7 @@ void write_stringz_as_smptett(char *string, struct encoder_ctx *context, LLONG m
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
// Scan for \n in the string and replace it with a 0
while (pos_r < len)
{
@@ -80,10 +80,10 @@ void write_stringz_as_smptett(char *string, struct encoder_ctx *context, LLONG m
dbg_print(CCX_DMT_DECODER_608, "\r");
dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline);
}
write(context->out->fh, el, u);
write_wrapped(context->out->fh, el, u);
//write (wb->fh, encoded_br, encoded_br_length);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
begin += strlen((const char *)begin) + 1;
}
@@ -93,14 +93,14 @@ void write_stringz_as_smptett(char *string, struct encoder_ctx *context, LLONG m
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf((char *)str, "<p begin=\"%02u:%02u:%02u.%03u\">\n\n", h2, m2, s2, ms2);
if (context->encoding != CCX_ENC_UNICODE)
{
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf((char *)str, "</p>\n");
free(el);
free(unescaped);
@@ -134,12 +134,12 @@ int write_cc_bitmap_as_smptett(struct cc_subtitle *sub, struct encoder_ctx *cont
millis_to_time(sub->start_time, &h1, &m1, &s1, &ms1);
millis_to_time(sub->end_time - 1, &h2, &m2, &s2, &ms2); // -1 To prevent overlapping with next line.
sprintf((char *)context->buffer, "<p begin=\"%02u:%02u:%02u.%03u\" end=\"%02u:%02u:%02u.%03u\">\n", h1, m1, s1, ms1, h2, m2, s2, ms2);
write(context->out->fh, buf, strlen(buf));
write_wrapped(context->out->fh, buf, strlen(buf));
len = strlen(rect[i].ocr_text);
write(context->out->fh, rect[i].ocr_text, len);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, rect[i].ocr_text, len);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
sprintf(buf, "</p>\n");
write(context->out->fh, buf, strlen(buf));
write_wrapped(context->out->fh, buf, strlen(buf));
}
}
}
@@ -231,7 +231,7 @@ int write_cc_buffer_as_smptett(struct eia608_screen *data, struct encoder_ctx *c
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
// Trimming subs because the position is defined by "tts:origin"
int old_trim_subs = context->trim_subs;
context->trim_subs = 1;
@@ -388,9 +388,9 @@ int write_cc_buffer_as_smptett(struct eia608_screen *data, struct encoder_ctx *c
}
}
write(context->out->fh, final, strlen(final));
write_wrapped(context->out->fh, final, strlen(final));
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
context->trim_subs = old_trim_subs;
sprintf(str, " <style tts:backgroundColor=\"#000000FF\" tts:fontSize=\"18px\"/></span>\n </p>\n");
@@ -399,7 +399,7 @@ int write_cc_buffer_as_smptett(struct eia608_screen *data, struct encoder_ctx *c
dbg_print(CCX_DMT_DECODER_608, "\r%s\n", str);
}
used = encode_line(context, context->buffer, (unsigned char *)str);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
if (context->encoding != CCX_ENC_UNICODE)
{

View File

@@ -23,14 +23,14 @@ int write_stringz_as_srt(char *string, struct encoder_ctx *context, LLONG ms_sta
context->srt_counter++;
sprintf(timeline, "%u%s", context->srt_counter, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf(timeline, "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u%s",
h1, m1, s1, ms1, h2, m2, s2, ms2, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
dbg_print(CCX_DMT_DECODER_608, "\n- - - SRT caption - - -\n");
dbg_print(CCX_DMT_DECODER_608, "%s", timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
int len = strlen(string);
unsigned char *unescaped = (unsigned char *)malloc(len + 1);
unsigned char *el = (unsigned char *)malloc(len * 3 + 1); // Be generous
@@ -64,14 +64,14 @@ int write_stringz_as_srt(char *string, struct encoder_ctx *context, LLONG ms_sta
dbg_print(CCX_DMT_DECODER_608, "\r");
dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline);
}
write(context->out->fh, el, u);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, el, u);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
begin += strlen((const char *)begin) + 1;
}
dbg_print(CCX_DMT_DECODER_608, "- - - - - - - - - - - -\r\n");
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
free(el);
free(unescaped);
@@ -114,14 +114,14 @@ int write_cc_bitmap_as_srt(struct cc_subtitle *sub, struct encoder_ctx *context)
context->srt_counter++;
sprintf(timeline, "%u%s", context->srt_counter, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf(timeline, "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u%s",
h1, m1, s1, ms1, h2, m2, s2, ms2, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
len = strlen(str);
write(context->out->fh, str, len);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, str, len);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
freep(&str);
}
@@ -198,12 +198,12 @@ int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *conte
++context->srt_counter;
sprintf(timeline, "%u%s", context->srt_counter, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
sprintf(timeline, "%02u:%02u:%02u,%03u --> %02u:%02u:%02u,%03u%s",
h1, m1, s1, ms1, h2, m2, s2, ms2, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
dbg_print(CCX_DMT_DECODER_608, "\n- - - SRT caption ( %d) - - -\n", context->srt_counter);
dbg_print(CCX_DMT_DECODER_608, "%s", timeline);
@@ -262,7 +262,7 @@ int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *conte
do_dash = 0;
if (do_dash)
write(context->out->fh, "- ", 2);
write_wrapped(context->out->fh, "- ", 2);
prev_line_start = first;
prev_line_end = last;
prev_line_center1 = center1;
@@ -274,8 +274,8 @@ int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *conte
dbg_print(CCX_DMT_DECODER_608, "\r");
dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline);
}
write(context->out->fh, context->subline, length);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->subline, length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
wrote_something = 1;
// fprintf (wb->fh,context->encoded_crlf);
}
@@ -283,7 +283,7 @@ int write_cc_buffer_as_srt(struct eia608_screen *data, struct encoder_ctx *conte
dbg_print(CCX_DMT_DECODER_608, "- - - - - - - - - - - -\r\n");
// fprintf (wb->fh, context->encoded_crlf);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
//printf("$ = %s\n",context->encoded_crlf);
return wrote_something;
}

View File

@@ -24,7 +24,7 @@ int write_stringz_as_ssa(char *string, struct encoder_ctx *context, LLONG ms_sta
dbg_print(CCX_DMT_DECODER_608, "\n- - - ASS/SSA caption - - -\n");
dbg_print(CCX_DMT_DECODER_608, "%s", timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
int len = strlen(string);
unsigned char *unescaped = (unsigned char *)malloc(len + 1);
unsigned char *el = (unsigned char *)malloc(len * 3 + 1); // Be generous
@@ -58,14 +58,14 @@ int write_stringz_as_ssa(char *string, struct encoder_ctx *context, LLONG ms_sta
dbg_print(CCX_DMT_DECODER_608, "\r");
dbg_print(CCX_DMT_DECODER_608, "%s\n", context->subline);
}
write(context->out->fh, el, u);
write(context->out->fh, "\\N", 2);
write_wrapped(context->out->fh, el, u);
write_wrapped(context->out->fh, "\\N", 2);
begin += strlen((const char *)begin) + 1;
}
dbg_print(CCX_DMT_DECODER_608, "- - - - - - - - - - - -\r\n");
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
free(el);
free(unescaped);
@@ -113,9 +113,9 @@ int write_cc_bitmap_as_ssa(struct cc_subtitle *sub, struct encoder_ctx *context)
sprintf(timeline, "Dialogue: 0,%02u:%02u:%02u.%01u,%02u:%02u:%02u.%02u,Default,,0000,0000,0000,,",
h1, m1, s1, ms1 / 10, h2, m2, s2, ms2 / 10);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write(context->out->fh, str, len);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, str, len);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
freep(&str);
}
@@ -188,7 +188,7 @@ int write_cc_buffer_as_ssa(struct eia608_screen *data, struct encoder_ctx *conte
dbg_print(CCX_DMT_DECODER_608, "\n- - - ASS/SSA caption - - -\n");
dbg_print(CCX_DMT_DECODER_608, "%s", timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
int line_count = 0;
for (int i = 0; i < 15; i++)
{
@@ -244,7 +244,7 @@ int write_cc_buffer_as_ssa(struct eia608_screen *data, struct encoder_ctx *conte
do_dash = 0;
if (do_dash)
write(context->out->fh, "- ", 2);
write_wrapped(context->out->fh, "- ", 2);
prev_line_start = first;
prev_line_end = last;
prev_line_center1 = center1;
@@ -258,9 +258,9 @@ int write_cc_buffer_as_ssa(struct eia608_screen *data, struct encoder_ctx *conte
}
if (line_count)
{
write(context->out->fh, "\\N", 2);
write_wrapped(context->out->fh, "\\N", 2);
}
write(context->out->fh, context->subline, length);
write_wrapped(context->out->fh, context->subline, length);
line_count++;
wrote_something = 1;
}
@@ -268,6 +268,6 @@ int write_cc_buffer_as_ssa(struct eia608_screen *data, struct encoder_ctx *conte
dbg_print(CCX_DMT_DECODER_608, "- - - - - - - - - - - -\r\n");
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
return wrote_something;
}

View File

@@ -96,7 +96,7 @@ int write_cc_bitmap_as_transcript(struct cc_subtitle *sub, struct encoder_ctx *c
}
}
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
}
#endif

View File

@@ -222,7 +222,7 @@ void write_webvtt_header(struct encoder_ctx *context)
ccx_options.enc_cfg.line_terminator_lf ? "\n\n" : "\r\n\r\n");
}
used = encode_line(context, context->buffer, (unsigned char *)header_string);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
}
if (ccx_options.webvtt_create_css)
@@ -242,21 +242,21 @@ void write_webvtt_header(struct encoder_ctx *context)
char *outline_css_file = (char *)malloc((strlen(css_file_name) + strlen(webvtt_outline_css)) * sizeof(char));
sprintf(outline_css_file, webvtt_outline_css, css_file_name);
write(context->out->fh, outline_css_file, strlen(outline_css_file));
write_wrapped(context->out->fh, outline_css_file, strlen(outline_css_file));
}
else if (ccx_options.use_webvtt_styling)
{
write(context->out->fh, webvtt_inline_css, strlen(webvtt_inline_css));
write_wrapped(context->out->fh, webvtt_inline_css, strlen(webvtt_inline_css));
if (ccx_options.enc_cfg.line_terminator_lf == 1) // If -lf parameter is set.
{
write(context->out->fh, "\n", 1);
write_wrapped(context->out->fh, "\n", 1);
}
else
{
write(context->out->fh, "\r\n", 2);
write_wrapped(context->out->fh, "\r\n", 2);
}
write(context->out->fh, "##\n", 3);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, "##\n", 3);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
context->wrote_webvtt_header = 1; // Do it even if couldn't write the header, because it won't be possible anyway
@@ -294,10 +294,10 @@ int write_cc_bitmap_as_webvtt(struct cc_subtitle *sub, struct encoder_ctx *conte
sprintf(timeline, "%02u:%02u:%02u.%03u --> %02u:%02u:%02u.%03u%s",
h1, m1, s1, ms1, h2, m2, s2, ms2, context->encoded_crlf);
used = encode_line(context, context->buffer, (unsigned char *)timeline);
write(context->out->fh, context->buffer, used);
write_wrapped(context->out->fh, context->buffer, used);
len = strlen(str);
write(context->out->fh, str, len);
write(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
write_wrapped(context->out->fh, str, len);
write_wrapped(context->out->fh, context->encoded_crlf, context->encoded_crlf_length);
}
freep(&str);
}
@@ -467,24 +467,24 @@ int write_cc_buffer_as_webvtt(struct eia608_screen *data, struct encoder_ctx *co
if (open_font != FONT_REGULAR)
{
if (open_font & FONT_ITALICS)
write(context->out->fh, strdup("<i>"), 3);
write_wrapped(context->out->fh, strdup("<i>"), 3);
if (open_font & FONT_UNDERLINED)
write(context->out->fh, strdup("<u>"), 3);
write_wrapped(context->out->fh, strdup("<u>"), 3);
}
// opening events for colors
int open_color = color_events[j] & 0xFF; // Last 16 bytes
if (open_color != COL_WHITE)
{
write(context->out->fh, strdup("<c."), 3);
write(context->out->fh, color_text[open_color][0], strlen(color_text[open_color][0]));
write(context->out->fh, ">", 1);
write_wrapped(context->out->fh, strdup("<c."), 3);
write_wrapped(context->out->fh, color_text[open_color][0], strlen(color_text[open_color][0]));
write_wrapped(context->out->fh, ">", 1);
}
}
// write current text symbol
if (context->subline[j] != '\0')
write(context->out->fh, &(context->subline[j]), 1);
write_wrapped(context->out->fh, &(context->subline[j]), 1);
if (ccx_options.use_webvtt_styling)
{
@@ -492,7 +492,7 @@ int write_cc_buffer_as_webvtt(struct eia608_screen *data, struct encoder_ctx *co
int close_color = color_events[j] >> 16; // First 16 bytes
if (close_color != COL_WHITE)
{
write(context->out->fh, strdup("</c>"), 4);
write_wrapped(context->out->fh, strdup("</c>"), 4);
}
// closing events for fonts
@@ -500,9 +500,9 @@ int write_cc_buffer_as_webvtt(struct eia608_screen *data, struct encoder_ctx *co
if (close_font != FONT_REGULAR)
{
if (close_font & FONT_ITALICS)
write(context->out->fh, strdup("</i>"), 4);
write_wrapped(context->out->fh, strdup("</i>"), 4);
if (close_font & FONT_UNDERLINED)
write(context->out->fh, strdup("</u>"), 4);
write_wrapped(context->out->fh, strdup("</u>"), 4);
}
}
}

View File

@@ -460,7 +460,8 @@ size_t buffered_read_opt(struct ccx_demuxer *ctx, unsigned char *buffer, size_t
{
char c;
for (size_t i = 0; i < bytes; i++)
read(ctx->infd, &c, 1);
if (read(ctx->infd, &c, 1) == -1)
fatal(1, "reading from file");
copied = bytes;
}
else

View File

@@ -25,14 +25,16 @@ ULLONG get_current_byte(FILE *file)
UBYTE *read_byte_block(FILE *file, ULLONG n)
{
UBYTE *buffer = malloc((size_t)(sizeof(UBYTE) * n));
fread(buffer, 1, (size_t)n, file);
if (fread(buffer, 1, (size_t)n, file) != n)
fatal(1, "reading from file");
return buffer;
}
char *read_bytes_signed(FILE *file, ULLONG n)
{
char *buffer = malloc((size_t)(sizeof(UBYTE) * (n + 1)));
fread(buffer, 1, (size_t)n, file);
if (fread(buffer, 1, (size_t)n, file) != n)
fatal(1, "reading from file");
buffer[n] = 0;
return buffer;
}
@@ -1109,7 +1111,7 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
free(filename);
if (track->header != NULL)
write(desc, track->header, strlen(track->header));
write_wrapped(desc, track->header, strlen(track->header));
for (int i = 0; i < track->sentence_count; i++)
{
@@ -1118,7 +1120,7 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
if (track->codec_id == MATROSKA_TRACK_SUBTITLE_CODEC_ID_WEBVTT)
{
write(desc, "\n\n", 2);
write_wrapped(desc, "\n\n", 2);
struct block_addition *blockaddition = sentence->blockaddition;
@@ -1127,8 +1129,8 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
{
if (blockaddition->comment != NULL)
{
write(desc, sentence->blockaddition->comment, sentence->blockaddition->comment_size);
write(desc, "\n", 1);
write_wrapped(desc, sentence->blockaddition->comment, sentence->blockaddition->comment_size);
write_wrapped(desc, "\n", 1);
}
}
@@ -1137,12 +1139,12 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
{
if (blockaddition->cue_identifier != NULL)
{
write(desc, blockaddition->cue_identifier, blockaddition->cue_identifier_size);
write(desc, "\n", 1);
write_wrapped(desc, blockaddition->cue_identifier, blockaddition->cue_identifier_size);
write_wrapped(desc, "\n", 1);
}
else if (blockaddition->comment != NULL)
{
write(desc, "\n", 1);
write_wrapped(desc, "\n", 1);
}
}
@@ -1155,25 +1157,25 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
char *timestamp_end = malloc(sizeof(char) * 80);
timestamp_to_vtttime(time_end, timestamp_end);
write(desc, timestamp_start, strlen(timestamp_start));
write(desc, " --> ", 5);
write(desc, timestamp_end, strlen(timestamp_start));
write_wrapped(desc, timestamp_start, strlen(timestamp_start));
write_wrapped(desc, " --> ", 5);
write_wrapped(desc, timestamp_end, strlen(timestamp_start));
// writing cue settings list
if (blockaddition != NULL)
{
if (blockaddition->cue_settings_list != NULL)
{
write(desc, " ", 1);
write(desc, blockaddition->cue_settings_list, blockaddition->cue_settings_list_size);
write_wrapped(desc, " ", 1);
write_wrapped(desc, blockaddition->cue_settings_list, blockaddition->cue_settings_list_size);
}
}
write(desc, "\n", 1);
write_wrapped(desc, "\n", 1);
int size = 0;
while (*(sentence->text + size) == '\n' || *(sentence->text + size) == '\r')
size++;
write(desc, sentence->text + size, sentence->text_size - size);
write_wrapped(desc, sentence->text + size, sentence->text_size - size);
free(timestamp_start);
free(timestamp_end);
@@ -1190,24 +1192,24 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
char *timestamp_end = malloc(sizeof(char) * 80);
timestamp_to_srttime(time_end, timestamp_end);
write(desc, number, strlen(number));
write(desc, "\n", 1);
write(desc, timestamp_start, strlen(timestamp_start));
write(desc, " --> ", 5);
write(desc, timestamp_end, strlen(timestamp_start));
write(desc, "\n", 1);
write_wrapped(desc, number, strlen(number));
write_wrapped(desc, "\n", 1);
write_wrapped(desc, timestamp_start, strlen(timestamp_start));
write_wrapped(desc, " --> ", 5);
write_wrapped(desc, timestamp_end, strlen(timestamp_start));
write_wrapped(desc, "\n", 1);
int size = 0;
while (*(sentence->text + size) == '\n' || *(sentence->text + size) == '\r')
size++;
write(desc, sentence->text + size, sentence->text_size - size);
write_wrapped(desc, sentence->text + size, sentence->text_size - size);
if (sentence->text[sentence->text_size - 1] == '\n')
{
write(desc, "\n", 1);
write_wrapped(desc, "\n", 1);
}
else
{
write(desc, "\n\n", 2);
write_wrapped(desc, "\n\n", 2);
}
free(timestamp_start);
@@ -1221,16 +1223,16 @@ void save_sub_track(struct matroska_ctx *mkv_ctx, struct matroska_sub_track *tra
time_end = MIN(time_end, track->sentences[i + 1]->time_start - 1);
char *timestamp_end = generate_timestamp_ass_ssa(time_end);
write(desc, "Dialogue: Marked=0,", strlen("Dialogue: Marked=0,"));
write(desc, timestamp_start, strlen(timestamp_start));
write(desc, ",", 1);
write(desc, timestamp_end, strlen(timestamp_start));
write(desc, ",", 1);
write_wrapped(desc, "Dialogue: Marked=0,", strlen("Dialogue: Marked=0,"));
write_wrapped(desc, timestamp_start, strlen(timestamp_start));
write_wrapped(desc, ",", 1);
write_wrapped(desc, timestamp_end, strlen(timestamp_start));
write_wrapped(desc, ",", 1);
char *text = ass_ssa_sentence_erase_read_order(sentence->text);
while ((text[0] == '\\') && (text[1] == 'n' || text[1] == 'N'))
text += 2;
write(desc, text, strlen(text));
write(desc, "\n", 1);
write_wrapped(desc, text, strlen(text));
write_wrapped(desc, "\n", 1);
free(timestamp_start);
free(timestamp_end);

View File

@@ -252,6 +252,19 @@ void print_error(int mode, const char *fmt, ...)
fprintf(stderr, "\n");
va_end(args);
}
void write_wrapped(int fd, const char *buf, size_t count)
{
while (count)
{
ssize_t written = write(fd, buf, count);
if (written == -1)
fatal(1, "writing to file");
buf += written;
count -= written;
}
}
/* Write formatted message to stderr and then exit. */
void fatal(int exit_code, const char *fmt, ...)
{

View File

@@ -48,4 +48,6 @@ char *strndup(const char *s, size_t n);
char *strtok_r(char *str, const char *delim, char **saveptr);
#endif //_WIN32
void write_wrapped(int fd, const char *buf, size_t count);
#endif //CC_UTILITY_H