rpicamsrc: fix indentation
[platform/upstream/gstreamer.git] / sys / rpicamsrc / RaspiCLI.c
1 /* *INDENT-OFF* */
2 /*
3 Copyright (c) 2013, Broadcom Europe Ltd
4 Copyright (c) 2013, James Hughes
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9     * Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright
12       notice, this list of conditions and the following disclaimer in the
13       documentation and/or other materials provided with the distribution.
14     * Neither the name of the copyright holder nor the
15       names of its contributors may be used to endorse or promote products
16       derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
22 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /**
31  * \file RaspiCLI.c
32  * Code for handling command line parameters
33  *
34  * \date 4th March 2013
35  * \Author: James Hughes
36  *
37  * Description
38  *
39  * Some functions/structures for command line parameter parsing
40  *
41  */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <memory.h>
46
47 #include "interface/vcos/vcos.h"
48
49 #include "RaspiCLI.h"
50
51
52 /**
53  * Convert a string from command line to a comand_id from the list
54  *
55  * @param commands Array of command to check
56  * @param num_command Number of commands in the array
57  * @param arg String to search for in the list
58  * @param num_parameters Returns the number of parameters used by the command
59  *
60  * @return command ID if found, -1 if not found
61  *
62  */
63 int raspicli_get_command_id(const COMMAND_LIST *commands, const int num_commands, const char *arg, int *num_parameters)
64 {
65    int command_id = -1;
66    int j;
67
68    vcos_assert(commands);
69    vcos_assert(num_parameters);
70    vcos_assert(arg);
71
72    if (!commands || !num_parameters || !arg)
73       return -1;
74
75    for (j = 0; j < num_commands; j++)
76    {
77       if (!strcmp(arg, commands[j].command) ||
78           !strcmp(arg, commands[j].abbrev))
79       {
80          // match
81          command_id = commands[j].id;
82          *num_parameters = commands[j].num_parameters;
83          break;
84       }
85    }
86
87    return command_id;
88 }
89
90
91 /**
92  * Display the list of commands in help format
93  *
94  * @param commands Array of command to check
95  * @param num_command Number of commands in the arry
96  *
97  *
98  */
99 void raspicli_display_help(const COMMAND_LIST *commands, const int num_commands)
100 {
101    int i;
102
103    vcos_assert(commands);
104
105    if (!commands)
106       return;
107
108    for (i = 0; i < num_commands; i++)
109    {
110       fprintf(stderr, "-%s, -%s\t: %s\n", commands[i].abbrev,
111          commands[i].command, commands[i].help);
112    }
113 }
114
115
116 /**
117  * Function to take a string, a mapping, and return the int equivalent
118  * @param str Incoming string to match
119  * @param map Mapping data
120  * @param num_refs The number of items in the mapping data
121  * @return The integer match for the string, or -1 if no match
122  */
123 int raspicli_map_xref(const char *str, const XREF_T *map, int num_refs)
124 {
125    int i;
126
127    for (i=0;i<num_refs;i++)
128    {
129       if (!strcasecmp(str, map[i].mode))
130       {
131          return map[i].mmal_mode;
132       }
133    }
134    return -1;
135 }
136
137 /**
138  * Function to take a mmal enum (as int) and return the string equivalent
139  * @param en Incoming int to match
140  * @param map Mapping data
141  * @param num_refs The number of items in the mapping data
142  * @return const pointer to string, or NULL if no match
143  */
144 const char *raspicli_unmap_xref(const int en, const XREF_T *map, int num_refs)
145 {
146    int i;
147
148    for (i=0;i<num_refs;i++)
149    {
150       if (en == map[i].mmal_mode)
151       {
152          return map[i].mode;
153       }
154    }
155    return NULL;
156 }
157 /* *INDENT-ON* */