Created
January 4, 2020 18:27
-
-
Save untodesu/23603ce84bbd18c72056a961ac361519 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //============================================================================== | |
| // Copyright (c) 2020, undbsd | |
| // All rights reserved. | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // 1. Redistributions of source code must retain the above copyright notice, this | |
| // list of conditions and the following disclaimer. | |
| // | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| //============================================================================== | |
| #include <libcommon/string.h> | |
| #include <libcommon/clopt.h> | |
| #define CL_DEFAULTPARAM "" | |
| // checks whether string corresponds to the argument pattern (regex: "^-.+") | |
| bool CL_isarg(const char *arg) | |
| { | |
| //The correct argument have at least two chars, first of them is '-' | |
| return ((C_strlen(arg) >= 2) && (*arg == '-')); | |
| } | |
| // returns index of specified string argument (CL_isarg checks) in the argv. | |
| // [ returns 0 if not found, suitable for checking ] | |
| int CL_argindex(int argc, char **argv, const char *arg) | |
| { | |
| if(argv && CL_isarg(arg)) { | |
| for(int i = 1; i < argc; i++) { | |
| if(!C_strcmp(argv[i], arg)) { | |
| return i; | |
| } | |
| } | |
| } | |
| return 0; | |
| } | |
| // returns the non-argument string following the argument string in the argv. | |
| // [ returns null if not found the argument or haven't parameter ] | |
| const char * CL_getparam(int argc, char **argv, const char *arg) | |
| { | |
| if(argv) { | |
| int id = CL_argindex(argc, argv, arg); | |
| if(id) { | |
| // The parameter cannot have argument format. | |
| // So the right pair will be '-arg param', | |
| // but not the '-arg -param' anyway, | |
| // even with double quotes it shouldn't be argument. | |
| if((++id < argc) && !CL_isarg(argv[id])) { | |
| return argv[id]; | |
| } | |
| } | |
| } | |
| return CL_DEFAULTPARAM; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment