]> git.sesse.net Git - ffmpeg/blob - doc/examples/avio_list_dir.c
doc/examples: Handle new types in avio_list_dir
[ffmpeg] / doc / examples / avio_list_dir.c
1 /*
2  * Copyright (c) 2014 Lukasz Marek
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22
23 #include <libavcodec/avcodec.h>
24 #include <libavformat/avformat.h>
25 #include <libavformat/avio.h>
26
27 static const char *type_string(int type)
28 {
29     switch (type) {
30     case AVIO_ENTRY_DIRECTORY:
31         return "<DIR>";
32     case AVIO_ENTRY_FILE:
33         return "<FILE>";
34     case AVIO_ENTRY_BLOCK_DEVICE:
35         return "<BLOCK DEVICE>";
36     case AVIO_ENTRY_CHARACTER_DEVICE:
37         return "<CHARACTER DEVICE>";
38     case AVIO_ENTRY_NAMED_PIPE:
39         return "<PIPE>";
40     case AVIO_ENTRY_SYMBOLIC_LINK:
41         return "<LINK>";
42     case AVIO_ENTRY_SOCKET:
43         return "<SOCKET>";
44     case AVIO_ENTRY_SERVER:
45         return "<SERVER>";
46     case AVIO_ENTRY_SHARE:
47         return "<SHARE>";
48     case AVIO_ENTRY_WORKGROUP:
49         return "<WORKGROUP>";
50     case AVIO_ENTRY_UNKNOWN:
51     default:
52         break;
53     }
54     return "<UNKNOWN>";
55 }
56
57 int main(int argc, char *argv[])
58 {
59     const char *input_dir = NULL;
60     AVIODirEntry *entry = NULL;
61     AVIODirContext *ctx = NULL;
62     int cnt, ret;
63     char filemode[4], uid_and_gid[20];
64
65     av_log_set_level(AV_LOG_DEBUG);
66
67     if (argc != 2) {
68         fprintf(stderr, "usage: %s input_dir\n"
69                 "API example program to show how to list files in directory "
70                 "accessed through AVIOContext.\n", argv[0]);
71         return 1;
72     }
73     input_dir = argv[1];
74
75     /* register codecs and formats and other lavf/lavc components*/
76     av_register_all();
77
78     if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
79         av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
80         return 1;
81     }
82
83     cnt = 0;
84     for (;;) {
85         if ((ret = avio_read_dir(ctx, &entry)) < 0) {
86             av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
87             return 1;
88         }
89         if (!entry)
90             break;
91         if (entry->filemode == -1) {
92             snprintf(filemode, 4, "???");
93         } else {
94             snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
95         }
96         snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
97         if (cnt == 0)
98             av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
99                    "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
100                    "ACCESSED", "STATUS_CHANGED");
101         av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
102                type_string(entry->type),
103                entry->size,
104                entry->name,
105                uid_and_gid,
106                filemode,
107                entry->modification_timestamp,
108                entry->access_timestamp,
109                entry->status_change_timestamp);
110         avio_free_directory_entry(&entry);
111         cnt++;
112     };
113
114     avio_close_dir(&ctx);
115
116     return 0;
117 }