|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Framework Manager. Parses the CFG file |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <stdio.h> |
|
19 #include <string.h> |
|
20 #include "xaframeworkmgr.h" |
|
21 |
|
22 /* Default line width permitted in the cfg file + 2 to hold "\r\n"*/ |
|
23 #define LINEWIDTH 82 |
|
24 |
|
25 typedef enum |
|
26 { |
|
27 FWMgrTagNone, |
|
28 FWMgrTagURIScheme, |
|
29 FWMgrTagFileExt |
|
30 } FWMgrTagType; |
|
31 |
|
32 /* Config file location */ |
|
33 const char configFileLocationZ[] = "z:/openmaxal/openmaxal.cfg"; |
|
34 const char configFileLocationC[] = "c:/openmaxal/openmaxal.cfg"; |
|
35 |
|
36 /* Tags used for parsing */ |
|
37 const char mediaPlayerBeginTag[] = "<mediaplayer>"; |
|
38 const char mediaPlayerEndTag[] = "</mediaplayer>"; |
|
39 const char mediaRecorderBeginTag[] = "<mediarecorder>"; |
|
40 const char mediaRecorderEndTag[] = "</mediarecorder>"; |
|
41 const char mediaFrameworkMmfBeginTag[] = "<mmf>"; |
|
42 const char mediaFrameworkMmfEndTag[] = "</mmf>"; |
|
43 const char mediaFrameworkGstBeginTag[] = "<gst>"; |
|
44 const char uriSchemeBeginTag[] = "<urischeme>"; |
|
45 const char uriSchemeEndTag[] = "</urischeme>"; |
|
46 const char mediaFrameworkGstEndTag[] = "</gst>"; |
|
47 const char fileExtBeginTag[] = "<fileext>"; |
|
48 const char fileExtEndTag[] = "</fileext>"; |
|
49 |
|
50 /* Local function definitions */ |
|
51 /* returns FWMgrTrue if processed successfully */ |
|
52 static FWMgrBool processConfigEntry(const char* buffer, |
|
53 FWMgrMOType *mediaType, FWMgrFwType *frameworkType, |
|
54 FWMgrTagType *tagType, FWMgrBool *newNode, FrameworkMap **node); |
|
55 |
|
56 /* returns FWMgrTrue if processed successfully */ |
|
57 static FWMgrBool processTagType(const char* buffer, |
|
58 FWMgrFwType *frameworkType, FWMgrTagType *tagType, |
|
59 FrameworkMap **node); |
|
60 |
|
61 /* returns FWMgrTrue if processed successfully */ |
|
62 static FWMgrBool tokenizeTag(FWMgrTagType tagType, const char* buffer, |
|
63 FrameworkMap **node); |
|
64 |
|
65 /* Global functions from header file */ |
|
66 |
|
67 /* FrameworkMap* XAFrameworkMgr_CreateFrameworkMap |
|
68 * Description: Creates a list of framework and use-case map. |
|
69 */ |
|
70 FrameworkMap* XAFrameworkMgr_CreateFrameworkMap() |
|
71 { |
|
72 char buffer[LINEWIDTH]; |
|
73 int readSize; |
|
74 int lineNumber = 0; |
|
75 FWMgrBool processedEntry = FWMgrTrue; |
|
76 FWMgrMOType currentMediaType = FWMgrMOUnknown; |
|
77 FWMgrFwType currentFrameworkType = FWMgrFWUknown; |
|
78 FWMgrTagType currentTagType = FWMgrTagNone; |
|
79 FrameworkMap *curNode = NULL; |
|
80 FWMgrBool newNode; |
|
81 FrameworkMap *frameworkMap = NULL; |
|
82 |
|
83 FILE* fp = fopen(configFileLocationC, "r"); |
|
84 if (fp == NULL) |
|
85 { |
|
86 fp = fopen(configFileLocationZ, "r"); |
|
87 } |
|
88 |
|
89 if (fp != NULL) |
|
90 { |
|
91 while ((fgets(buffer, LINEWIDTH, fp) != NULL) && processedEntry) |
|
92 { |
|
93 /* keep looping until NULL pointer OR error... */ |
|
94 lineNumber++; |
|
95 readSize = strlen(buffer); |
|
96 /* Ignore comments line */ |
|
97 if (buffer[0] == '#') |
|
98 continue; |
|
99 |
|
100 /* Ignore replace "\r\n" with '\0' */ |
|
101 if ((readSize >= 2) && (buffer[readSize - 2] == '\r') |
|
102 && (buffer[readSize - 1] == '\n')) |
|
103 buffer[readSize - 2] = '\0'; |
|
104 |
|
105 /* Ignore new line... */ |
|
106 if (readSize == 2) |
|
107 continue; |
|
108 |
|
109 processedEntry = processConfigEntry(buffer, ¤tMediaType, |
|
110 ¤tFrameworkType, ¤tTagType, &newNode, |
|
111 &curNode); |
|
112 if (newNode) |
|
113 { |
|
114 /*Just link to the last element in the chain*/ |
|
115 if (!frameworkMap) |
|
116 { |
|
117 frameworkMap = curNode; |
|
118 } |
|
119 else |
|
120 { |
|
121 FrameworkMap *lastNode = frameworkMap; |
|
122 while (lastNode->next) |
|
123 { |
|
124 lastNode = lastNode->next; |
|
125 } |
|
126 lastNode->next = curNode; |
|
127 } |
|
128 } |
|
129 } |
|
130 fclose(fp); |
|
131 } |
|
132 else |
|
133 { |
|
134 printf("unable to open config file!\n"); |
|
135 } |
|
136 return frameworkMap; |
|
137 } |
|
138 |
|
139 #ifdef _DEBUG |
|
140 /* void XAFrameworkMgr_DumpFrameworkMap |
|
141 * Description: Prints map to std console. |
|
142 */ |
|
143 void XAFrameworkMgr_DumpFrameworkMap(FrameworkMap *map) |
|
144 { |
|
145 FrameworkMap *node = map; |
|
146 int i; |
|
147 int loopIndex = 0; |
|
148 while (node) |
|
149 { |
|
150 loopIndex++; |
|
151 printf("%d>", loopIndex); |
|
152 if (node->moType == FWMgrMOPlayer) |
|
153 printf("MediaPlayer-"); |
|
154 else if (node->moType == FWMgrMORecorder) |
|
155 printf("MediaRecrdr-"); |
|
156 else |
|
157 printf("UKNOWN-"); |
|
158 if (node->fwType == FWMgrFWMMF) |
|
159 printf("MMF-"); |
|
160 else if (node->fwType == FWMgrFWGST) |
|
161 printf("GST-"); |
|
162 else |
|
163 printf("UKNOWN-"); |
|
164 printf("Scheme["); |
|
165 for (i = 0; i < node->uriSchemeCount; i++) |
|
166 printf(" %s", node->uriSchemes[i]); |
|
167 printf("]FileExt["); |
|
168 for (i = 0; i < node->fileExtCount; i++) |
|
169 printf(" %s", node->fileExts[i]); |
|
170 printf("]\n"); |
|
171 node = node->next; |
|
172 } |
|
173 } |
|
174 #endif |
|
175 |
|
176 /* void XAFrameworkMgr_DeleteFrameworkMap |
|
177 * Description: Deletes the list of framework and use-case map. |
|
178 */ |
|
179 void XAFrameworkMgr_DeleteFrameworkMap(FrameworkMap **map) |
|
180 { |
|
181 FrameworkMap *node = *map; |
|
182 FrameworkMap *nextNode = NULL; |
|
183 int i; |
|
184 while (node) |
|
185 { |
|
186 for (i = 0; i < node->uriSchemeCount; i++) |
|
187 free(node->uriSchemes[i]); |
|
188 free(node->uriSchemes); |
|
189 |
|
190 for (i = 0; i < node->fileExtCount; i++) |
|
191 free(node->fileExts[i]); |
|
192 free(node->fileExts); |
|
193 |
|
194 nextNode = node->next; |
|
195 free(node); |
|
196 node = nextNode; |
|
197 } |
|
198 *map = NULL; |
|
199 } |
|
200 |
|
201 /* FWMgrFwType XAFrameworkMgr_GetFramework |
|
202 * Description: Returns the framework enum that handles uri. |
|
203 */ |
|
204 FWMgrFwType XAFrameworkMgr_GetFramework(FrameworkMap *map, const char *uri, |
|
205 FWMgrMOType mediaObject) |
|
206 { |
|
207 FWMgrFwType retVal = FWMgrFWUknown; |
|
208 char fileScheme[] = "file"; |
|
209 char *uriScheme = NULL; |
|
210 char *fileExt = NULL; |
|
211 FrameworkMap *node = map; |
|
212 FWMgrBool uriMatchFound = FWMgrFalse; |
|
213 FWMgrBool fileExtMatchFound = FWMgrFalse; |
|
214 int i = 0; |
|
215 int copyLen = 0; |
|
216 |
|
217 if (!map || !uri) |
|
218 { |
|
219 /* TODO Log invalid uri */ |
|
220 return retVal; |
|
221 } |
|
222 |
|
223 /* Get uri scheme */ |
|
224 uriScheme = strchr(uri, ':'); |
|
225 if (uriScheme == NULL) |
|
226 { |
|
227 /* TODO Log invalid uri */ |
|
228 return retVal; |
|
229 } |
|
230 |
|
231 copyLen = (uriScheme - uri); |
|
232 uriScheme = (char*) calloc(copyLen + 1, sizeof(char)); |
|
233 strncpy(uriScheme, uri, copyLen); |
|
234 uriScheme[copyLen] = '\0'; /*Null terminate it*/ |
|
235 |
|
236 if (strcasecmp(uriScheme, fileScheme) == 0) |
|
237 { |
|
238 /* Get uri extension */ |
|
239 char* dotLoc = strrchr(uri, '.'); |
|
240 if (dotLoc == NULL) |
|
241 { |
|
242 /* TODO Log invalid uri */ |
|
243 free(uriScheme); |
|
244 return retVal; |
|
245 } |
|
246 /* We need to add 1 to exclude '.'*/ |
|
247 copyLen = strlen(uri) - (dotLoc + 1 - uri); |
|
248 fileExt = (char*) calloc(copyLen + 1, sizeof(char)); |
|
249 strncpy(fileExt, dotLoc + 1, copyLen); |
|
250 fileExt[copyLen] = '\0'; /*Null terminate it*/ |
|
251 } |
|
252 |
|
253 while (node) |
|
254 { |
|
255 if (mediaObject == node->moType) |
|
256 { |
|
257 uriMatchFound = FWMgrFalse; |
|
258 fileExtMatchFound = FWMgrFalse; |
|
259 /* Match for uri*/ |
|
260 for (i = 0; i < node->uriSchemeCount; i++) |
|
261 { |
|
262 if (strcasecmp(uriScheme, node->uriSchemes[i]) == 0) |
|
263 { |
|
264 uriMatchFound = FWMgrTrue; |
|
265 break; |
|
266 } |
|
267 } |
|
268 /* if uri scheme is not file, we only need to check for uri */ |
|
269 if (!fileExt) |
|
270 { |
|
271 fileExtMatchFound = FWMgrTrue; |
|
272 } |
|
273 else |
|
274 { |
|
275 for (i = 0; i < node->fileExtCount; i++) |
|
276 { |
|
277 if (strcasecmp(fileExt, node->fileExts[i]) == 0) |
|
278 { |
|
279 fileExtMatchFound = FWMgrTrue; |
|
280 break; |
|
281 } |
|
282 } |
|
283 } |
|
284 |
|
285 if ((uriMatchFound == FWMgrTrue) && (fileExtMatchFound |
|
286 == FWMgrTrue)) |
|
287 { |
|
288 retVal = node->fwType; |
|
289 break; |
|
290 } |
|
291 } |
|
292 node = node->next; |
|
293 } |
|
294 free(uriScheme); |
|
295 free(fileExt); |
|
296 return retVal; |
|
297 } |
|
298 |
|
299 /* Local functions */ |
|
300 |
|
301 /* FWMgrBool processConfigEntry |
|
302 * Description: Processes a single line entry from the config file. |
|
303 */ |
|
304 FWMgrBool processConfigEntry(const char* buffer, FWMgrMOType *mediaType, |
|
305 FWMgrFwType *frameworkType, FWMgrTagType *tagType, |
|
306 FWMgrBool *newNode, FrameworkMap **node) |
|
307 { |
|
308 FWMgrBool processedSuccessfully = FWMgrTrue; |
|
309 |
|
310 if (!buffer || !mediaType || !frameworkType || !tagType || !newNode) |
|
311 { |
|
312 processedSuccessfully = FWMgrFalse; |
|
313 return processedSuccessfully; |
|
314 } |
|
315 |
|
316 *newNode = FWMgrFalse; |
|
317 switch (*mediaType) |
|
318 { |
|
319 case FWMgrMOUnknown: |
|
320 { |
|
321 if (strcmp(buffer, mediaPlayerBeginTag) == 0) |
|
322 { |
|
323 *mediaType = FWMgrMOPlayer; |
|
324 *frameworkType = FWMgrFWUknown; |
|
325 *tagType = FWMgrTagNone; |
|
326 *node = NULL; |
|
327 } |
|
328 else if (strcmp(buffer, mediaRecorderBeginTag) == 0) |
|
329 { |
|
330 *mediaType = FWMgrMORecorder; |
|
331 *frameworkType = FWMgrFWUknown; |
|
332 *tagType = FWMgrTagNone; |
|
333 *node = NULL; |
|
334 } |
|
335 } |
|
336 break; |
|
337 case FWMgrMOPlayer: |
|
338 case FWMgrMORecorder: |
|
339 { |
|
340 switch (*frameworkType) |
|
341 { |
|
342 case FWMgrFWUknown: |
|
343 { |
|
344 if ((*mediaType == FWMgrMOPlayer) && (strcmp(buffer, |
|
345 mediaPlayerEndTag) == 0)) |
|
346 *mediaType = FWMgrMOUnknown; |
|
347 else if ((*mediaType == FWMgrMORecorder) && (strcmp( |
|
348 buffer, mediaRecorderEndTag) == 0)) |
|
349 *mediaType = FWMgrMOUnknown; |
|
350 else if ((strcmp(buffer, mediaFrameworkMmfBeginTag) == 0) |
|
351 || (strcmp(buffer, mediaFrameworkGstBeginTag) |
|
352 == 0)) |
|
353 { |
|
354 |
|
355 *frameworkType = FWMgrFWMMF; |
|
356 if (strcmp(buffer, mediaFrameworkGstBeginTag) == 0) |
|
357 *frameworkType = FWMgrFWGST; |
|
358 if (*node) |
|
359 { |
|
360 printf( |
|
361 "Fatal error error. Entry already exists and creating another one!!!"); |
|
362 processedSuccessfully = FWMgrFalse; |
|
363 } |
|
364 else |
|
365 { |
|
366 *node = (FrameworkMap*) calloc(1, |
|
367 sizeof(FrameworkMap)); |
|
368 if (!(*node)) |
|
369 { |
|
370 printf( |
|
371 "Fatal error. No memory to create an Entry!!!"); |
|
372 processedSuccessfully = FWMgrFalse; |
|
373 } |
|
374 else |
|
375 { |
|
376 *newNode = FWMgrTrue; |
|
377 (*node)->moType = *mediaType; |
|
378 (*node)->fwType = *frameworkType; |
|
379 } |
|
380 } |
|
381 } |
|
382 } |
|
383 break; |
|
384 case FWMgrFWMMF: |
|
385 { |
|
386 processedSuccessfully = processTagType(buffer, |
|
387 frameworkType, tagType, node); |
|
388 } |
|
389 break; |
|
390 case FWMgrFWGST: |
|
391 { |
|
392 processedSuccessfully = processTagType(buffer, |
|
393 frameworkType, tagType, node); |
|
394 } |
|
395 break; |
|
396 default: |
|
397 processedSuccessfully = FWMgrFalse; |
|
398 break; |
|
399 }; |
|
400 } |
|
401 break; |
|
402 default: |
|
403 processedSuccessfully = FWMgrFalse; |
|
404 break; |
|
405 }; |
|
406 return processedSuccessfully; |
|
407 } |
|
408 |
|
409 /* FWMgrBool processTagType |
|
410 * Description: Processes a framework type, uri, file tags entry from |
|
411 * the config file. |
|
412 */ |
|
413 FWMgrBool processTagType(const char* buffer, FWMgrFwType *frameworkType, |
|
414 FWMgrTagType *tagType, FrameworkMap **node) |
|
415 { |
|
416 FWMgrBool processedSuccessfully = FWMgrTrue; |
|
417 |
|
418 if (!buffer || !frameworkType || !tagType || (*node == NULL)) |
|
419 { |
|
420 processedSuccessfully = FWMgrFalse; |
|
421 return processedSuccessfully; |
|
422 } |
|
423 |
|
424 switch (*tagType) |
|
425 { |
|
426 case FWMgrTagNone: |
|
427 { |
|
428 if (((*frameworkType == FWMgrFWMMF) && (strcmp(buffer, |
|
429 mediaFrameworkMmfEndTag) == 0)) || ((*frameworkType |
|
430 == FWMgrFWGST) |
|
431 && (strcmp(buffer, mediaFrameworkGstEndTag) == 0))) |
|
432 { |
|
433 *node = NULL; |
|
434 *frameworkType = FWMgrFWUknown; |
|
435 } |
|
436 else if (strcmp(buffer, uriSchemeBeginTag) == 0) |
|
437 *tagType = FWMgrTagURIScheme; |
|
438 else if (strcmp(buffer, fileExtBeginTag) == 0) |
|
439 *tagType = FWMgrTagFileExt; |
|
440 } |
|
441 break; |
|
442 case FWMgrTagURIScheme: |
|
443 { |
|
444 if (strcmp(buffer, uriSchemeEndTag) == 0) |
|
445 *tagType = FWMgrTagNone; |
|
446 else |
|
447 { |
|
448 processedSuccessfully = FWMgrFalse; |
|
449 if (*node) |
|
450 processedSuccessfully = tokenizeTag(FWMgrTagURIScheme, |
|
451 buffer, node); |
|
452 } |
|
453 } |
|
454 break; |
|
455 case FWMgrTagFileExt: |
|
456 { |
|
457 if (strcmp(buffer, fileExtEndTag) == 0) |
|
458 *tagType = FWMgrTagNone; |
|
459 else |
|
460 { |
|
461 processedSuccessfully = FWMgrFalse; |
|
462 if (*node) |
|
463 processedSuccessfully = tokenizeTag(FWMgrTagFileExt, |
|
464 buffer, node); |
|
465 } |
|
466 } |
|
467 break; |
|
468 default: |
|
469 processedSuccessfully = FWMgrFalse; |
|
470 break; |
|
471 }; |
|
472 return processedSuccessfully; |
|
473 } |
|
474 |
|
475 /* FWMgrBool processTagType |
|
476 * Description: Processes a framework type, uri, file tags entry from |
|
477 * the config file. |
|
478 */ |
|
479 FWMgrBool tokenizeTag(FWMgrTagType tagType, const char* buffer, |
|
480 FrameworkMap **node) |
|
481 { |
|
482 char* tempStartPtr; |
|
483 char* tempEndPtr; |
|
484 int index = 0; |
|
485 int strLen = 0; |
|
486 |
|
487 if (!buffer || (*node == NULL)) |
|
488 { |
|
489 return FWMgrFalse; |
|
490 } |
|
491 |
|
492 tempStartPtr = /*const_cast<char*>*/(char*) (buffer); |
|
493 tempEndPtr = /*const_cast<char*>*/(char*) (buffer); |
|
494 |
|
495 if (tagType == FWMgrTagURIScheme) |
|
496 { |
|
497 (*node)->uriSchemeCount = atoi(buffer); |
|
498 (*node)->uriSchemes = (char**) calloc((*node)->uriSchemeCount, |
|
499 sizeof(*((*node)->uriSchemes))); |
|
500 if (!(*node)->uriSchemes) |
|
501 { |
|
502 printf("Fatal error. No memory to create an Entry!!!"); |
|
503 return FWMgrFalse; |
|
504 } |
|
505 } |
|
506 else if (tagType == FWMgrTagFileExt) |
|
507 { |
|
508 (*node)->fileExtCount = atoi(buffer); |
|
509 (*node)->fileExts = (char**) calloc((*node)->fileExtCount, |
|
510 sizeof(*((*node)->fileExts))); |
|
511 if (!(*node)->fileExts) |
|
512 { |
|
513 printf("Fatal error. No memory to create an Entry!!!"); |
|
514 return FWMgrFalse; |
|
515 } |
|
516 } |
|
517 else |
|
518 return FWMgrFalse; |
|
519 |
|
520 /*Find the index of :*/ |
|
521 tempStartPtr = strchr(tempStartPtr, ','); |
|
522 index = 0; |
|
523 while (tempStartPtr && (strlen(tempStartPtr) > 1)) |
|
524 { |
|
525 tempStartPtr++; /* Ignore separator ','*/ |
|
526 tempEndPtr = strchr(tempStartPtr, ','); |
|
527 strLen = (tempEndPtr - tempStartPtr) + 1; /* To hold null terminator */ |
|
528 if (strLen > 0) |
|
529 { |
|
530 if (tagType == FWMgrTagURIScheme) |
|
531 { |
|
532 (*node)->uriSchemes[index] = (char*) calloc(strLen, |
|
533 sizeof(char)); |
|
534 strncpy((*node)->uriSchemes[index], tempStartPtr, strLen); |
|
535 (*node)->uriSchemes[index][strLen - 1] = '\0'; /*Null terminate it*/ |
|
536 index++; |
|
537 } |
|
538 else if (tagType == FWMgrTagFileExt) |
|
539 { |
|
540 (*node)->fileExts[index] = (char*) calloc(strLen, |
|
541 sizeof(char)); |
|
542 strncpy((*node)->fileExts[index], tempStartPtr, strLen); |
|
543 (*node)->fileExts[index][strLen - 1] = '\0'; /*Null terminate it*/ |
|
544 index++; |
|
545 } |
|
546 } |
|
547 tempStartPtr = tempEndPtr; |
|
548 } |
|
549 return FWMgrTrue; |
|
550 } |
|
551 |