Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
abranhe committed Nov 9, 2018
0 parents commit 8a3bb35
Show file tree
Hide file tree
Showing 12 changed files with 421 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*.c]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.DS_STORE
*.o
*.log
*.gcov
*.gcda
*.gcno
binary
test
example
coverage
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: c
compiler:
# - clang
- gcc
script: make run-test
after_script: sudo pip install cpp-coveralls && make run-coverage && coveralls --exclude test.c

notifications:
email: false
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
PREFIX ?= /usr/local

SRC = cli.c deps/binary.c/binary.c

OBJ_SRC = $(SRC:.c=.o)

CFLAGS = -D_GNU_SOURCE -std=c99 -I deps/binary.c/

LFLAGS = -Wall -Wno-format-y2k -W -Wstrict-prototypes \
-Wpointer-arith -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch \
-Wshadow -Wcast-align -Wbad-function-cast -Wchar-subscripts -Winline \
-Wnested-externs -Wredundant-decls

binary: $(OBJ_SRC)
$(CC) $(OBJ_SRC) -o $@

.SUFFIXES: .c .o
.c.o:
$(CC) $< $(CFLAGS) $(LFLAGS) -c -o $@

install: binary
cp -f binary $(PREFIX)/bin/binary

uninstall:
rm -f $(PREFIX)/bin/binary

run-test: binary
bash ./test.sh

clean:
rm -f binary *.o deps/**/*.o *.gc{ov,da,no}

.PHONY: clean run-test install uninstall
81 changes: 81 additions & 0 deletions cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// See for more details:
// https://github.com/abranhe/binary.c
//
// cli.c
//
// MIT licensed.
// Copyright (c) Abraham Hernandez <abraham@abranhe.com>
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "binary.h"

const char
*show_help() {
return "\n\
An small library to work with binary numbers\n\n\
Usage:\n\n\
$ binary <option> <input> \n\n\
Options:\n\n\
-is, --is-binary check if a number is binary\n\
--to-decimal convert a binary number to decimal\n\
--to-binary convert a decimal number to binary\n\
-v, --version output version number\n\
-h, --help output usage information\n\n\
Example:\n\n\
$ binary --is-binary 10101000\n\
true\n\
$ binary --to-decimal 1011\n\
7\n\n";
}

/* CLI. */
int
main(int argc, char **argv) {
char *a = argv[1];
char *b = argv[2];

if (argc == 2) {
if (!strcmp(a, "-v") || !strcmp(a, "--version")) {
printf("%s", "1.0.0\n");
return 0;
}

if (!strcmp(a, "-h") || !strcmp(a, "--help")) {
printf("%s", show_help());
return 0;
}
}

if (argc == 3) {
if (!strcmp(a, "-is") || !strcmp(a, "--is-binary")) {
char *pb;
is_binary(strtoll(b, &pb, 0))
? printf("binary\n")
: printf("non-binary\n");
return 0;
}

if (!strcmp(a, "--to-decimal")) {
char *pb;
printf("%ld\n", to_decimal(strtoll(b, &pb, 0)));
return 0;
}

if (!strcmp(a, "--to-binary")) {
char *pb;
printf("%lld\n", to_binary(strtol(b, &pb, 0)));
return 0;
}
}

if (argc != 3) {
fprintf(stderr, "\033[31mBinary expects two arguments.\n\033[0m\n");
fprintf(stderr, "\033[31mSee `--help` for more details.\033[0m\n");
return 1;
}
return 0;
}
61 changes: 61 additions & 0 deletions deps/binary.c/binary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// An small library to work with
// binary numbers.
//
// binary.c
//
// MIT licensed.
// Copyright (c) Abraham Hernandez <abraham@abranhe.com>
//

#include <stdbool.h>

bool
is_binary (long long binary) {
bool status = true;
while(true) {
if (binary == 0) break;
else {
int tmp = binary % 10;
if(tmp > 1) {
status = false;
break;
}
binary = binary / 10;
}
}
return status;
}

long
to_decimal (long long binary) {
if(!is_binary(binary)) return -1;

int decimal = 0;
int multiplier = 1;

while (binary != 0) {
decimal += (binary % 10) * multiplier;
binary /= 10;
multiplier *= 2;
}
return decimal;
}

long long to_binary(long number) {
long long binary = 0;
int remainder;
int i = 1;

while (number != 0) {
remainder = number % 2;
number /= 2;
binary += remainder * i;
i *= 10;
}
return binary;
}

// TODO:
// add_binary()
// substract_binary() ...
29 changes: 29 additions & 0 deletions deps/binary.c/binary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef BINARY_H
#define BINARY_H

//
// binary.h
//
// MIT licensed.
// Copyright (c) Abraham Hernandez <abraham@abranhe.com>
//

#define bool int

#ifdef __cplusplus
extern "C" {
#endif

bool
is_binary(long binary);

long
to_decimal(long long binary);

long long to_binary(long number);

#ifdef __cplusplus
}
#endif

#endif // BINARY_H
14 changes: 14 additions & 0 deletions deps/binary.c/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "binary.c",
"version": "1.0.0",
"description": "An small library to work with binary numbers",
"license": "MIT",
"keywords": [
"binary", "binary-numbers"
],
"repo": "abranhe/binary.c",
"src": [
"binary.h",
"binary.c"
]
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Abraham Hernandez <abraham@abranhe.com> (abranhe.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "binary.c",
"version": "1.0.0",
"description": "An small library to work with binary numbers",
"license": "MIT",
"keywords": [
"binary",
"binary-numbers",
"bin",
"cli"
],
"repo": "abranhe/binary.c",
"dependencies": {
"abranhe/binary.c": "1.0.0"
},
"install": "make install"
}
80 changes: 80 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<p align="center">
<br>
<img src="https://cdn.abranhe.com/projects/binary/logo.svg">
<br>
<br>
<b>binary cli</b>: The command line interface to work with binary numbers
<br>
</p>

<p align="center">
<a href="https://travis-ci.org/abranhe/binary"><img src="https://img.shields.io/travis/abranhe/binary.svg?logo=travis" /></a>
<a href="https://github.com/abranhe"><img src="https://abranhe.com/badge.svg"></a>
<a href="https://cash.me/$abranhe"><img src="https://cdn.abranhe.com/badges/cash-me.svg"></a>
<a href="https://patreon.com/abranhe"><img src="https://cdn.abranhe.com/badges/patreon.svg" /></a>
<a href="https://github.com/abranhe/binary/blob/master/license"><img src="https://img.shields.io/github/license/abranhe/binary.svg" /></a>

<br>
<br>
</p>
In mathematics and digital electronics, a binary number is a number expressed in the **base-2 numeral system** or **binary numeral system**, which uses only two symbols: typically `0` and `1`.


## Installation

*Installing using [Clib](https://github.com/clibs/clib)*

```sh
$ clib install abranhe/binary
```

## CLI

*The options of binary*

```
An small library to work with binary numbers
Usage:
$ binary <option> <input>
Options:
-is, --is-binary check if a number is binary
--to-decimal convert a binary number to decimal
--to-binary convert a decimal number to binary
-v, --version output version number
-h, --help output usage information
Example:
$ binary --is-binary 10101000
true
$ binary --to-decimal 1011
7
```

## Related

- [binary.c][binary]: API for this module written in C.

## Team

|[![Carlos Abraham Logo][abranhe-img]][abranhe]|
| :-: |
| [Carlos Abraham][abranhe] |

## License

[MIT][license] License © [Carlos Abraham][abranhe]

<!-------------------- Links ------------------------>
[abranhe]: https://github.com/abranhe
[abranhe-img]: https://avatars3.githubusercontent.com/u/21347264?s=50
[license]: https://github.com/abranhe/binary/blob/master/license
[travis-badge]: https://img.shields.io/travis/abranhe/binary.svg
[travis-status]: https://travis-ci.org/abranhe/binary
[coverage-badge]: https://img.shields.io/coveralls/abranhe/binary.svg
[coverage-status]: https://coveralls.io/r/abranhe/binary?branch=master
[binary]: https://github.com/abranhe/binary.c
Loading

0 comments on commit 8a3bb35

Please sign in to comment.