mirror of
https://github.com/fumiama/CMoe-Counter.git
synced 2025-02-10 15:53:03 +01:00
优化代码结构
This commit is contained in:
parent
6a9398a0b9
commit
d29ecb28c5
68
cmoe.c
68
cmoe.c
@ -3,17 +3,18 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
|
#include <sys/uio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "cmoe.h"
|
#include "cmoe.h"
|
||||||
|
|
||||||
static uint32_t* items_len;
|
static uint32_t* items_len;
|
||||||
static COUNTER counter;
|
static COUNTER counter;
|
||||||
|
|
||||||
#define ADD_HERDER(h) {\
|
#define ADD_HEADER(h) {\
|
||||||
strcpy(buf + offset, (h));\
|
strcpy(buf + offset, (h));\
|
||||||
offset += sizeof((h)) - 1;\
|
offset += sizeof((h)) - 1;\
|
||||||
}
|
}
|
||||||
#define ADD_HERDER_PARAM(h, p) {\
|
#define ADD_HEADER_PARAM(h, p) {\
|
||||||
sprintf(buf + offset, (h), (p));\
|
sprintf(buf + offset, (h), (p));\
|
||||||
offset += strlen(buf + offset);\
|
offset += strlen(buf + offset);\
|
||||||
}
|
}
|
||||||
@ -22,20 +23,20 @@ static void headers(uint32_t content_len, const char* content_type) {
|
|||||||
char buf[1024];
|
char buf[1024];
|
||||||
uint32_t offset = 0;
|
uint32_t offset = 0;
|
||||||
|
|
||||||
ADD_HERDER(HTTP200 SERVER_STRING CACHE_CTRL);
|
ADD_HEADER(HTTP200 SERVER_STRING CACHE_CTRL);
|
||||||
ADD_HERDER_PARAM(CONTENT_TYPE, content_type);
|
ADD_HEADER_PARAM(CONTENT_TYPE, content_type);
|
||||||
ADD_HERDER_PARAM(CONTENT_LEN "\r\n", content_len);
|
ADD_HEADER_PARAM(CONTENT_LEN "\r\n", content_len);
|
||||||
content_len += offset;
|
content_len += offset;
|
||||||
write(1, (char*)&content_len, sizeof(uint32_t));
|
struct iovec iov[2] = {{&content_len, sizeof(uint32_t)}, {buf, offset}};
|
||||||
write(1, buf, offset);
|
writev(1, &iov, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void http_error(ERRCODE code, char* msg) {
|
static void http_error(ERRCODE code, char* msg) {
|
||||||
uint32_t len = strlen(msg) + typel[code];
|
uint32_t len = strlen(msg) + typel[code];
|
||||||
char* str = malloc(len);
|
char* str = malloc(len);
|
||||||
sprintf(str, types[code], msg);
|
sprintf(str, types[code], msg);
|
||||||
write(1, (char*)&len, sizeof(uint32_t));
|
struct iovec iov[2] = {{&len, sizeof(uint32_t)}, {str, len}};
|
||||||
write(1, str, len);
|
writev(1, &iov, 2);
|
||||||
free(str);
|
free(str);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -43,12 +44,11 @@ static void http_error(ERRCODE code, char* msg) {
|
|||||||
static char* get_arg(const char* query) {
|
static char* get_arg(const char* query) {
|
||||||
int len = 0;
|
int len = 0;
|
||||||
while(query[len] && query[len] != '&') len++;
|
while(query[len] && query[len] != '&') len++;
|
||||||
if(len > 0) {
|
if(len <= 0) return NULL;
|
||||||
char* name = malloc(len+1);
|
char* name = malloc(len+1);
|
||||||
memcpy(name, query, len);
|
memcpy(name, query, len);
|
||||||
name[len] = 0;
|
name[len] = 0;
|
||||||
return name;
|
return name;
|
||||||
} else return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int del_user(FILE* fp, SIMPLE_PB* spb) {
|
static int del_user(FILE* fp, SIMPLE_PB* spb) {
|
||||||
@ -57,25 +57,22 @@ static int del_user(FILE* fp, SIMPLE_PB* spb) {
|
|||||||
uint32_t this = next - spb->real_len;
|
uint32_t this = next - spb->real_len;
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
uint32_t end = ftell(fp);
|
uint32_t end = ftell(fp);
|
||||||
if(next == end) {
|
if(next == end) return ftruncate(fileno(fp), end - spb->real_len);
|
||||||
return ftruncate(fileno(fp), end - spb->real_len);
|
uint32_t cap = end - next;
|
||||||
} else {
|
char *data = malloc(cap);
|
||||||
uint32_t cap = end - next;
|
if(data) {
|
||||||
char *data = malloc(cap);
|
fseek(fp, next, SEEK_SET);
|
||||||
if(data) {
|
if(fread(data, cap, 1, fp) == 1) {
|
||||||
fseek(fp, next, SEEK_SET);
|
if(!ftruncate(fileno(fp), end - spb->real_len)){
|
||||||
if(fread(data, cap, 1, fp) == 1) {
|
fseek(fp, this, SEEK_SET);
|
||||||
if(!ftruncate(fileno(fp), end - spb->real_len)){
|
if(fwrite(data, cap, 1, fp) == 1) {
|
||||||
fseek(fp, this, SEEK_SET);
|
free(data);
|
||||||
if(fwrite(data, cap, 1, fp) == 1) {
|
fflush(fp);
|
||||||
free(data);
|
return 0;
|
||||||
fflush(fp);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(data);
|
|
||||||
}
|
}
|
||||||
|
free(data);
|
||||||
}
|
}
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
@ -89,7 +86,7 @@ static int add_user(char* name, uint32_t count, FILE* fp) {
|
|||||||
|
|
||||||
static uint32_t get_content_len(int isbig, uint16_t* len_type, char* cntstr) {
|
static uint32_t get_content_len(int isbig, uint16_t* len_type, char* cntstr) {
|
||||||
uint32_t len = sizeof(svg_small) - 1
|
uint32_t len = sizeof(svg_small) - 1
|
||||||
+ (sizeof(img_slot_front) + sizeof(img_slot_rear) - 2) * 10
|
+ (sizeof(img_slot_front) + sizeof(img_slot_rear) - 1) * 10
|
||||||
+ 16 + isbig
|
+ 16 + isbig
|
||||||
+ sizeof(svg_tail) - 1;
|
+ sizeof(svg_tail) - 1;
|
||||||
for(int i = 0; i < 10; i++) {
|
for(int i = 0; i < 10; i++) {
|
||||||
@ -120,6 +117,7 @@ static void return_count(char* name, char* theme) {
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
char cntstr[11];
|
char cntstr[11];
|
||||||
sprintf(cntstr, "%010u", d->count);
|
sprintf(cntstr, "%010u", d->count);
|
||||||
|
free(spb);
|
||||||
int isbig = 0;
|
int isbig = 0;
|
||||||
char** theme_type = mb;
|
char** theme_type = mb;
|
||||||
uint16_t* len_type = mbl;
|
uint16_t* len_type = mbl;
|
||||||
@ -148,19 +146,17 @@ static void return_count(char* name, char* theme) {
|
|||||||
printf(img_slot_front, w * i, w, h);
|
printf(img_slot_front, w * i, w, h);
|
||||||
int n = cntstr[i] - '0';
|
int n = cntstr[i] - '0';
|
||||||
fwrite(theme_type[n], len_type[n], 1, stdout);
|
fwrite(theme_type[n], len_type[n], 1, stdout);
|
||||||
printf(img_slot_rear);
|
puts(img_slot_rear);
|
||||||
}
|
}
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
write(1, svg_tail, sizeof(svg_tail)-1);
|
write(1, svg_tail, sizeof(svg_tail)-1);
|
||||||
}
|
}
|
||||||
free(spb);
|
|
||||||
user_exist = 1;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else free(spb);
|
} else free(spb);
|
||||||
}
|
}
|
||||||
if(!user_exist) http_error(HTTP404, "No Such User.");
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
http_error(HTTP404, "No Such User.");
|
||||||
} else http_error(HTTP500, "Open File Error.");
|
} else http_error(HTTP500, "Open File Error.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
cmoe.h
2
cmoe.h
@ -50,7 +50,7 @@ const char svg_big[] =
|
|||||||
" <g>\n"
|
" <g>\n"
|
||||||
" \n";
|
" \n";
|
||||||
const char img_slot_front[] = " <image x=\"%d\" y=\"0\" width=\"%d\" height=\"%d\" xlink:href=\"";
|
const char img_slot_front[] = " <image x=\"%d\" y=\"0\" width=\"%d\" height=\"%d\" xlink:href=\"";
|
||||||
const char img_slot_rear[] = "\"></image>\n";
|
const char img_slot_rear[] = "\"></image>";
|
||||||
const char svg_tail[] =
|
const char svg_tail[] =
|
||||||
" </g>\n"
|
" </g>\n"
|
||||||
"</svg>";
|
"</svg>";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user