adopt to winsock
[melted] / src / mvcp / mvcp_status.c
1 /*
2 * mvcp_status.c -- Unit Status Handling
3 * Copyright (C) 2002-2009 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /* System header files */
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* Application header files */
27 #include "mvcp_status.h"
28 #include "mvcp_tokeniser.h"
29 #include "mvcp_util.h"
30
31 /** Parse a unit status string.
32 */
33
34 void mvcp_status_parse( mvcp_status status, char *text )
35 {
36 mvcp_tokeniser tokeniser = mvcp_tokeniser_init( );
37 int r = mvcp_tokeniser_parse_new( tokeniser, text, " " );
38 if ( 19 == r || 17 == r )
39 {
40 status->unit = atoi( mvcp_tokeniser_get_string( tokeniser, 0 ) );
41 strncpy( status->clip, mvcp_util_strip( mvcp_tokeniser_get_string( tokeniser, 2 ), '\"' ), sizeof( status->clip ) );
42 status->position = atol( mvcp_tokeniser_get_string( tokeniser, 3 ) );
43 status->speed = atoi( mvcp_tokeniser_get_string( tokeniser, 4 ) );
44 status->fps = atof( mvcp_tokeniser_get_string( tokeniser, 5 ) );
45 status->in = atol( mvcp_tokeniser_get_string( tokeniser, 6 ) );
46 status->out = atol( mvcp_tokeniser_get_string( tokeniser, 7 ) );
47 status->length = atol( mvcp_tokeniser_get_string( tokeniser, 8 ) );
48
49 strncpy( status->tail_clip, mvcp_util_strip( mvcp_tokeniser_get_string( tokeniser, 9 ), '\"' ), sizeof( status->tail_clip ) );
50 status->tail_position = atol( mvcp_tokeniser_get_string( tokeniser, 10 ) );
51 status->tail_in = atol( mvcp_tokeniser_get_string( tokeniser, 11 ) );
52 status->tail_out = atol( mvcp_tokeniser_get_string( tokeniser, 12 ) );
53 status->tail_length = atol( mvcp_tokeniser_get_string( tokeniser, 13 ) );
54 status->seek_flag = atoi( mvcp_tokeniser_get_string( tokeniser, 14 ) );
55 status->generation = atoi( mvcp_tokeniser_get_string( tokeniser, 15 ) );
56 status->clip_index = atoi( mvcp_tokeniser_get_string( tokeniser, 16 ) );
57 if ( 19 == r )
58 {
59 status->start = atoi( mvcp_tokeniser_get_string( tokeniser, 17 ) );
60 status->dur = atoi( mvcp_tokeniser_get_string( tokeniser, 18 ) );
61 };
62
63 if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "unknown" ) )
64 status->status = unit_unknown;
65 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "undefined" ) )
66 status->status = unit_undefined;
67 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "offline" ) )
68 status->status = unit_offline;
69 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "not_loaded" ) )
70 status->status = unit_not_loaded;
71 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "stopped" ) )
72 status->status = unit_stopped;
73 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "paused" ) )
74 status->status = unit_paused;
75 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "playing" ) )
76 status->status = unit_playing;
77 else if ( !strcmp( mvcp_tokeniser_get_string( tokeniser, 1 ), "disconnected" ) )
78 status->status = unit_disconnected;
79 }
80 else
81 {
82 memset( status, 0, sizeof( mvcp_status_t ) );
83 fprintf( stderr, "Status thread changed?\n" );
84 }
85 mvcp_tokeniser_close( tokeniser );
86 }
87
88 /** Serialise a status into a string.
89 */
90
91 char *mvcp_status_serialise( mvcp_status status, char *text, int length )
92 {
93 const char *status_string = NULL;
94
95 switch( status->status )
96 {
97 case unit_undefined:
98 status_string = "undefined";
99 break;
100
101 case unit_offline:
102 status_string = "offline";
103 break;
104
105 case unit_not_loaded:
106 status_string = "not_loaded";
107 break;
108
109 case unit_stopped:
110 status_string = "stopped";
111 break;
112
113 case unit_playing:
114 status_string = "playing";
115 break;
116
117 case unit_unknown:
118 status_string = "unknown";
119 break;
120
121 case unit_paused:
122 status_string = "paused";
123 break;
124
125 case unit_disconnected:
126 status_string = "disconnected";
127 break;
128 }
129
130 snprintf( text, length, "%d %s \"%s\" %d %d %.2f %d %d %d \"%s\" %d %d %d %d %d %d %d %d %d\r\n",
131 status->unit,
132 status_string,
133 status->clip,
134 status->position,
135 status->speed,
136 status->fps,
137 status->in,
138 status->out,
139 status->length,
140 status->tail_clip,
141 status->tail_position,
142 status->tail_in,
143 status->tail_out,
144 status->tail_length,
145 status->seek_flag,
146 status->generation,
147 status->clip_index,
148 status->start,
149 status->dur );
150
151 return text;
152 }
153
154 /** Compare two status codes for changes.
155 */
156
157 int mvcp_status_compare( mvcp_status status1, mvcp_status status2 )
158 {
159 return memcmp( status1, status2, sizeof( mvcp_status_t ) );
160 }
161
162 /** Copy status code info from dest to src.
163 */
164
165 mvcp_status mvcp_status_copy( mvcp_status dest, mvcp_status src )
166 {
167 return memcpy( dest, src, sizeof( mvcp_status_t ) );
168 }