From 12f0385a6bf5660295e090f226e6098f90dd4709 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?=
<41315874+fumiama@users.noreply.github.com>
Date: Thu, 20 Jun 2024 00:59:55 +0900
Subject: [PATCH] feat: add nixie tube theme
---
README.md | 11 +++--
assets/index.html | 16 ++++---
cmoe.c | 118 ++++++++++++++++++++++++++++------------------
cmoe.h | 33 +++++++++----
4 files changed, 112 insertions(+), 66 deletions(-)
diff --git a/README.md b/README.md
index 56c5232..bc3cc39 100644
--- a/README.md
+++ b/README.md
@@ -8,13 +8,18 @@
More theme
##### moebooru(mb)
-![moebooru](https://count.getloli.com/get/@demo?theme=moebooru)
+![moebooru](https://counter.seku.su/cmoe?name=demo&theme=mb)
##### rule34(r34)
-![Rule34](https://count.getloli.com/get/@demo?theme=rule34)
+![Rule34](https://counter.seku.su/cmoe?name=demo&theme=r34)
##### gelbooru(gb)
-![Gelbooru](https://count.getloli.com/get/@demo?theme=gelbooru)
+![Gelbooru](https://counter.seku.su/cmoe?name=demo&theme=gb)
+
+##### nixie(nix)
+![Nixie Tube](https://counter.seku.su/cmoe?name=demo&theme=nix)
+
+
## Usage
### 1. Install [simple-http-server](https://github.com/fumiama/simple-http-server)
diff --git a/assets/index.html b/assets/index.html
index fe08c0a..703b01d 100644
--- a/assets/index.html
+++ b/assets/index.html
@@ -29,18 +29,20 @@
Just use the query parameters theme
, like this: /cmoe?name=yourname&theme=mb
-
diff --git a/cmoe.c b/cmoe.c
index 00c30bd..35ac69f 100644
--- a/cmoe.c
+++ b/cmoe.c
@@ -80,21 +80,84 @@ static inline int add_user(char* name, uint32_t count, FILE* fp) {
return !set_pb(fp, items_len, sizeof(counter_t), &counter);
}
-static inline uint32_t get_content_len(int isbig, uint16_t* len_type, char* cntstr) {
+static inline uint32_t get_content_len(int isbigtiny, uint16_t* len_type, char* cntstr) {
+ static int lendiff[] = {
+ 0|(0<<8)|(-1u<<16), 1|(1<<8)|(0<<16), 1|(2<<8)|(1<<16), 2|(2<<8)|(1<<16), 2|(2<<8)|(1<<16),
+ 2|(2<<8)|(1<<16), 2|(2<<8)|(1<<16), 2|(2<<8)|(1<<16), 2|(2<<8)|(1<<16), 2|(2<<8)|(1<<16)
+ };
uint32_t len = sizeof(svg_small) // small & big has the same len
+ sizeof(svg_tail) - 1;
+ if (isbigtiny&2) len--; // is tiny
int i = 0;
for (; cntstr[i]; i++) {
len += len_type[cntstr[i] - '0'] + (sizeof(img_slot_front) + sizeof(img_slot_rear) - 2);
- if (i > 0) len++;
- if (i > 2-isbig) len++;
+ len += (uint32_t)((int)((int8_t)((lendiff[i]>>(isbigtiny*8))&0xff)));
}
return len;
}
-#define has_next(fp, ch) ((ch=getc(fp)),(feof(fp)?0:(ungetc(ch,fp),1)))
#define cmp_and_set_type(t) if (!strcmp(theme, #t)) { theme_type = (char**)t; len_type = (uint16_t*)t##l; }
+static void draw(int count, char* theme) {
+ char cntstrbuf[11];
+ sprintf(cntstrbuf, "%010u", count);
+ char* cntstr = cntstrbuf;
+ int i = 0;
+ for (; i < 10; i++) if (cntstrbuf[i] != '0') {
+ if (i > 2) cntstr = cntstrbuf+i-2;
+ break;
+ }
+ int isbig = 0;
+ char** theme_type = (char**)mb;
+ uint16_t* len_type = (uint16_t*)mbl;
+ if (theme) {
+ cmp_and_set_type(mbh) else
+ cmp_and_set_type(r34) else
+ cmp_and_set_type(gb) else
+ cmp_and_set_type(gbh) else
+ cmp_and_set_type(asl) else
+ cmp_and_set_type(nix)
+ isbig = (
+ (theme_type == (char**)gb || theme_type == (char**)gbh) |
+ ((theme_type == (char**)nix) << 1)
+ );
+ }
+ int w, h;
+ char *head;
+ if (isbig&1) {
+ w = W_BIG;
+ h = H_BIG;
+ head = (char*)svg_big;
+ }
+ else if (isbig&2) {
+ w = W_TINY;
+ h = H_TINY;
+ head = (char*)svg_tiny;
+ }
+ else {
+ w = W_SMALL;
+ h = H_SMALL;
+ head = (char*)svg_small;
+ }
+ if (headers(get_content_len(isbig, len_type, cntstr), "image/svg+xml")) {
+ write(1, "\0\0\0\0", 4);
+ return;
+ }
+ printf(head, w*(10+cntstrbuf-cntstr));
+ for (i = 0; cntstr[i]; i++) {
+ printf(img_slot_front, w * i, w, h);
+ int n = cntstr[i] - '0';
+ fwrite(theme_type[n], len_type[n], 1, stdout);
+ printf(img_slot_rear);
+ }
+ printf(svg_tail);
+}
+
+#define has_next(fp, ch) ((ch=getc(fp)),(feof(fp)?0:(ungetc(ch,fp),1)))
static void return_count(FILE* fp, char* name, char* theme) {
+ if (!strcmp(name, "demo")) {
+ draw(123456789, theme);
+ return;
+ }
int ch, exist = 0, user_exist = 0;
char buf[sizeof(simple_pb_t)+sizeof(counter_t)];
while (has_next(fp, ch)) {
@@ -109,55 +172,16 @@ static void return_count(FILE* fp, char* name, char* theme) {
http_error(HTTP500, "Add User Error.");
return;
}
- char cntstrbuf[11];
- sprintf(cntstrbuf, "%010u", d->count);
- char* cntstr = cntstrbuf;
- int i = 0;
- for (; i < 10; i++) if (cntstrbuf[i] != '0') {
- if (i > 2) cntstr = cntstrbuf+i-2;
- break;
- }
- int isbig = 0;
- char** theme_type = (char**)mb;
- uint16_t* len_type = (uint16_t*)mbl;
- if (theme) {
- cmp_and_set_type(mbh) else
- cmp_and_set_type(r34) else
- cmp_and_set_type(gb) else
- cmp_and_set_type(gbh) else
- cmp_and_set_type(asl)
- isbig = (theme_type == (char**)gb || theme_type == (char**)gbh);
- }
- int w, h;
- char *head;
- if (isbig) {
- w = W_BIG;
- h = H_BIG;
- head = (char*)svg_big;
- }
- else {
- w = W_SMALL;
- h = H_SMALL;
- head = (char*)svg_small;
- }
- if (headers(get_content_len(isbig, len_type, cntstr), "image/svg+xml")) {
- write(1, "\0\0\0\0", 4);
- return;
- }
- printf(head, w*(10+cntstrbuf-cntstr));
- for (i = 0; cntstr[i]; i++) {
- printf(img_slot_front, w * i, w, h);
- int n = cntstr[i] - '0';
- fwrite(theme_type[n], len_type[n], 1, stdout);
- printf(img_slot_rear);
- }
- printf(svg_tail);
+ draw(d->count, theme);
return;
}
http_error(HTTP404, "No Such User.");
}
static int name_exist(FILE* fp, char* name) {
+ if (!strcmp(name, "demo")) {
+ return 1;
+ }
int ch, exist = 0;
char buf[sizeof(simple_pb_t)+sizeof(counter_t)];
while (has_next(fp, ch)) {
diff --git a/cmoe.h b/cmoe.h
index 0409f4e..6325781 100644
--- a/cmoe.h
+++ b/cmoe.h
@@ -17,7 +17,7 @@ typedef struct counter_t counter_t;
#define H404 "HTTP/1.0 404 NOT FOUND\r\nContent-Type: text/html\r\n\r\nNot Found\r\n%s\r\n\r\n"
#define H500 "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n
%s\r\n"
const static char* types[] = {H400, H404, H500};
-const static uint8_t typel[] = {59, 111, 69};
+const static uint8_t typel[] = {sizeof(H400)-2, sizeof(H404)-2, sizeof(H500)-2};
enum errcode_enum_t {HTTP400, HTTP404, HTTP500};
typedef enum errcode_enum_t errcode_enum_t;
@@ -25,19 +25,21 @@ typedef enum errcode_enum_t errcode_enum_t;
#define CONTENT_LEN "Content-Length: %d\r\n"
#define CACHE_CTRL "Cache-Control: max-age=0, no-cache, no-store, must-revalidate\r\n"
+#define W_TINY 50
+#define H_TINY 50
#define W_SMALL 45
#define H_SMALL 100
#define W_BIG 68
#define H_BIG 150
-const static char svg_small[] =
- "