From 4bcc79fdb788c835403cf5232a45052b1dee31b7 Mon Sep 17 00:00:00 2001 From: Saurabh Shrivastava Date: Sun, 12 Mar 2017 00:28:33 +0530 Subject: [PATCH 1/2] Use already available functions. Removed code with redundant functionality. -extracting filename without extension -generating timestamp for srt format from milliseconds --- src/lib_ccx/matroska.c | 48 +++++++----------------------------------- 1 file changed, 8 insertions(+), 40 deletions(-) diff --git a/src/lib_ccx/matroska.c b/src/lib_ccx/matroska.c index a11c4f30..a452dc9d 100644 --- a/src/lib_ccx/matroska.c +++ b/src/lib_ccx/matroska.c @@ -1,4 +1,5 @@ #include "lib_ccx.h" +#include "utility.h" #include "matroska.h" #include @@ -211,20 +212,6 @@ void parse_segment_info(FILE* file) { } } -char* generate_timestamp_utf8(ULLONG milliseconds) { - ULLONG millis = milliseconds % 1000; - milliseconds /= 1000; - ULLONG seconds = milliseconds % 60; - milliseconds /= 60; - ULLONG minutes = milliseconds % 60; - milliseconds /= 60; - ULLONG hours = milliseconds; - - char* buf = malloc(sizeof(char) * 15); - sprintf(buf, "%02" LLD_M ":%02" LLD_M ":%02" LLD_M ",%03" LLD_M, hours, minutes, seconds, millis); - return buf; -} - char* generate_timestamp_ass_ssa(ULLONG milliseconds) { ULLONG millis = (milliseconds % 1000) / 10; milliseconds /= 1000; @@ -738,35 +725,14 @@ void parse_segment(struct matroska_ctx* mkv_ctx) } } -char *filename_without_ext(char* filename) { - char *returnname; - char *lastdot; - if (filename == NULL) - return filename; - if ((returnname = malloc (strlen (filename) + 1)) == NULL) - return filename; - strcpy (returnname, filename); - lastdot = strrchr (returnname, '.'); - if (lastdot != NULL) - *lastdot = '\0'; - return returnname; -} - char* generate_filename_from_track(struct matroska_ctx* mkv_ctx, struct matroska_sub_track* track) { - char* filename = malloc(sizeof(char)*(MAX_FILE_NAME_SIZE-30)); - char * filename_non_ext; - filename[MAX_FILE_NAME_SIZE-31] = '\0'; - strncpy(filename, mkv_ctx->filename,(MAX_FILE_NAME_SIZE-31)); - filename_non_ext=filename_without_ext(filename); - char* buf = malloc(sizeof(char) * MAX_FILE_NAME_SIZE); + char* buf = malloc(sizeof(char) * 200); if (track->lang_index == 0) - sprintf(buf, "%s_%s.%s", filename_non_ext, track->lang, matroska_track_text_subtitle_id_extensions[track->codec_id]); + sprintf(buf, "%s_%s.%s", get_basename(mkv_ctx->filename), track->lang, matroska_track_text_subtitle_id_extensions[track->codec_id]); else - sprintf(buf, "%s_%s_" LLD ".%s", filename_non_ext, track->lang, track->lang_index, + sprintf(buf, "%s_%s_" LLD ".%s", get_basename(mkv_ctx->filename), track->lang, track->lang_index, matroska_track_text_subtitle_id_extensions[track->codec_id]); - free(filename); - free(filename_non_ext); return buf; } @@ -814,11 +780,13 @@ void save_sub_track(struct matroska_ctx* mkv_ctx, struct matroska_sub_track* tra char number[9]; sprintf(number, "%d", i + 1); - char *timestamp_start = generate_timestamp_utf8(sentence->time_start); + char *timestamp_start = malloc(sizeof(char) * 80); //being generous + timestamp_to_srttime(sentence->time_start, timestamp_start); ULLONG time_end = sentence->time_end; if (i + 1 < track->sentence_count) time_end = MIN(time_end, track->sentences[i + 1]->time_start - 1); - char *timestamp_end = generate_timestamp_utf8(time_end); + char *timestamp_end = malloc(sizeof(char) * 80); + timestamp_to_srttime(time_end, timestamp_end); write(desc, number, strlen(number)); write(desc, "\n", 1); From 7b4c3bb26db9abfbeb3922e0677d3c39751daccb Mon Sep 17 00:00:00 2001 From: Saurabh Shrivastava Date: Sun, 12 Mar 2017 00:39:57 +0530 Subject: [PATCH 2/2] Removed depricated function. --- src/lib_ccx/lib_ccx.h | 1 - src/lib_ccx/utility.c | 10 ---------- 2 files changed, 11 deletions(-) diff --git a/src/lib_ccx/lib_ccx.h b/src/lib_ccx/lib_ccx.h index f938281b..99d32996 100644 --- a/src/lib_ccx/lib_ccx.h +++ b/src/lib_ccx/lib_ccx.h @@ -252,7 +252,6 @@ bool_t in_array(uint16_t *array, uint16_t length, uint16_t element) ; int hex_to_int (char high, char low); int hex_string_to_int(char* string, int len); void timestamp_to_srttime(uint64_t timestamp, char *buffer); -void timestamp_to_smptetttime(uint64_t timestamp, char *buffer); int levenshtein_dist (const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len); void millis_to_date (uint64_t timestamp, char *buffer, enum ccx_output_date_format date_format, char millis_separator); void signal_handler(int sig_type); diff --git a/src/lib_ccx/utility.c b/src/lib_ccx/utility.c index 9dc59dd1..68fcea86 100644 --- a/src/lib_ccx/utility.c +++ b/src/lib_ccx/utility.c @@ -139,16 +139,6 @@ void timestamp_to_srttime(uint64_t timestamp, char *buffer) sprintf(buffer, "%02"PRIu8":%02"PRIu8":%02"PRIu8",%03"PRIu16, h, m, s, u); } -void timestamp_to_smptetttime(uint64_t timestamp, char *buffer) -{ - uint64_t p = timestamp; - uint8_t h = (uint8_t) (p / 3600000); - uint8_t m = (uint8_t) (p / 60000 - 60 * h); - uint8_t s = (uint8_t) (p / 1000 - 3600 * h - 60 * m); - uint16_t u = (uint16_t) (p - 3600000 * h - 60000 * m - 1000 * s); - sprintf(buffer, "%02"PRIu8":%02"PRIu8":%02"PRIu8".%03"PRIu16, h, m, s, u); -} - #define MIN3(a, b, c) ((a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c))) int levenshtein_dist (const uint64_t *s1, const uint64_t *s2, unsigned s1len, unsigned s2len)