summaryrefslogtreecommitdiff
path: root/apps/plugins/lib/arg_helper.h
blob: 248e8e4eaf3392196084a409c17d1c937f302288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2021 William Wilgus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef _LIB_ARG_HELPER_H_
#define _LIB_ARG_HELPER_H_

#include "plugin.h"

#define ARGP_MAX_FRAC_DIGITS 9 /* Uses 30 bits max (0.999999999) */

#define ARGP_EXP(a, b) (a ##E## b)
#define ARGP_FRAC_DEC_MULTIPLIER(n) AP_EXP(1,n) /*1x10^n*/
#define ARGPARSE_FRAC_DEC_MULTIPLIER (long) ARGP_FRAC_DEC_MULTIPLIER(ARGP_MAX_FRAC_DIGITS)

/* fills buf with a string upto buf_sz, null terminates the buffer
 * strings break on WS by default but can be enclosed in single or double quotes
 * opening and closing quotes will not be included in the buffer but will be counted
 * use alternating quotes if you really want them included '"text"' or "'text'"
 * failure to close the string will result in eating all remaining args till \0
 * If buffer full remaining chars are discarded till stopchar or \0 is reached */
int string_parse(const char **parameter, char* buf, size_t buf_sz);
/* passes *character a single character eats remaining non-WS characters */
int char_parse(const char **parameter, char* character);
/* determine true false using the first character the rest are skipped/ignored */
int bool_parse(const char **parameter, bool *choice);
/* passes number and or decimal portion of number base 10 only.. */
int longnum_parse(const char **parameter, long *number, long *decimal);
int num_parse(const char **parameter, int *number, int *decimal);

/*
*argparse(const char *parameter, int parameter_len,
*         int (*arg_callback)(char argchar, const char **parameter))
* parameter     : constant char string of arguments
* parameter_len : may be set to -1 if your parameter string is NULL (\0) terminated
* arg_callback  : function gets called for each SWCHAR found in the parameter string
* Note: WS at beginning is stripped, **parameter starts at the first NON WS char
* return 0 for arg_callback to quit parsing immediately
*/
void argparse(const char *parameter, int parameter_len,
              int (*arg_callback)(char argchar, const char **parameter));

#endif /* _LIB_ARG_HELPER_H_ */