File: parser.h

package info (click to toggle)
libmpdclient 2.22-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: ansic: 9,923; makefile: 9
file content (156 lines) | stat: -rw-r--r-- 3,698 bytes parent folder | download
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
// SPDX-License-Identifier: BSD-2-Clause
// Copyright The Music Player Daemon Project

#ifndef LIBMPDCLIENT_PARSER_H
#define LIBMPDCLIENT_PARSER_H

#include "protocol.h"
#include "compiler.h"

#include <stdbool.h>

enum mpd_parser_result {
	/**
	 * Response line was not understood.
	 */
	MPD_PARSER_MALFORMED,

	/**
	 * MPD has returned "OK" or "list_OK" (check with
	 * mpd_parser_is_discrete()).
	 */
	MPD_PARSER_SUCCESS,

	/**
	 * MPD has returned "ACK" with an error code.  Call
	 * mpd_parser_get_server_error() to get the error code.
	 */
	MPD_PARSER_ERROR,

	/**
	 * MPD has returned a name-value pair.  Call
	 * mpd_parser_get_name() and mpd_parser_get_value().
	 */
	MPD_PARSER_PAIR,
};

/**
 * \struct mpd_parser
 *
 * This opaque object is a low-level parser for the MPD protocol.  You
 * feed it with input lines, and it provides parsed representations.
 */
struct mpd_parser;

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Allocates a new mpd_parser object.  Returns NULL on error (out of
 * memory).
 */
mpd_malloc
struct mpd_parser *
mpd_parser_new(void);

/**
 * Frees a mpd_parser object.
 */
void
mpd_parser_free(struct mpd_parser *parser);

/**
 * Feeds a line (without the trailing newline character) received from
 * MPD / mpd_async_recv_line() into the parser.
 *
 * Note that the line parameter is writable, because the parser will
 * modify it.  The functions mpd_parser_get_name() and
 * mpd_parser_get_value() will return pointers inside this buffer.
 * This means that after passing the line to this function, you must
 * not modify or free it, until the name and value pointers are not
 * used anymore.
 *
 * @param parser the #mpd_parser object
 * @param line a line received from the MPD server
 * @return a result code indicating the type of line, or error
 */
enum mpd_parser_result
mpd_parser_feed(struct mpd_parser *parser, char *line);

/**
 * Call this when mpd_parser_feed() has returned #MPD_PARSER_SUCCESS
 * to find out whether this is an "OK" (false) or a "list_OK" (true)
 * response.
 *
 * @param parser the #mpd_parser object
 */
mpd_pure
bool
mpd_parser_is_discrete(const struct mpd_parser *parser);

/**
 * Call this when mpd_parser_feed() has returned #MPD_PARSER_ERROR to
 * obtain the reason for the error.
 *
 * @param parser the #mpd_parser object
 */
mpd_pure
enum mpd_server_error
mpd_parser_get_server_error(const struct mpd_parser *parser);

/**
 * On #MPD_PARSER_ERROR, this returns the number of the list command
 * which failed.  Don't call this outside of a command list.
 *
 * @param parser the #mpd_parser object
 */
mpd_pure
unsigned
mpd_parser_get_at(const struct mpd_parser *parser);

/**
 * On #MPD_PARSER_ERROR, this returns the human readable error message
 * returned by MPD (UTF-8).
 *
 * This returns a pointer into the line buffer passed to
 * mpd_parser_feed().  It is valid as long as the buffer is not
 * freed/modified.
 *
 * @param parser the #mpd_parser object
 */
mpd_pure
const char *
mpd_parser_get_message(const struct mpd_parser *parser);

/**
 * On #MPD_PARSER_PAIR, this returns the name.
 *
 * This returns a pointer into the line buffer passed to
 * mpd_parser_feed().  It is valid as long as the buffer is not
 * freed/modified.
 *
 * @param parser the #mpd_parser object
 */
mpd_pure
const char *
mpd_parser_get_name(const struct mpd_parser *parser);

/**
 * On #MPD_PARSER_PAIR, this returns the value.
 *
 * This returns a pointer into the line buffer passed to
 * mpd_parser_feed().  It is valid as long as the buffer is not
 * freed/modified.
 *
 * @param parser the #mpd_parser object
 */
mpd_pure
const char *
mpd_parser_get_value(const struct mpd_parser *parser);

#ifdef __cplusplus
}
#endif

#endif