splitmuxsink: Allow time and bytes to reach their respective thresholds
[platform/upstream/gst-plugins-good.git] / gst / multifile / gstsplitutils.c
1 /* GStreamer Split Source Utility Functions
2  * Copyright (C) 2011 Collabora Ltd. <tim.muller@collabora.co.uk>
3  * Copyright (C) 2014 Jan Schmidt <jan@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <string.h>
26
27 #include "gstsplitutils.h"
28 #include "patternspec.h"
29
30 static int
31 gst_split_util_array_sortfunc (gchar ** a, gchar ** b)
32 {
33   return strcmp (*a, *b);
34 }
35
36 gchar **
37 gst_split_util_find_files (const gchar * dirname,
38     const gchar * basename, GError ** err)
39 {
40   PatternSpec *pspec;
41   GPtrArray *files;
42   const gchar *name;
43   GDir *dir;
44
45   if (dirname == NULL || basename == NULL)
46     goto invalid_location;
47
48   GST_INFO ("checking in directory '%s' for pattern '%s'", dirname, basename);
49
50   dir = g_dir_open (dirname, 0, err);
51   if (dir == NULL)
52     return NULL;
53
54   if (DEFAULT_PATTERN_MATCH_MODE == MATCH_MODE_UTF8 &&
55       !g_utf8_validate (basename, -1, NULL)) {
56     goto not_utf8;
57   }
58
59   /* mode will be AUTO on linux/unix and UTF8 on win32 */
60   pspec = pattern_spec_new (basename, DEFAULT_PATTERN_MATCH_MODE);
61
62   files = g_ptr_array_new ();
63
64   while ((name = g_dir_read_name (dir))) {
65     GST_TRACE ("check: %s", name);
66     if (pattern_match_string (pspec, name)) {
67       GST_DEBUG ("match: %s", name);
68       g_ptr_array_add (files, g_build_filename (dirname, name, NULL));
69     }
70   }
71
72   if (files->len == 0)
73     goto no_matches;
74
75   g_ptr_array_sort (files, (GCompareFunc) gst_split_util_array_sortfunc);
76   g_ptr_array_add (files, NULL);
77
78   pattern_spec_free (pspec);
79   g_dir_close (dir);
80
81   return (gchar **) g_ptr_array_free (files, FALSE);
82
83 /* ERRORS */
84 invalid_location:
85   {
86     g_set_error_literal (err, G_FILE_ERROR, G_FILE_ERROR_INVAL,
87         "No filename specified.");
88     return NULL;
89   }
90 not_utf8:
91   {
92     g_dir_close (dir);
93     g_set_error_literal (err, G_FILE_ERROR, G_FILE_ERROR_INVAL,
94         "Filename pattern must be UTF-8 on Windows.");
95     return NULL;
96   }
97 no_matches:
98   {
99     pattern_spec_free (pspec);
100     g_dir_close (dir);
101     g_set_error_literal (err, G_FILE_ERROR, G_FILE_ERROR_NOENT,
102         "Found no files matching the pattern.");
103     return NULL;
104   }
105 }