Skip to content

Commit 8a3bb35

Browse files
committed
init
0 parents  commit 8a3bb35

File tree

12 files changed

+421
-0
lines changed

12 files changed

+421
-0
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*.c]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.DS_STORE
2+
*.o
3+
*.log
4+
*.gcov
5+
*.gcda
6+
*.gcno
7+
binary
8+
test
9+
example
10+
coverage

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: c
2+
compiler:
3+
# - clang
4+
- gcc
5+
script: make run-test
6+
after_script: sudo pip install cpp-coveralls && make run-coverage && coveralls --exclude test.c
7+
8+
notifications:
9+
email: false

Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
PREFIX ?= /usr/local
2+
3+
SRC = cli.c deps/binary.c/binary.c
4+
5+
OBJ_SRC = $(SRC:.c=.o)
6+
7+
CFLAGS = -D_GNU_SOURCE -std=c99 -I deps/binary.c/
8+
9+
LFLAGS = -Wall -Wno-format-y2k -W -Wstrict-prototypes \
10+
-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \
11+
-Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \
12+
-Wnested-externs -Wredundant-decls
13+
14+
binary: $(OBJ_SRC)
15+
$(CC) $(OBJ_SRC) -o $@
16+
17+
.SUFFIXES: .c .o
18+
.c.o:
19+
$(CC) $< $(CFLAGS) $(LFLAGS) -c -o $@
20+
21+
install: binary
22+
cp -f binary $(PREFIX)/bin/binary
23+
24+
uninstall:
25+
rm -f $(PREFIX)/bin/binary
26+
27+
run-test: binary
28+
bash ./test.sh
29+
30+
clean:
31+
rm -f binary *.o deps/**/*.o *.gc{ov,da,no}
32+
33+
.PHONY: clean run-test install uninstall

cli.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// See for more details:
3+
// https://github.com/abranhe/binary.c
4+
//
5+
// cli.c
6+
//
7+
// MIT licensed.
8+
// Copyright (c) Abraham Hernandez <[email protected]>
9+
//
10+
11+
#include <stdio.h>
12+
#include <string.h>
13+
#include <stdlib.h>
14+
#include "binary.h"
15+
16+
const char
17+
*show_help() {
18+
return "\n\
19+
An small library to work with binary numbers\n\n\
20+
Usage:\n\n\
21+
$ binary <option> <input> \n\n\
22+
Options:\n\n\
23+
-is, --is-binary check if a number is binary\n\
24+
--to-decimal convert a binary number to decimal\n\
25+
--to-binary convert a decimal number to binary\n\
26+
-v, --version output version number\n\
27+
-h, --help output usage information\n\n\
28+
Example:\n\n\
29+
$ binary --is-binary 10101000\n\
30+
true\n\
31+
$ binary --to-decimal 1011\n\
32+
7\n\n";
33+
}
34+
35+
/* CLI. */
36+
int
37+
main(int argc, char **argv) {
38+
char *a = argv[1];
39+
char *b = argv[2];
40+
41+
if (argc == 2) {
42+
if (!strcmp(a, "-v") || !strcmp(a, "--version")) {
43+
printf("%s", "1.0.0\n");
44+
return 0;
45+
}
46+
47+
if (!strcmp(a, "-h") || !strcmp(a, "--help")) {
48+
printf("%s", show_help());
49+
return 0;
50+
}
51+
}
52+
53+
if (argc == 3) {
54+
if (!strcmp(a, "-is") || !strcmp(a, "--is-binary")) {
55+
char *pb;
56+
is_binary(strtoll(b, &pb, 0))
57+
? printf("binary\n")
58+
: printf("non-binary\n");
59+
return 0;
60+
}
61+
62+
if (!strcmp(a, "--to-decimal")) {
63+
char *pb;
64+
printf("%ld\n", to_decimal(strtoll(b, &pb, 0)));
65+
return 0;
66+
}
67+
68+
if (!strcmp(a, "--to-binary")) {
69+
char *pb;
70+
printf("%lld\n", to_binary(strtol(b, &pb, 0)));
71+
return 0;
72+
}
73+
}
74+
75+
if (argc != 3) {
76+
fprintf(stderr, "\033[31mBinary expects two arguments.\n\033[0m\n");
77+
fprintf(stderr, "\033[31mSee `--help` for more details.\033[0m\n");
78+
return 1;
79+
}
80+
return 0;
81+
}

deps/binary.c/binary.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// An small library to work with
3+
// binary numbers.
4+
//
5+
// binary.c
6+
//
7+
// MIT licensed.
8+
// Copyright (c) Abraham Hernandez <[email protected]>
9+
//
10+
11+
#include <stdbool.h>
12+
13+
bool
14+
is_binary (long long binary) {
15+
bool status = true;
16+
while(true) {
17+
if (binary == 0) break;
18+
else {
19+
int tmp = binary % 10;
20+
if(tmp > 1) {
21+
status = false;
22+
break;
23+
}
24+
binary = binary / 10;
25+
}
26+
}
27+
return status;
28+
}
29+
30+
long
31+
to_decimal (long long binary) {
32+
if(!is_binary(binary)) return -1;
33+
34+
int decimal = 0;
35+
int multiplier = 1;
36+
37+
while (binary != 0) {
38+
decimal += (binary % 10) * multiplier;
39+
binary /= 10;
40+
multiplier *= 2;
41+
}
42+
return decimal;
43+
}
44+
45+
long long to_binary(long number) {
46+
long long binary = 0;
47+
int remainder;
48+
int i = 1;
49+
50+
while (number != 0) {
51+
remainder = number % 2;
52+
number /= 2;
53+
binary += remainder * i;
54+
i *= 10;
55+
}
56+
return binary;
57+
}
58+
59+
// TODO:
60+
// add_binary()
61+
// substract_binary() ...

deps/binary.c/binary.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef BINARY_H
2+
#define BINARY_H
3+
4+
//
5+
// binary.h
6+
//
7+
// MIT licensed.
8+
// Copyright (c) Abraham Hernandez <[email protected]>
9+
//
10+
11+
#define bool int
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
bool
18+
is_binary(long binary);
19+
20+
long
21+
to_decimal(long long binary);
22+
23+
long long to_binary(long number);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
29+
#endif // BINARY_H

deps/binary.c/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "binary.c",
3+
"version": "1.0.0",
4+
"description": "An small library to work with binary numbers",
5+
"license": "MIT",
6+
"keywords": [
7+
"binary", "binary-numbers"
8+
],
9+
"repo": "abranhe/binary.c",
10+
"src": [
11+
"binary.h",
12+
"binary.c"
13+
]
14+
}

license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Abraham Hernandez <[email protected]> (abranhe.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "binary.c",
3+
"version": "1.0.0",
4+
"description": "An small library to work with binary numbers",
5+
"license": "MIT",
6+
"keywords": [
7+
"binary",
8+
"binary-numbers",
9+
"bin",
10+
"cli"
11+
],
12+
"repo": "abranhe/binary.c",
13+
"dependencies": {
14+
"abranhe/binary.c": "1.0.0"
15+
},
16+
"install": "make install"
17+
}

0 commit comments

Comments
 (0)