Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
igor725 committed May 28, 2022
1 parent bb3d665 commit c1ed461
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "groups.h"
#include "cpe.h"

Client *Clients_List[MAX_CLIENTS] = {0};
Client *Clients_List[MAX_CLIENTS] = {NULL};

cs_byte Clients_GetCount(EClientState state) {
cs_byte count = 0;
Expand Down
31 changes: 19 additions & 12 deletions src/compr.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ cs_bool Compr_Init(Compr *ctx, ComprType type) {
ctx->state = COMPR_STATE_IDLE;
ctx->type = type;

if(type == COMPR_TYPE_DEFLATE || type == COMPR_TYPE_GZIP) {
if(!zlib.definit) return false;
if(type == COMPR_TYPE_DEFLATE || type == COMPR_TYPE_GZIP)
ctx->ret = zlib.definit(
ctx->stream, Z_DEFAULT_COMPRESSION,
Z_DEFLATED, getWndBits(type),
MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY,
ZLIB_VERSION, sizeof(z_stream)
);
} else if(type == COMPR_TYPE_INFLATE || type == COMPR_TYPE_UNGZIP) {
if(!zlib.infinit) return false;
ctx->ret = zlib.infinit(ctx->stream, getWndBits(type), ZLIB_VERSION, sizeof(z_stream));
}
else if(type == COMPR_TYPE_INFLATE || type == COMPR_TYPE_UNGZIP)
ctx->ret = zlib.infinit(
ctx->stream, getWndBits(type),
ZLIB_VERSION, sizeof(z_stream)
);

return ctx->ret == Z_OK;
}
Expand All @@ -123,20 +123,22 @@ cs_ulong Compr_CRC32(const cs_byte *data, cs_uint32 len) {
}

void Compr_SetInBuffer(Compr *ctx, void *data, cs_uint32 size) {
z_streamp stream = (z_streamp )ctx->stream;
if(!ctx->stream) return;
z_streamp stream = (z_streamp)ctx->stream;
ctx->state = COMPR_STATE_INPROCESS;
stream->avail_in = size;
stream->next_in = data;
}

void Compr_SetOutBuffer(Compr *ctx, void *data, cs_uint32 size) {
z_streamp stream = (z_streamp )ctx->stream;
if(!ctx->stream) return;
z_streamp stream = (z_streamp)ctx->stream;
stream->avail_out = size;
stream->next_out = data;
}

INL static cs_bool DeflateStep(Compr *ctx) {
if(!zlib.deflate) return false;
if(!ctx->stream || !zlib.deflate) return false;
z_streamp stream = (z_streamp)ctx->stream;
cs_uint32 outbuf_size = stream->avail_out;

Expand All @@ -154,12 +156,17 @@ INL static cs_bool DeflateStep(Compr *ctx) {
}

INL static cs_bool InflateStep(Compr *ctx) {
if(!ctx->stream || !zlib.inflate) return false;
z_streamp stream = (z_streamp)ctx->stream;
cs_uint32 avail = stream->avail_out;
ctx->written = 0;
ctx->ret = zlib.inflate(stream, Z_NO_FLUSH);
if(ctx->ret == Z_NEED_DICT || ctx->ret == Z_DATA_ERROR ||
ctx->ret == Z_MEM_ERROR) return false;
switch(ctx->ret) {
case Z_NEED_DICT:
case Z_DATA_ERROR:
case Z_MEM_ERROR:
return false;
}
ctx->written = avail - stream->avail_out;
ctx->queued = stream->avail_in;
return true;
Expand All @@ -184,7 +191,7 @@ cs_str Compr_GetLastError(Compr *ctx) {
}

cs_str Compr_GetError(cs_int32 code) {
if(!zlib.error) return "zlib is not loaded correctly";
if(!zlib.error) return "zlib is not loaded";
return zlib.error(code);
}

Expand Down
4 changes: 1 addition & 3 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ if(!checkNumber(value)) { \

CStore *Config_NewStore(cs_str name) {
CStore *store = Memory_Alloc(1, sizeof(CStore));
store->name = String_AllocCopy(name);
cs_char *ext = String_FindSubstr(store->name, ".cfg");
if(ext) *ext = '\0';
store->name = String_TrimExtension(String_AllocCopy(name));
return store;
}

Expand Down
2 changes: 1 addition & 1 deletion src/platforms/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ cs_bool File_ProcClose(cs_file fp) {
}

cs_bool Socket_Init(void) {
WSADATA ws;
WSADATA ws = {0};
return WSAStartup(MAKEWORD(2, 2), &ws) != SOCKET_ERROR;
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ void Plugin_LoadAll(void) {
Directory_Ensure("plugins");
Directory_Ensure("plugins" PATH_DELIM "disabled");

DirIter pIter;
DirIter pIter = {0};
if(Iter_Init(&pIter, "plugins", DLIB_EXT)) {
do {
if(!pIter.isDir && pIter.cfile)
Expand Down
8 changes: 3 additions & 5 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,13 @@ cs_bool Server_Init(void) {
cs_str worlds = Config_GetStrByKey(cfg, CFG_WORLDS_KEY);
cs_uint32 wIndex = 0;
if(*worlds == '*') {
DirIter wIter;
DirIter wIter = {0};
if(Iter_Init(&wIter, "worlds", "cws")) {
do {
if(wIter.isDir || !wIter.cfile) continue;
cs_char wname[64] = {0};
if(String_Copy(wname, 64, wIter.cfile)) {
cs_char *ext = String_FindSubstr(wname, ".cws");
if(ext) *ext = '\0';
(void)String_TrimExtension(wname);
} else continue;

World *tmp = World_Create(wname);
Expand Down Expand Up @@ -322,8 +321,7 @@ cs_bool Server_Init(void) {

if(state == 0) {
skip_creating = false;
cs_char *ext = String_FindSubstr(buffer, ".cws");
if(ext) *ext = '\0';
(void)String_TrimExtension(buffer);
tmp = World_Create(buffer);
if(World_Load(tmp)) {
World_Lock(tmp, 0);
Expand Down
6 changes: 6 additions & 0 deletions src/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ cs_char *String_FindSubstr(cs_str str, cs_str strsrch) {
return strstr(str, strsrch);
}

cs_str String_TrimExtension(cs_str str) {
cs_char *ext = String_LastChar(str, '.');
if(ext) *ext = '\0';
return str;
}

cs_str String_AllocCopy(cs_str str) {
cs_size len = String_Length(str) + 1;
cs_char *ptr = Memory_Alloc(1, len);
Expand Down
1 change: 1 addition & 0 deletions src/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdarg.h>

API cs_char *String_FindSubstr(cs_str str, cs_str strsrch);
API cs_str String_TrimExtension(cs_str str);
API cs_bool String_Compare(cs_str str1, cs_str str2);
API cs_bool String_CaselessCompare(cs_str str1, cs_str str2);
API cs_bool String_CaselessCompare2(cs_str str1, cs_str str2, cs_size len);
Expand Down

0 comments on commit c1ed461

Please sign in to comment.