C++ -> C
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -1,6 +1,7 @@
|
|||||||
|
ncac
|
||||||
|
|
||||||
|
*.o
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
|
||||||
bazel-*
|
|
||||||
|
|
||||||
tags
|
tags
|
||||||
|
|||||||
12
BUILD
12
BUILD
@@ -1,12 +0,0 @@
|
|||||||
cc_binary(
|
|
||||||
name = "ncac",
|
|
||||||
srcs = [
|
|
||||||
"ncac.cc",
|
|
||||||
"ncac.h",
|
|
||||||
],
|
|
||||||
deps = [
|
|
||||||
"//asana",
|
|
||||||
"//ui",
|
|
||||||
"@system_include//:curses"
|
|
||||||
],
|
|
||||||
)
|
|
||||||
4
Makefile
Normal file
4
Makefile
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
CFLAGS=--std=c18 -O2 -Wall -Wextra -pedantic
|
||||||
|
LDLIBS=-lncurses -lcurl
|
||||||
|
|
||||||
|
ncac: ui/base.o asana/fetch.o ncac.o
|
||||||
25
WORKSPACE
25
WORKSPACE
@@ -1,25 +0,0 @@
|
|||||||
new_local_repository(
|
|
||||||
name = "system_include",
|
|
||||||
path = "/usr/lib",
|
|
||||||
build_file_content = """
|
|
||||||
cc_library(
|
|
||||||
name = "curses",
|
|
||||||
srcs = ["libcurses.dylib"],
|
|
||||||
visibility = ["//visibility:public"],
|
|
||||||
)
|
|
||||||
|
|
||||||
cc_library(
|
|
||||||
name = "curl",
|
|
||||||
srcs = ["libcurl.dylib"],
|
|
||||||
visibility = ["//visibility:public"],
|
|
||||||
)
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
new_http_archive(
|
|
||||||
name = "gtest",
|
|
||||||
url = "https://github.com/google/googletest/archive/release-1.8.0.tar.gz",
|
|
||||||
sha256 = "58a6f4277ca2bc8565222b3bbd58a177609e9c488e8a72649359ba51450db7d8",
|
|
||||||
build_file = "gtest.BUILD",
|
|
||||||
strip_prefix = "googletest-release-1.8.0",
|
|
||||||
)
|
|
||||||
0
asana/asana.c
Normal file
0
asana/asana.c
Normal file
4
asana/asana.h
Normal file
4
asana/asana.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#ifndef ASANA_ASANA_H_
|
||||||
|
#define ASANA_ASANA_H_
|
||||||
|
|
||||||
|
#endif // ASANA_ASANA_H_
|
||||||
19
asana/fetch.c
Normal file
19
asana/fetch.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include "fetch.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
|
||||||
|
CURL *curl;
|
||||||
|
|
||||||
|
bool asana_init() {
|
||||||
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
curl = curl_easy_init();
|
||||||
|
if (!curl) return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void asana_cleanup() {
|
||||||
|
curl_global_cleanup();
|
||||||
|
}
|
||||||
|
|
||||||
16
asana/fetch.h
Normal file
16
asana/fetch.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef ASANA_FETCH_H_
|
||||||
|
#define ASANA_FETCH_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* initialize libcurl
|
||||||
|
*/
|
||||||
|
bool asana_init();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cleanup libcurl before program end
|
||||||
|
*/
|
||||||
|
void asana_cleanup();
|
||||||
|
|
||||||
|
#endif // ASANA_FETCH_H_
|
||||||
13
gtest.BUILD
13
gtest.BUILD
@@ -1,13 +0,0 @@
|
|||||||
cc_library(
|
|
||||||
name = "main",
|
|
||||||
srcs = glob(
|
|
||||||
["src/*.cc"],
|
|
||||||
exclude = ["src/gtest-all.cc"]
|
|
||||||
),
|
|
||||||
hdrs = glob([
|
|
||||||
"include/**/*.h",
|
|
||||||
"src/*.h"
|
|
||||||
]),
|
|
||||||
copts = ["-Iexternal/gtest/include"],
|
|
||||||
visibility = ["//visibility:public"],
|
|
||||||
)
|
|
||||||
@@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <term.h>
|
#include <term.h>
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "ui/base.h"
|
#include "ui/base.h"
|
||||||
#include "asana/fetch.h"
|
#include "asana/fetch.h"
|
||||||
|
|
||||||
@@ -21,10 +20,10 @@ int main(int argc, char **argv) {
|
|||||||
while(1) {
|
while(1) {
|
||||||
switch (input = getch()) {
|
switch (input = getch()) {
|
||||||
case 'a':
|
case 'a':
|
||||||
ui::draw_text("Hello, world!", &curs_x, &curs_y);
|
draw_text("Hello, world!", &curs_x, &curs_y);
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
ui::draw_text("Goodbye, world!", &curs_x, &curs_y);
|
draw_text("Goodbye, world!", &curs_x, &curs_y);
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
finish(SIGTERM);
|
finish(SIGTERM);
|
||||||
@@ -36,26 +35,26 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
static void finish(int sig) {
|
static void finish(int sig) {
|
||||||
endwin();
|
endwin();
|
||||||
asana::deinit();
|
asana_cleanup();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setup() {
|
static void setup() {
|
||||||
// initialize the asana client
|
// initialize the asana client
|
||||||
if (!asana::init()) {
|
if (!asana_init()) {
|
||||||
std::cerr << "Unable to initialize the Asana client\n" << EOF;
|
fprintf(stderr, "Unable to initialize the Asana client\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize ncurses
|
// initialize ncurses
|
||||||
if (!initscr()) {
|
if (!initscr()) {
|
||||||
std::cerr << "Unable to initialize the curses screen.\n" << EOF;
|
fprintf(stderr, "Unable to initialize the curses screen.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't buffer or echo input
|
// don't buffer or echo input
|
||||||
if (cbreak() == ERR) {
|
if (cbreak() == ERR) {
|
||||||
std::cerr << "Unable to initialize.\n" << EOF;
|
fprintf(stderr, "Unable to initialize.\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
noecho();
|
noecho();
|
||||||
7
ui/BUILD
7
ui/BUILD
@@ -1,7 +0,0 @@
|
|||||||
cc_library(
|
|
||||||
name="ui",
|
|
||||||
srcs = ["base.cc"],
|
|
||||||
hdrs = ["base.h"],
|
|
||||||
deps = ["@system_include//:curses"],
|
|
||||||
visibility = ["//visibility:public"],
|
|
||||||
)
|
|
||||||
18
ui/base.c
Normal file
18
ui/base.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include "base.h"
|
||||||
|
|
||||||
|
#include <curses.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
void draw_text(char *text, int *x, int *y) {
|
||||||
|
if (!text || strnlen(text, 1) == 0) return;
|
||||||
|
|
||||||
|
mvaddch(*y, *x, *text);
|
||||||
|
++text;
|
||||||
|
|
||||||
|
for (; *text != '\0'; ++text) {
|
||||||
|
addch(*text);
|
||||||
|
}
|
||||||
|
|
||||||
|
getyx(stdscr, *y, *x);
|
||||||
|
}
|
||||||
23
ui/base.cc
23
ui/base.cc
@@ -1,23 +0,0 @@
|
|||||||
#include "base.h"
|
|
||||||
|
|
||||||
#include <curses.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace ui {
|
|
||||||
void draw_text(const std::string text, int *x, int *y) {
|
|
||||||
if (!text.length()) return;
|
|
||||||
|
|
||||||
auto iter = text.begin();
|
|
||||||
|
|
||||||
mvaddch(*y, *x, *iter);
|
|
||||||
++iter;
|
|
||||||
|
|
||||||
for (; iter != text.end(); ++iter) {
|
|
||||||
addch(*iter);
|
|
||||||
}
|
|
||||||
|
|
||||||
getyx(stdscr, *y, *x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
14
ui/base.h
14
ui/base.h
@@ -1,14 +1,10 @@
|
|||||||
#ifndef UI_BASE_H_
|
#ifndef UI_BASE_H_
|
||||||
#define UI_BASE_H_
|
#define UI_BASE_H_
|
||||||
|
|
||||||
#include <string>
|
/**
|
||||||
|
* Prints the string text starting at position *x, *y. x and y are updated to
|
||||||
|
* the position after the last character
|
||||||
|
*/
|
||||||
|
void draw_text(char *text, int *x, int *y);
|
||||||
|
|
||||||
namespace ui {
|
|
||||||
/**
|
|
||||||
* Prints the string text starting at position *x, *y. x and y are updated to
|
|
||||||
* the position after the last character
|
|
||||||
*/
|
|
||||||
void draw_text(const std::string text, int *x, int *y);
|
|
||||||
|
|
||||||
} // namespace ui
|
|
||||||
#endif // UI_BASE_H_
|
#endif // UI_BASE_H_
|
||||||
|
|||||||
Reference in New Issue
Block a user