ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
playlist.c File Reference

Go to the source code of this file.

Functions

asciichat_error_t media_playlist_append (media_playlist_t *list, const char *path, bool url)
asciichat_error_t media_playlist_parse (const char *text, media_playlist_t *list)
asciichat_error_t media_playlist_format (const media_playlist_t *list, char *text, size_t capacity)
bool media_playlist_adjacent (const media_playlist_t *list, int direction, size_t *index)

Function Documentation

◆ media_playlist_adjacent()

bool media_playlist_adjacent ( const media_playlist_t * list,
int direction,
size_t * index )

Find an adjacent item without wrapping at playlist boundaries.

Definition at line 128 of file playlist.c.

129 {
130 if (!list || !index || !list->count || list->index >= list->count ||
131 (direction != -1 && direction != 1))
132 return false;
133 if (direction < 0 && list->index) {
134 *index = list->index - 1;
135 return true;
136 }
137 if (direction > 0 && list->index + 1 < list->count) {
138 *index = list->index + 1;
139 return true;
140 }
141 return false;
142}
static bool size_t index
Definition queries.h:10

References media_playlist_t::count, index, and media_playlist_t::index.

Referenced by session_capture_at_end().

◆ media_playlist_append()

asciichat_error_t media_playlist_append ( media_playlist_t * list,
const char * path,
bool url )

Append one already separated argument; preserves insertion order.

Definition at line 7 of file playlist.c.

8 {
9 if (!list || !path || list->count >= MEDIA_PLAYLIST_MAX_ITEMS)
11 "A playlist supports at most %d artifacts",
13 char normalized[MEDIA_PLAYLIST_PATH_MAX];
14 asciichat_error_t result =
15 media_input_normalize(path, normalized, sizeof(normalized));
16 if (result != ASCIICHAT_OK)
17 return result;
18 if (url && !strstr(normalized, "://")) {
19 char absolute[MEDIA_PLAYLIST_PATH_MAX];
20 int written =
21 snprintf(absolute, sizeof(absolute), "http://%.*s",
22 (int)(sizeof(absolute) - sizeof("http://")), normalized);
23 if (written < 0 || (size_t)written >= sizeof(absolute))
24 return SET_ERRNO(ERROR_INVALID_PARAM, "Media URL is too long");
25 const size_t absolute_bytes = strlen(absolute) + 1;
26 memcpy(normalized, absolute, absolute_bytes);
27 }
28 if (strcmp(normalized, "-") == 0 && list->count)
30 "Stdin must be the only playlist artifact");
31 if (list->count && strcmp(list->paths[0], "-") == 0)
33 "Stdin must be the only playlist artifact");
34 const size_t normalized_bytes = strlen(normalized) + 1;
35 memcpy(list->paths[list->count], normalized, normalized_bytes);
36 list->urls[list->count++] = url;
37 return ASCIICHAT_OK;
38}
#define SET_ERRNO(code,...)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
asciichat_error_t media_input_normalize(const char *text, char *output, size_t capacity)
Definition media/input.c:14
#define MEDIA_PLAYLIST_PATH_MAX
Definition playlist.h:11
#define MEDIA_PLAYLIST_MAX_ITEMS
Definition playlist.h:10
bool urls[64]
Definition playlist.h:15
char paths[64][256]
Definition playlist.h:14

References ASCIICHAT_OK, media_playlist_t::count, ERROR_INVALID_PARAM, media_input_normalize(), MEDIA_PLAYLIST_MAX_ITEMS, MEDIA_PLAYLIST_PATH_MAX, media_playlist_t::paths, SET_ERRNO, and media_playlist_t::urls.

Referenced by media_playlist_parse(), session_capture_create(), session_capture_open_media(), and ui_input_read_key().

◆ media_playlist_format()

asciichat_error_t media_playlist_format ( const media_playlist_t * list,
char * text,
size_t capacity )

Render one quoted artifact per line, with no trailing whitespace.

Definition at line 103 of file playlist.c.

104 {
105 if (!list || !text || !capacity)
106 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid playlist display buffer");
107 size_t used = 0;
108 text[0] = '\0';
109 for (size_t i = 0; i < list->count; ++i) {
110 const char *path = list->paths[i];
111 char quote = strchr(path, '"') ? '\'' : '"';
112 if (strchr(path, quote))
113 return SET_ERRNO(ERROR_INVALID_PARAM, "Cannot quote media artifact");
114 size_t bytes = strlen(path);
115 if (used + bytes + 3 + (i != 0) > capacity)
116 return SET_ERRNO(ERROR_INVALID_PARAM, "Playlist display is too long");
117 if (i)
118 text[used++] = '\n';
119 text[used++] = quote;
120 memcpy(text + used, path, bytes);
121 used += bytes;
122 text[used++] = quote;
123 text[used] = '\0';
124 }
125 return ASCIICHAT_OK;
126}

References ASCIICHAT_OK, media_playlist_t::count, ERROR_INVALID_PARAM, media_playlist_t::paths, and SET_ERRNO.

Referenced by session_handle_keyboard_input(), and ui_input_read_key().

◆ media_playlist_parse()

asciichat_error_t media_playlist_parse ( const char * text,
media_playlist_t * list )

Parse shell-quoted paths, file URIs or URLs. A lone unquoted path may contain spaces.

Definition at line 40 of file playlist.c.

41 {
42 if (!text || !list)
43 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid playlist input");
44 media_playlist_t parsed = {0};
45 while (isspace((unsigned char)*text))
46 ++text;
47 if (text[0] == '&' && text[1] == ' ')
48 text += 2;
49 while (*text) {
50 while (isspace((unsigned char)*text))
51 ++text;
52 if (!*text)
53 break;
54 const char *start = text;
55 if (*text == '\'' || *text == '"') {
56 char quote = *text++;
57 while (*text && *text != quote)
58 ++text;
59 if (!*text || (text[1] && !isspace((unsigned char)text[1])))
60 return SET_ERRNO(
62 "Separate media artifacts with whitespace and match their quotes");
63 ++text;
64 } else {
65 // Preserve a manually typed path containing spaces. Newline-delimited
66 // clipboard URI lists remain separate artifacts.
67 while (*text && *text != '\n' && *text != '\r') {
68 if (isspace((unsigned char)*text)) {
69 const char *next = text;
70 while (*next == ' ' || *next == '\t')
71 ++next;
72 bool path_start = *next == '\'' || *next == '"' || *next == '/' ||
73 *next == '\\' ||
74 (isalpha((unsigned char)next[0]) && next[1] == ':');
75 const char *scheme = strstr(next, "://");
76 const char *space = strpbrk(next, " \t\r\n");
77 if (path_start || (scheme && (!space || scheme < space)))
78 break;
79 }
80 ++text;
81 }
82 }
83 size_t bytes = (size_t)(text - start);
84 char argument[MEDIA_PLAYLIST_PATH_MAX + 2];
85 if (bytes >= sizeof(argument))
86 return SET_ERRNO(ERROR_INVALID_PARAM, "Media artifact is too long");
87 memcpy(argument, start, bytes);
88 argument[bytes] = '\0';
89 bool url = strstr(argument, "://") &&
90 strncmp(argument + (argument[0] == '\'' || argument[0] == '"'),
91 "file://", sizeof("file://") - 1) != 0;
92 asciichat_error_t result = media_playlist_append(&parsed, argument, url);
93 if (result != ASCIICHAT_OK)
94 return result;
95 }
96 if (!parsed.count)
98 "Enter at least one media path or URL");
99 *list = parsed;
100 return ASCIICHAT_OK;
101}
asciichat_error_t media_playlist_append(media_playlist_t *list, const char *path, bool url)
Definition playlist.c:7

References ASCIICHAT_OK, media_playlist_t::count, ERROR_INVALID_PARAM, media_playlist_append(), MEDIA_PLAYLIST_PATH_MAX, and SET_ERRNO.

Referenced by session_capture_open_playlist(), session_handle_keyboard_input(), ui_input_media_take(), and ui_input_read_key().