strip added smb:// shares of their user/pass when adding, and instead store that...
[vuplus_xbmc] / lib / cmyth / libcmyth / rec_num.c
1 /*
2  *  Copyright (C) 2004-2006, Eric Lund
3  *  http://www.mvpmc.org/
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 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  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 /*
21  * rec_num.c -  functions to manage recorder number structures.  Mostly
22  *              just allocating, freeing, and filling them out.
23  */
24 #include <sys/types.h>
25 #include <stdlib.h>
26 #ifndef _MSC_VER
27 #include <unistd.h>
28 #endif
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <cmyth_local.h>
33
34 /*
35  * cmyth_rec_num_destroy(cmyth_rec_num_t rn)
36  * 
37  * Scope: PRIVATE (static)
38  *
39  * Description
40  *
41  * Destroy and release all storage associated with the recorder number
42  * structure 'rn'.  This function should only ever be called by
43  * cmyth_rec_num_release().  All others should call
44  * cmyth_rec_num_release() to free rec_num structures.
45  *
46  * Return Value:
47  *
48  * None.
49  */
50 static void
51 cmyth_rec_num_destroy(cmyth_rec_num_t rn)
52 {
53         cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
54         if (!rn) {
55                 return;
56         }
57         if (rn->recnum_host) {
58                 ref_release(rn->recnum_host);
59         }
60 }
61
62 /*
63  * cmyth_rec_num_create(void)
64  * 
65  * Scope: PUBLIC
66  *
67  * Description
68  *
69  * Create a recorder number structure.
70  *
71  * Return Value:
72  *
73  * Success: A non-NULL cmyth_rec_num_t (this type is a pointer)
74  *
75  * Failure: A NULL cmyth_rec_num_t
76  */
77 cmyth_rec_num_t
78 cmyth_rec_num_create(void)
79 {
80         cmyth_rec_num_t ret = ref_alloc(sizeof(*ret));
81
82         cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
83         if (!ret) {
84                 return NULL;
85         }
86         ref_set_destroy(ret, (ref_destroy_t)cmyth_rec_num_destroy);
87
88         ret->recnum_host = NULL;
89         ret->recnum_port = 0;
90         ret->recnum_id = 0;
91         return ret;
92 }
93
94 /*
95  * cmyth_rec_num_hold(cmyth_rec_num_t p)
96  * 
97  * Scope: PUBLIC
98  *
99  * Description
100  *
101  * Take a new reference to a rec_num structure.  Rec_Num structures
102  * are reference counted to facilitate caching of pointers to them.
103  * This allows a holder of a pointer to release their hold and trust
104  * that once the last reference is released the rec_num will be
105  * destroyed.  This function is how one creates a new holder of a
106  * rec_num.  This function always returns the pointer passed to it.
107  * While it cannot fail, if it is passed a NULL pointer, it will do
108  * nothing.
109  *
110  * Return Value:
111  *
112  * Success: The value of 'p'
113  *
114  * Failure: There is no real failure case, but a NULL 'p' will result in a
115  *          NULL return.
116  */
117 cmyth_rec_num_t
118 cmyth_rec_num_hold(cmyth_rec_num_t p)
119 {
120         cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
121         return ref_hold(p);
122 }
123
124 /*
125  * cmyth_rec_num_release(cmyth_rec_num_t p)
126  * 
127  * Scope: PUBLIC
128  *
129  * Description
130  *
131  * Release a reference to a rec_num structure.  Rec_Num structures
132  * are reference counted to facilitate caching of pointers to them.
133  * This allows a holder of a pointer to release their hold and trust
134  * that once the last reference is released the rec_num will be
135  * destroyed.  This function is how one drops a reference to a
136  * rec_num.
137  *
138  * Return Value:
139  *
140  * None.
141  */
142 void
143 cmyth_rec_num_release(cmyth_rec_num_t p)
144 {
145         cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
146         ref_release(p);
147 }
148
149 /*
150  * cmyth_rec_num_get(char *host,
151  *                    unsigned short port,
152  *                    unsigned id)
153  * 
154  * Scope: PUBLIC
155  *
156  * Description
157  *
158  * Create a recorder number structure 'rn' using the values 'host',
159  * 'port', and 'id'.
160  *
161  * Return Value:
162  *
163  * Success: A new cmyth_rec_num (this is a ppointer type)
164  *
165  * Failure: NULL
166  */
167 cmyth_rec_num_t 
168 cmyth_rec_num_get(char *host,
169                    unsigned short port,
170                    unsigned id)
171 {
172         cmyth_rec_num_t ret;
173
174         if ((ret = cmyth_rec_num_create()) == NULL) {
175                 return NULL;
176         }
177         ret->recnum_host = ref_strdup(host);
178         if (!ret->recnum_host) {
179                 ref_release(ret);
180                 return NULL;
181         }
182         ret->recnum_port = port;
183         ret->recnum_id = id;
184         return ret;
185 }
186
187 /*
188  * cmyth_rec_num_string(cmyth_rec_num_t rn)
189  * 
190  * Scope: PUBLIC
191  *
192  * Description
193  *
194  * Compose a MythTV protocol string from a rec_num structure and
195  * return a pointer to a malloc'ed buffer containing the string.
196  *
197  * Return Value:
198  *
199  * Success: A non-NULL malloc'ed character buffer pointer.
200  *
201  * Failure: NULL
202  */
203 char *
204 cmyth_rec_num_string(cmyth_rec_num_t rn)
205 {
206         unsigned len = sizeof("[]:[][]:[]");
207         char id[16];
208         char port[8];
209         char *ret;
210
211         if (!rn) {
212                 return NULL;
213         }
214         if (!rn->recnum_host) {
215                 return NULL;
216         }
217         sprintf(id, "%d", rn->recnum_id);
218         len += strlen(id);
219         sprintf(port, "%d", rn->recnum_port);
220         len += strlen(port);
221         len += strlen(rn->recnum_host);
222         ret = malloc((len + 1) * sizeof(char));
223         if (!ret) {
224                 return NULL;
225         }
226         strcpy(ret, id);
227         strcat(ret, "[]:[]");
228         strcat(ret, rn->recnum_host);
229         strcat(ret, "[]:[]");
230         strcat(ret, port);
231         return ret;
232 }