summaryrefslogtreecommitdiffstats
path: root/libhb/batch.c
blob: 45fcee1a815b537d04bc37082b79ae8691ca2079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* batch.c

   Copyright (c) 2003-2014 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */
 
#include "hb.h"
#include "lang.h"

struct hb_batch_s
{
    char      * path;
    hb_list_t * list_file;
};

static int compare_str(const void *a, const void *b)
{
    return strncmp(*(const char**)a, *(const char**)b, PATH_MAX);
}

/***********************************************************************
 * hb_batch_init
 ***********************************************************************
 *
 **********************************************************************/
hb_batch_t * hb_batch_init( char * path )
{
    hb_batch_t    * d;
    hb_stat_t       sb;
    HB_DIR        * dir;
    struct dirent * entry;
    char          * filename;
    int             count, ii;
    char         ** files;

    if ( hb_stat( path, &sb ) )
        return NULL;

    if ( !S_ISDIR( sb.st_mode ) )
        return NULL;

    dir = hb_opendir(path);
    if ( dir == NULL )
        return NULL;

    // Count the total number of entries
    while ( (entry = hb_readdir( dir ) ) )
    {
        count++;
    }
    files = malloc(count * sizeof(char*));

    // Find all regular files
    ii = 0;
    hb_rewinddir(dir);
    while ( (entry = hb_readdir( dir ) ) )
    {
        filename = hb_strdup_printf( "%s" DIR_SEP_STR "%s", path, entry->d_name );
        if ( hb_stat( filename, &sb ) )
        {
            free( filename );
            continue;
        }

        if ( !S_ISREG( sb.st_mode ) )
        {
            free( filename );
            continue;
        }

        files[ii++] = filename;
    }
    count = ii;

    // Sort the files
    qsort(files, count, sizeof(char*), compare_str);

    // Create file list
    d = calloc( sizeof( hb_batch_t ), 1 );
    d->list_file = hb_list_init();
    for (ii = 0; ii < count; ii++)
    {
        hb_list_add( d->list_file, files[ii] );
    }
    hb_closedir( dir );
    free(files);

    if ( hb_list_count( d->list_file ) == 0 )
    {
        hb_list_close( &d->list_file );
        free( d );
        return NULL;
    }

    d->path = strdup( path );

    return d;
}

/***********************************************************************
 * hb_batch_title_count
 **********************************************************************/
int hb_batch_title_count( hb_batch_t * d )
{
    return hb_list_count( d->list_file );
}

/***********************************************************************
 * hb_batch_title_scan
 **********************************************************************/
hb_title_t * hb_batch_title_scan( hb_batch_t * d, int t )
{

    hb_title_t   * title;
    char         * filename;
    hb_stream_t  * stream;

    if ( t < 0 )
        return NULL;

    filename = hb_list_item( d->list_file, t - 1 );
    if ( filename == NULL )
        return NULL;

    hb_log( "batch: scanning %s", filename );
    title = hb_title_init( filename, 0 );
    stream = hb_stream_open( filename, title, 1 );
    if ( stream == NULL )
    {
        hb_title_close( &title );
        return NULL;
    }

    title = hb_stream_title_scan( stream, title );
    hb_stream_close( &stream );
    if ( title != NULL )
    {
        title->index = t;
    }

    return title;
}

/***********************************************************************
 * hb_batch_close
 ***********************************************************************
 * Closes and frees everything
 **********************************************************************/
void hb_batch_close( hb_batch_t ** _d )
{
    hb_batch_t * d = *_d;
    char       * filename;

    while ( ( filename = hb_list_item( d->list_file, 0 ) ) )
    {
        hb_list_rem( d->list_file, filename );
        free( filename );
    }
    hb_list_close( &d->list_file );
    free( d->path );
    free( d );
    *_d = NULL;
}