1 /* Reading Desktop Entry files.
2    Copyright (C) 1995-1998, 2000-2003, 2005-2006, 2008-2009, 2014-2016, 2020 Free Software Foundation, Inc.
3    This file was written by Daiki Ueno <ueno@gnu.org>.
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program 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
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
17 
18 #ifndef _READ_DESKTOP_H
19 #define _READ_DESKTOP_H
20 
21 #include <sys/types.h>
22 #include <stdio.h>
23 #include "mem-hash-map.h"
24 #include "po-lex.h"
25 #include "str-list.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /* Forward declaration.  */
32 struct desktop_reader_ty;
33 
34 
35 /* This first structure, playing the role of the "Class" in OO sense,
36    contains pointers to functions.  Each function is a method for the
37    class (base or derived).  Use a NULL pointer where no action is
38    required.  */
39 
40 typedef struct desktop_reader_class_ty desktop_reader_class_ty;
41 struct desktop_reader_class_ty
42 {
43   /* how many bytes to malloc for an instance of this class */
44   size_t size;
45 
46   /* what to do immediately after the instance is malloc()ed */
47   void (*constructor) (struct desktop_reader_ty *pop);
48 
49   /* what to do immediately before the instance is free()ed */
50   void (*destructor) (struct desktop_reader_ty *pop);
51 
52   /* what to do with a group header */
53   void (*handle_group) (struct desktop_reader_ty *pop,
54                         const char *group);
55 
56   /* what to do with a key/value pair */
57   void (*handle_pair) (struct desktop_reader_ty *pop,
58                        lex_pos_ty *key_pos,
59                        const char *key,
60                        const char *locale,
61                        const char *value);
62 
63   /* what to do with a comment */
64   void (*handle_comment) (struct desktop_reader_ty *pop, const char *s);
65 
66   /* what to do with a blank line */
67   void (*handle_blank) (struct desktop_reader_ty *pop, const char *s);
68 };
69 
70 /* This next structure defines the base class passed to the methods.
71    Derived methods will often need to cast their first argument before
72    using it (this corresponds to the implicit 'this' argument in C++).
73 
74    When declaring derived classes, use the DESKTOP_READER_TY define
75    at the start of the structure, to declare inherited instance variables,
76    etc.  */
77 
78 #define DESKTOP_READER_TY              \
79   desktop_reader_class_ty *methods;
80 
81 typedef struct desktop_reader_ty desktop_reader_ty;
82 struct desktop_reader_ty
83 {
84   DESKTOP_READER_TY
85 };
86 
87 extern desktop_reader_ty *
88        desktop_reader_alloc (desktop_reader_class_ty *methods);
89 extern void desktop_reader_free (desktop_reader_ty *reader);
90 
91 extern void desktop_reader_handle_group (desktop_reader_ty *reader,
92                                          const char *group);
93 
94 extern void desktop_reader_handle_pair (desktop_reader_ty *reader,
95                                         lex_pos_ty *key_pos,
96                                  const char *key,
97                                  const char *locale,
98                                  const char *value);
99 
100 extern void desktop_reader_handle_comment (desktop_reader_ty *reader,
101                                            const char *s);
102 
103 extern void desktop_reader_handle_blank (desktop_reader_ty *reader,
104                                          const char *s);
105 
106 
107 extern void desktop_parse (desktop_reader_ty *reader, FILE *file,
108                            const char *real_filename,
109                            const char *logical_filename);
110 
111 
112 extern char *desktop_escape_string (const char *s, bool is_list);
113 extern char *desktop_unescape_string (const char *s, bool is_list);
114 
115 extern void desktop_add_keyword (hash_table *keywords, const char *name,
116                                  bool is_list);
117 extern void desktop_add_default_keywords (hash_table *keywords);
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 
123 
124 #endif /* _READ_DESKTOP_H */
125