diff options
author | dynaflash <[email protected]> | 2007-04-25 17:24:58 +0000 |
---|---|---|
committer | dynaflash <[email protected]> | 2007-04-25 17:24:58 +0000 |
commit | cdcdac58032b929a7edea0459047a9cb56da767d (patch) | |
tree | c0aad497331fe94fb7f6b13b9600283f9350cf59 /test | |
parent | e1f55561971a286170d003d20eff51c9a5563f43 (diff) |
Fix Previous Bad commit for Cyanders Chapter Markers
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@548 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile | 6 | ||||
-rw-r--r-- | test/parsecsv.c | 214 | ||||
-rw-r--r-- | test/parsecsv.h | 36 | ||||
-rw-r--r-- | test/test.c | 55 |
4 files changed, 308 insertions, 3 deletions
diff --git a/test/Makefile b/test/Makefile index 0f021cc1d..6068484ea 100644 --- a/test/Makefile +++ b/test/Makefile @@ -13,11 +13,13 @@ CXXFLAGS += -I../libhb LIBS2 = ../libhb/libhb.a $(LIBS:%=../contrib/lib/lib%.a) LDFLAGS += $(LIBS2) -../HandBrakeCLI: test.c $(LIBS2) +../HandBrakeCLI: test.c parsecsv.c $(LIBS2) @CMD="$(CC) $(CFLAGS) -o test.o -c test.c"; $$CMD || \ ( echo "Compile line for $@ was:"; echo $$CMD; false ) + @CMD="$(CC) $(CFLAGS) -o parsecsv.o -c parsecsv.c"; $$CMD || \ + ( echo "Compile line for $@ was:"; echo $$CMD; false ) @echo "Link HandBrakeCLI" - @CMD="g++ $(CXXFLAGS) -o ../HandBrakeCLI test.o $(LDFLAGS) -lz -lpthread"; $$CMD || \ + @CMD="g++ $(CXXFLAGS) -o ../HandBrakeCLI test.o parsecsv.o $(LDFLAGS) -lz -lpthread"; $$CMD || \ ( echo "Compile line for $@ was:"; echo $$CMD; false ) @CMD="rm -rf ../plugins ; mkdir ../plugins ; cp ../contrib/lib/libquicktime/* ../plugins"; $$CMD diff --git a/test/parsecsv.c b/test/parsecsv.c new file mode 100644 index 000000000..f0a8a1a18 --- /dev/null +++ b/test/parsecsv.c @@ -0,0 +1,214 @@ +/* $Id: parsecsv.c $ + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.m0k.org/>. + It may be used under the terms of the GNU General Public License. */ + +#include <fcntl.h> +#include "hb.h" +#include "parsecsv.h" + +/* Internal declarations */ +#define is_newline(_x) ( (_x) == 13 || \ + (_x) == 11 || \ + (_x) == 10 ) + +#define is_white(_x) ( (_x) == '\t' || \ + (_x) == ' ' || \ + is_newline(_x) ) + +#define is_sep(_x) ( (_x) == ',' ) + +#define is_esc(_x) ( (_x) == '\\' ) + +#define CSV_CHAR_ERROR 0x8000 +#define CSV_CHAR_EOF 0x4000 +#define CSV_CHAR_ROWSEP 0x2000 +#define CSV_CHAR_COLSEP 0x1000 + +#define CSV_PARSE_NORMAL 0x0000 +#define CSV_PARSE_SEEK 0x0001 +#define CSV_PARSE_ESC 0x0002 + +static uint16_t hb_parse_character( hb_csv_file_t * file ); +static void hb_trim_end( char *text ); + +/* Open a CSV File */ +hb_csv_file_t *hb_open_csv_file( const char *filepath ) +{ + hb_csv_file_t *file = NULL; + FILE * fileref; + + if( filepath == NULL ) + { + return file; + } + + fileref = fopen( filepath, "r" ); + if( fileref == NULL ) + { + return file; + } + + file = malloc( sizeof( hb_csv_file_t ) ); + file->fileref = fileref; + file->eof = 0; + file->parse_state = CSV_PARSE_SEEK; + file->curr_col = 0; + file->curr_row = 0; + return file; +} + +void hb_close_csv_file( hb_csv_file_t *file ) +{ + if( file == NULL ) + { + return; + } + + fclose( file->fileref ); + free( file ); +} + +/* Parse CSV Cells */ +hb_csv_cell_t *hb_read_next_cell( hb_csv_file_t *file ) +{ + hb_csv_cell_t *cell = NULL; + uint16_t c; + int index; + + if( file == NULL ) + { + return cell; + } + + if( file->eof ) + { + return cell; + } + + cell = malloc( sizeof( hb_csv_cell_t ) ); + cell->cell_row = file->curr_row; + cell->cell_col = file->curr_col; + index = 0; + while( CSV_CHAR_EOF != (c = hb_parse_character( file ) ) ) + { + if( c == CSV_CHAR_ROWSEP ) + { + file->curr_row++; + file->curr_col = 0; + break; + } + else if( c == CSV_CHAR_COLSEP ) + { + file->curr_col++; + break; + } + else + { + if( index < 1023 ) + { + cell->cell_text[index] = (char)c; + index++; + } + } + } + + if( c == CSV_CHAR_EOF ) + { + file->eof = 1; + } + + /* Terminate the cell text */ + cell->cell_text[index] = '\0'; + hb_trim_end( cell->cell_text ); + return cell; +} + +void hb_dispose_cell( hb_csv_cell_t *cell ) +{ + if( cell == NULL ) + { + return; + } + + free( cell ); +} + +/* Raw parsing */ +static uint16_t hb_parse_character( hb_csv_file_t * file ) +{ + int byte; + uint16_t c; + int read_result; + int need_char = 1; + + if( file == NULL ) + { + return CSV_CHAR_ERROR; + } + + while( need_char ) + { + byte = fgetc( file->fileref ); + if( feof( file->fileref ) ) + { + return CSV_CHAR_EOF; + } + if( ferror( file->fileref ) ) + { + return CSV_CHAR_ERROR; + } + + if( file->parse_state == CSV_PARSE_SEEK && is_white(byte) ) + { + continue; + } + else if( file->parse_state != CSV_PARSE_ESC && is_esc(byte) ) + { + file->parse_state = CSV_PARSE_ESC; + continue; + } + else if( file->parse_state != CSV_PARSE_ESC && is_sep(byte) ) + { + file->parse_state = CSV_PARSE_SEEK; + need_char = 0; + c = CSV_CHAR_COLSEP; + } + else if( file->parse_state == CSV_PARSE_ESC ) + { + file->parse_state = CSV_PARSE_NORMAL; + need_char = 0; + c = (uint16_t)byte; + } + else if( is_newline(byte) ) + { + file->parse_state = CSV_PARSE_SEEK; + need_char = 0; + c = CSV_CHAR_ROWSEP; + } + else + { + file->parse_state = CSV_PARSE_NORMAL; + need_char = 0; + c = (uint16_t)byte; + } + } + + return c; +} + +static void hb_trim_end( char *text ) +{ + if( text == NULL ) + { + return; + } + + int i = strlen(text) - 1; + + for( i = strlen(text) - 1; i >= 0 && is_white(text[i]) ; i-- ) + { + text[i] = '\0'; + } +}
\ No newline at end of file diff --git a/test/parsecsv.h b/test/parsecsv.h new file mode 100644 index 000000000..6247ddb44 --- /dev/null +++ b/test/parsecsv.h @@ -0,0 +1,36 @@ +/* $Id: parsecsv.h $ + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.m0k.org/>. + It may be used under the terms of the GNU General Public License. */ + +/* + A very simple CSV file parser. + */ + +typedef struct hb_csv_file_s hb_csv_file_t; +typedef struct hb_csv_cell_s hb_csv_cell_t; + +struct hb_csv_file_s +{ + FILE * fileref; + int eof; + int parse_state; + int curr_row; + int curr_col; +}; + +struct hb_csv_cell_s +{ + char cell_text[1024]; + int cell_row; + int cell_col; +}; + +/* Open a CSV File */ +hb_csv_file_t *hb_open_csv_file( const char *filepath ); +void hb_close_csv_file( hb_csv_file_t *file ); + +/* Parse CSV Cells */ +hb_csv_cell_t *hb_read_next_cell( hb_csv_file_t *file ); +void hb_dispose_cell( hb_csv_cell_t *cell );
\ No newline at end of file diff --git a/test/test.c b/test/test.c index 903b330b6..967cd9c11 100644 --- a/test/test.c +++ b/test/test.c @@ -11,6 +11,7 @@ #include <unistd.h> #include "hb.h" +#include "parsecsv.h" /* Options */ static int debug = HB_DEBUG_NONE; @@ -44,6 +45,7 @@ static int pixelratio = 0; static int chapter_start = 0; static int chapter_end = 0; static int chapter_markers = 0; +static char * marker_file = NULL; static int crf = 0; static char *x264opts = NULL; static char *x264opts2 = NULL; @@ -314,6 +316,53 @@ static int HandleEvents( hb_handle_t * h ) if ( chapter_markers ) { job->chapter_markers = chapter_markers; + + if( marker_file != NULL ) + { + hb_csv_file_t * file = hb_open_csv_file( marker_file ); + hb_csv_cell_t * cell; + int row = 0; + int chapter = 0; + + fprintf( stderr, "Reading chapter markers from file %s\n", marker_file ); + + if( file == NULL ) + { + fprintf( stderr, "Cannot open chapter marker file, using defaults\n" ); + } + else + { + /* Parse the cells */ + while( NULL != ( cell = hb_read_next_cell( file ) ) ) + { + /* We have a chapter number */ + if( cell->cell_col == 0 ) + { + row = cell->cell_row; + chapter = atoi( cell->cell_text ); + } + + /* We have a chapter name */ + if( cell->cell_col == 1 && row == cell->cell_row ) + { + /* If we have a valid chapter, copy the string an terminate it */ + if( chapter >= job->chapter_start && chapter <= job->chapter_end ) + { + hb_chapter_t * chapter_s; + + chapter_s = hb_list_item( job->title->list_chapter, chapter - 1); + strncpy(chapter_s->title, cell->cell_text, 1023); + chapter_s->title[1023] = '\0'; + } + } + + + hb_dispose_cell( cell ); + } + + hb_close_csv_file( file ); + } + } } if( crop[0] >= 0 && crop[1] >= 0 && @@ -650,7 +699,7 @@ static int ParseOptions( int argc, char ** argv ) { "title", required_argument, NULL, 't' }, { "chapters", required_argument, NULL, 'c' }, - { "markers", no_argument, NULL, 'm' }, + { "markers", optional_argument, NULL, 'm' }, { "audio", required_argument, NULL, 'a' }, { "mixdown", required_argument, NULL, '6' }, { "subtitle", required_argument, NULL, 's' }, @@ -740,6 +789,10 @@ static int ParseOptions( int argc, char ** argv ) break; } case 'm': + if( optarg != NULL ) + { + marker_file = strdup( optarg ); + } chapter_markers = 1; break; case 'a': |