WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / batch.c
blobcf751c6d058604212bf07dbd8151ea9c8955267f
1 /* batch.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
10 #include "hb.h"
11 #include "lang.h"
13 struct hb_batch_s
15 char * path;
16 hb_list_t * list_file;
17 hb_handle_t * h;
20 static int compare_str(const void *a, const void *b)
22 return strncmp(*(const char**)a, *(const char**)b, PATH_MAX);
25 /***********************************************************************
26 * hb_batch_init
27 ***********************************************************************
29 **********************************************************************/
30 hb_batch_t * hb_batch_init( hb_handle_t *h, char * path )
32 hb_batch_t * d;
33 hb_stat_t sb;
34 HB_DIR * dir;
35 struct dirent * entry;
36 char * filename;
37 int count, ii;
38 char ** files;
40 if ( hb_stat( path, &sb ) )
41 return NULL;
43 if ( !S_ISDIR( sb.st_mode ) )
44 return NULL;
46 dir = hb_opendir(path);
47 if ( dir == NULL )
48 return NULL;
50 // Count the total number of entries
51 count = 0;
52 while ( (entry = hb_readdir( dir ) ) )
54 count++;
56 files = malloc(count * sizeof(char*));
58 // Find all regular files
59 ii = 0;
60 hb_rewinddir(dir);
61 while ( (entry = hb_readdir( dir ) ) )
63 filename = hb_strdup_printf( "%s" DIR_SEP_STR "%s", path, entry->d_name );
64 if ( hb_stat( filename, &sb ) )
66 free( filename );
67 continue;
70 if ( !S_ISREG( sb.st_mode ) )
72 free( filename );
73 continue;
76 files[ii++] = filename;
78 count = ii;
80 // Sort the files
81 qsort(files, count, sizeof(char*), compare_str);
83 // Create file list
84 d = calloc( sizeof( hb_batch_t ), 1 );
85 d->h = h;
86 d->list_file = hb_list_init();
87 for (ii = 0; ii < count; ii++)
89 hb_list_add( d->list_file, files[ii] );
91 hb_closedir( dir );
92 free(files);
94 if ( hb_list_count( d->list_file ) == 0 )
96 hb_list_close( &d->list_file );
97 free( d );
98 return NULL;
101 d->path = strdup( path );
103 return d;
106 /***********************************************************************
107 * hb_batch_title_count
108 **********************************************************************/
109 int hb_batch_title_count( hb_batch_t * d )
111 return hb_list_count( d->list_file );
114 /***********************************************************************
115 * hb_batch_title_scan
116 **********************************************************************/
117 hb_title_t * hb_batch_title_scan( hb_batch_t * d, int t )
120 hb_title_t * title;
121 char * filename;
122 hb_stream_t * stream;
124 if ( t < 0 )
125 return NULL;
127 filename = hb_list_item( d->list_file, t - 1 );
128 if ( filename == NULL )
129 return NULL;
131 hb_log( "batch: scanning %s", filename );
132 title = hb_title_init( filename, t );
133 stream = hb_stream_open(d->h, filename, title, 1);
134 if ( stream == NULL )
136 hb_title_close( &title );
137 return NULL;
140 title = hb_stream_title_scan( stream, title );
141 hb_stream_close( &stream );
143 return title;
146 /***********************************************************************
147 * hb_batch_close
148 ***********************************************************************
149 * Closes and frees everything
150 **********************************************************************/
151 void hb_batch_close( hb_batch_t ** _d )
153 hb_batch_t * d = *_d;
154 char * filename;
156 while ( ( filename = hb_list_item( d->list_file, 0 ) ) )
158 hb_list_rem( d->list_file, filename );
159 free( filename );
161 hb_list_close( &d->list_file );
162 free( d->path );
163 free( d );
164 *_d = NULL;