Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
025a9357
Unverified
Commit
025a9357
authored
Nov 06, 2019
by
Etienne Samson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
examples: move "args" to its own header
parent
745ccc8a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
251 additions
and
239 deletions
+251
-239
examples/args.c
+170
-0
examples/args.h
+79
-0
examples/common.c
+0
-168
examples/common.h
+2
-71
No files found.
examples/args.c
0 → 100644
View file @
025a9357
#include "common.h"
#include "args.h"
size_t
is_prefixed
(
const
char
*
str
,
const
char
*
pfx
)
{
size_t
len
=
strlen
(
pfx
);
return
strncmp
(
str
,
pfx
,
len
)
?
0
:
len
;
}
int
optional_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
,
const
char
*
def
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
size_t
len
=
is_prefixed
(
found
,
opt
);
if
(
!
len
)
return
0
;
if
(
!
found
[
len
])
{
if
(
args
->
pos
+
1
==
args
->
argc
)
{
*
out
=
def
;
return
1
;
}
args
->
pos
+=
1
;
*
out
=
args
->
argv
[
args
->
pos
];
return
1
;
}
if
(
found
[
len
]
==
'='
)
{
*
out
=
found
+
len
+
1
;
return
1
;
}
return
0
;
}
int
match_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
size_t
len
=
is_prefixed
(
found
,
opt
);
if
(
!
len
)
return
0
;
if
(
!
found
[
len
])
{
if
(
args
->
pos
+
1
==
args
->
argc
)
fatal
(
"expected value following argument"
,
opt
);
args
->
pos
+=
1
;
*
out
=
args
->
argv
[
args
->
pos
];
return
1
;
}
if
(
found
[
len
]
==
'='
)
{
*
out
=
found
+
len
+
1
;
return
1
;
}
return
0
;
}
static
const
char
*
match_numeric_arg
(
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
size_t
len
=
is_prefixed
(
found
,
opt
);
if
(
!
len
)
return
NULL
;
if
(
!
found
[
len
])
{
if
(
args
->
pos
+
1
==
args
->
argc
)
fatal
(
"expected numeric value following argument"
,
opt
);
args
->
pos
+=
1
;
found
=
args
->
argv
[
args
->
pos
];
}
else
{
found
=
found
+
len
;
if
(
*
found
==
'='
)
found
++
;
}
return
found
;
}
int
match_uint16_arg
(
uint16_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
match_numeric_arg
(
args
,
opt
);
uint16_t
val
;
char
*
endptr
=
NULL
;
if
(
!
found
)
return
0
;
val
=
(
uint16_t
)
strtoul
(
found
,
&
endptr
,
0
);
if
(
!
endptr
||
*
endptr
!=
'\0'
)
fatal
(
"expected number after argument"
,
opt
);
if
(
out
)
*
out
=
val
;
return
1
;
}
int
match_uint32_arg
(
uint32_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
match_numeric_arg
(
args
,
opt
);
uint16_t
val
;
char
*
endptr
=
NULL
;
if
(
!
found
)
return
0
;
val
=
(
uint32_t
)
strtoul
(
found
,
&
endptr
,
0
);
if
(
!
endptr
||
*
endptr
!=
'\0'
)
fatal
(
"expected number after argument"
,
opt
);
if
(
out
)
*
out
=
val
;
return
1
;
}
static
int
match_int_internal
(
int
*
out
,
const
char
*
str
,
int
allow_negative
,
const
char
*
opt
)
{
char
*
endptr
=
NULL
;
int
val
=
(
int
)
strtol
(
str
,
&
endptr
,
10
);
if
(
!
endptr
||
*
endptr
!=
'\0'
)
fatal
(
"expected number"
,
opt
);
else
if
(
val
<
0
&&
!
allow_negative
)
fatal
(
"negative values are not allowed"
,
opt
);
if
(
out
)
*
out
=
val
;
return
1
;
}
int
match_bool_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
if
(
!
strcmp
(
found
,
opt
))
{
*
out
=
1
;
return
1
;
}
if
(
!
strncmp
(
found
,
"--no-"
,
strlen
(
"--no-"
))
&&
!
strcmp
(
found
+
strlen
(
"--no-"
),
opt
+
2
))
{
*
out
=
0
;
return
1
;
}
*
out
=
-
1
;
return
0
;
}
int
is_integer
(
int
*
out
,
const
char
*
str
,
int
allow_negative
)
{
return
match_int_internal
(
out
,
str
,
allow_negative
,
NULL
);
}
int
match_int_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
,
int
allow_negative
)
{
const
char
*
found
=
match_numeric_arg
(
args
,
opt
);
if
(
!
found
)
return
0
;
return
match_int_internal
(
out
,
found
,
allow_negative
,
opt
);
}
examples/args.h
0 → 100644
View file @
025a9357
#ifndef INCLUDE_examples_args_h__
#define INCLUDE_examples_args_h__
/**
* Argument-processing helper structure
*/
struct
args_info
{
int
argc
;
char
**
argv
;
int
pos
;
};
#define ARGS_INFO_INIT { argc, argv, 0, 0 }
#define ARGS_CURRENT(args) args->argv[args->pos]
/**
* Check if a string has the given prefix. Returns 0 if not prefixed
* or the length of the prefix if it is.
*/
extern
size_t
is_prefixed
(
const
char
*
str
,
const
char
*
pfx
);
/**
* Match an integer string, returning 1 if matched, 0 if not.
*/
extern
int
is_integer
(
int
*
out
,
const
char
*
str
,
int
allow_negative
);
/**
* Check current `args` entry against `opt` string. If it matches
* exactly, take the next arg as a string; if it matches as a prefix with
* an equal sign, take the remainder as a string; if value not supplied,
* default value `def` will be given. otherwise return 0.
*/
extern
int
optional_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
,
const
char
*
def
);
/**
* Check current `args` entry against `opt` string. If it matches
* exactly, take the next arg as a string; if it matches as a prefix with
* an equal sign, take the remainder as a string; otherwise return 0.
*/
extern
int
match_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Check current `args` entry against `opt` string parsing as uint16. If
* `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
* is a prefix (equal sign optional), take the remainder of the arg as a
* uint16_t value; otherwise return 0.
*/
extern
int
match_uint16_arg
(
uint16_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Check current `args` entry against `opt` string parsing as uint32. If
* `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
* is a prefix (equal sign optional), take the remainder of the arg as a
* uint32_t value; otherwise return 0.
*/
extern
int
match_uint32_arg
(
uint32_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Check current `args` entry against `opt` string parsing as int. If
* `opt` matches exactly, take the next arg as an int value; if it matches
* as a prefix (equal sign optional), take the remainder of the arg as a
* int value; otherwise return 0.
*/
extern
int
match_int_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
,
int
allow_negative
);
/**
* Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
* If `opt` matches positively, out will be set to 1, or if `opt` matches
* negatively, out will be set to 0, and in both cases 1 will be returned.
* If neither the positive or the negative form of opt matched, out will be -1,
* and 0 will be returned.
*/
extern
int
match_bool_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
);
#endif
examples/common.c
View file @
025a9357
...
...
@@ -53,174 +53,6 @@ void fatal(const char *message, const char *extra)
exit
(
1
);
}
size_t
is_prefixed
(
const
char
*
str
,
const
char
*
pfx
)
{
size_t
len
=
strlen
(
pfx
);
return
strncmp
(
str
,
pfx
,
len
)
?
0
:
len
;
}
int
optional_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
,
const
char
*
def
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
size_t
len
=
is_prefixed
(
found
,
opt
);
if
(
!
len
)
return
0
;
if
(
!
found
[
len
])
{
if
(
args
->
pos
+
1
==
args
->
argc
)
{
*
out
=
def
;
return
1
;
}
args
->
pos
+=
1
;
*
out
=
args
->
argv
[
args
->
pos
];
return
1
;
}
if
(
found
[
len
]
==
'='
)
{
*
out
=
found
+
len
+
1
;
return
1
;
}
return
0
;
}
int
match_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
size_t
len
=
is_prefixed
(
found
,
opt
);
if
(
!
len
)
return
0
;
if
(
!
found
[
len
])
{
if
(
args
->
pos
+
1
==
args
->
argc
)
fatal
(
"expected value following argument"
,
opt
);
args
->
pos
+=
1
;
*
out
=
args
->
argv
[
args
->
pos
];
return
1
;
}
if
(
found
[
len
]
==
'='
)
{
*
out
=
found
+
len
+
1
;
return
1
;
}
return
0
;
}
static
const
char
*
match_numeric_arg
(
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
size_t
len
=
is_prefixed
(
found
,
opt
);
if
(
!
len
)
return
NULL
;
if
(
!
found
[
len
])
{
if
(
args
->
pos
+
1
==
args
->
argc
)
fatal
(
"expected numeric value following argument"
,
opt
);
args
->
pos
+=
1
;
found
=
args
->
argv
[
args
->
pos
];
}
else
{
found
=
found
+
len
;
if
(
*
found
==
'='
)
found
++
;
}
return
found
;
}
int
match_uint16_arg
(
uint16_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
match_numeric_arg
(
args
,
opt
);
uint16_t
val
;
char
*
endptr
=
NULL
;
if
(
!
found
)
return
0
;
val
=
(
uint16_t
)
strtoul
(
found
,
&
endptr
,
0
);
if
(
!
endptr
||
*
endptr
!=
'\0'
)
fatal
(
"expected number after argument"
,
opt
);
if
(
out
)
*
out
=
val
;
return
1
;
}
int
match_uint32_arg
(
uint32_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
match_numeric_arg
(
args
,
opt
);
uint16_t
val
;
char
*
endptr
=
NULL
;
if
(
!
found
)
return
0
;
val
=
(
uint32_t
)
strtoul
(
found
,
&
endptr
,
0
);
if
(
!
endptr
||
*
endptr
!=
'\0'
)
fatal
(
"expected number after argument"
,
opt
);
if
(
out
)
*
out
=
val
;
return
1
;
}
static
int
match_int_internal
(
int
*
out
,
const
char
*
str
,
int
allow_negative
,
const
char
*
opt
)
{
char
*
endptr
=
NULL
;
int
val
=
(
int
)
strtol
(
str
,
&
endptr
,
10
);
if
(
!
endptr
||
*
endptr
!=
'\0'
)
fatal
(
"expected number"
,
opt
);
else
if
(
val
<
0
&&
!
allow_negative
)
fatal
(
"negative values are not allowed"
,
opt
);
if
(
out
)
*
out
=
val
;
return
1
;
}
int
match_bool_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
)
{
const
char
*
found
=
args
->
argv
[
args
->
pos
];
if
(
!
strcmp
(
found
,
opt
))
{
*
out
=
1
;
return
1
;
}
if
(
!
strncmp
(
found
,
"--no-"
,
strlen
(
"--no-"
))
&&
!
strcmp
(
found
+
strlen
(
"--no-"
),
opt
+
2
))
{
*
out
=
0
;
return
1
;
}
*
out
=
-
1
;
return
0
;
}
int
is_integer
(
int
*
out
,
const
char
*
str
,
int
allow_negative
)
{
return
match_int_internal
(
out
,
str
,
allow_negative
,
NULL
);
}
int
match_int_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
,
int
allow_negative
)
{
const
char
*
found
=
match_numeric_arg
(
args
,
opt
);
if
(
!
found
)
return
0
;
return
match_int_internal
(
out
,
found
,
allow_negative
,
opt
);
}
int
diff_output
(
const
git_diff_delta
*
d
,
const
git_diff_hunk
*
h
,
...
...
examples/common.h
View file @
025a9357
...
...
@@ -52,6 +52,8 @@
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
#define UNUSED(x) (void)(x)
#include "args.h"
extern
int
lg2_add
(
git_repository
*
repo
,
int
argc
,
char
**
argv
);
extern
int
lg2_blame
(
git_repository
*
repo
,
int
argc
,
char
**
argv
);
extern
int
lg2_cat_file
(
git_repository
*
repo
,
int
argc
,
char
**
argv
);
...
...
@@ -97,77 +99,6 @@ extern char *read_file(const char *path);
extern
void
fatal
(
const
char
*
message
,
const
char
*
extra
);
/**
* Check if a string has the given prefix. Returns 0 if not prefixed
* or the length of the prefix if it is.
*/
extern
size_t
is_prefixed
(
const
char
*
str
,
const
char
*
pfx
);
/**
* Match an integer string, returning 1 if matched, 0 if not.
*/
extern
int
is_integer
(
int
*
out
,
const
char
*
str
,
int
allow_negative
);
struct
args_info
{
int
argc
;
char
**
argv
;
int
pos
;
};
#define ARGS_INFO_INIT { argc, argv, 0 }
/**
* Check current `args` entry against `opt` string. If it matches
* exactly, take the next arg as a string; if it matches as a prefix with
* an equal sign, take the remainder as a string; if value not supplied,
* default value `def` will be given. otherwise return 0.
*/
extern
int
optional_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
,
const
char
*
def
);
/**
* Check current `args` entry against `opt` string. If it matches
* exactly, take the next arg as a string; if it matches as a prefix with
* an equal sign, take the remainder as a string; otherwise return 0.
*/
extern
int
match_str_arg
(
const
char
**
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Check current `args` entry against `opt` string parsing as uint16. If
* `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
* is a prefix (equal sign optional), take the remainder of the arg as a
* uint16_t value; otherwise return 0.
*/
extern
int
match_uint16_arg
(
uint16_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Check current `args` entry against `opt` string parsing as uint32. If
* `opt` matches exactly, take the next arg as a uint16_t value; if `opt`
* is a prefix (equal sign optional), take the remainder of the arg as a
* uint32_t value; otherwise return 0.
*/
extern
int
match_uint32_arg
(
uint32_t
*
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Check current `args` entry against `opt` string parsing as int. If
* `opt` matches exactly, take the next arg as an int value; if it matches
* as a prefix (equal sign optional), take the remainder of the arg as a
* int value; otherwise return 0.
*/
extern
int
match_int_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
,
int
allow_negative
);
/**
* Check current `args` entry against a "bool" `opt` (ie. --[no-]progress).
* If `opt` matches positively, out will be set to 1, or if `opt` matches
* negatively, out will be set to 0, and in both cases 1 will be returned.
* If neither the positive or the negative form of opt matched, out will be -1,
* and 0 will be returned.
*/
extern
int
match_bool_arg
(
int
*
out
,
struct
args_info
*
args
,
const
char
*
opt
);
/**
* Basic output function for plain text diff output
* Pass `FILE*` such as `stdout` or `stderr` as payload (or NULL == `stdout`)
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment