📄 projectx_utils.c
字号:
//should include
#include "ProjectX_Copyright.h"
#include "ProjectX_Common.h"
//std and common include
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
//program specify include
#include "ProjectX_Utils.h"
//area variant define
MEMORY_TRANCE * memory_trance_head = NULL;
//extern variant define
extern char ** pcfg_buf;
extern char base_path[FILENAME_MAX];
void add_node_to_memory_trance(MEMORY_TRANCE * pmt){
MEMORY_TRANCE ** ppmt;
for(ppmt = &memory_trance_head;*ppmt != NULL;ppmt = &((*ppmt)->pNext)){
;
}
*ppmt = pmt;
return;
}
void delete_node_from_memory_trance(void * address){
MEMORY_TRANCE * pc = NULL;
MEMORY_TRANCE * pp = NULL;
BOOL found = FALSE;
for(pc = memory_trance_head;pc != NULL;pc = pc->pNext){
if (pc->address == address) {
found = TRUE;
break;
}
pp = pc;
}
if (!found) {
x_ui_display_error("Memory Leak ..........................Doubt Bug");
address = NULL;
return;
}
if (pp != NULL) {
pp->pNext = pc->pNext;
free(pc->address);
free(pc);
}else{
memory_trance_head = pc->pNext;
free(pc->address);
free(pc);
}
address = NULL;
return;
}
void report_memory_trance_status(void){
#ifdef USE_MEMORY_TRANCE
MEMORY_TRANCE * pmt;
char cache[FILENAME_MAX];
int total_size = 0;
int total_node = 0;
for(pmt = memory_trance_head;pmt != NULL;pmt = pmt->pNext){
total_size += pmt->size_malloc;
total_node++;
}
sprintf(cache,"Total malloc memory size is %d,Total malloc num is %d",total_size,total_node);
x_ui_display_info(cache);
return;
#else
return;
#endif //USE_MEMORY_TRANCE
}
void report_memory_trance_info(){
#ifdef USE_MEMORY_TRANCE
MEMORY_TRANCE * pmt;
char cache[FILENAME_MAX] = "";
for(pmt = memory_trance_head;pmt != NULL;pmt = pmt->pNext){
sprintf(cache,"Memory Leak Doubt where func == %s at lines %d in file %s with size %d",pmt->func_name,pmt->num_lines,pmt->file_name,pmt->size_malloc);
x_ui_display_error(cache);
cache[0] = '\0';
}
return;
#else
return;
#endif //USE_MEMORY_TRANCE
}
void * ms_debug_malloc(char * func_name, int num_line, char * file_name, size_t size){
int length;
char * poffset;
MEMORY_TRANCE * pmt;
if (size <= 0) {
return NULL;
}
pmt = (LPMEMORY_TRANCE)malloc(sizeof(MEMORY_TRANCE));
length = strlen(func_name);
if (length < 50) {
strcpy(pmt->func_name,func_name);
}else{
strcpy(pmt->func_name,"function name too long,you may need rename it");
}
pmt->num_lines = num_line;
poffset = strrchr(file_name,'\\');
poffset++;
strcpy(pmt->file_name,poffset);
pmt->address = malloc(size);
pmt->size_malloc = size;
pmt->pNext = NULL;
add_node_to_memory_trance(pmt);
return pmt->address;
}
void ms_debug_free(char * func_name, int num_line, char * file_name,void * address){
delete_node_from_memory_trance(address);
return;
}
unsigned int get_ip_by_string(char * str){
int ip = 0;
char cache[10];
char * poffset;
if (str[0] == '\0') {
return 0;
}
poffset = strchr(str,'.');
if (poffset == NULL) {
return 0;
}else{
cache[0] = '\0';
strncpy(cache,str,poffset - str);
cache[poffset - str] = '\0';
ip = atoi(cache);
}
str = poffset + 1;
poffset = strchr(str,'.');
if (poffset == NULL) {
return 0;
}else{
cache[0] = '\0';
strncpy(cache,str,poffset - str);
cache[poffset - str] = '\0';
ip = (ip << 8) + atoi(cache);
}
str = poffset + 1;
poffset = strchr(str,'.');
if (poffset == NULL) {
return 0;
}else{
cache[0] = '\0';
strncpy(cache,str,poffset - str);
cache[poffset - str] = '\0';
ip = (ip << 8) + atoi(cache);
}
str = poffset + 1;
ip = (ip << 8) + atoi(str);
return ip;
}
int get_string_by_ip(unsigned int ip,char * str_out){
unsigned int ip_t = 0;
char cache[10];
char * poffset;
str_out[0] = '\0';
cache[0] = '\0';
ip_t = ip;
ip_t = ip_t >> 24;
itoa(ip_t,cache,10);
strcat(cache,".");
poffset = strcpy(str_out,cache);
poffset += strlen(cache);
cache[0] = '\0';
ip_t = ip;
ip_t = ip_t&0xFF0000;
ip_t = ip_t >> 16;
itoa(ip_t,cache,10);
strcat(cache,".");
strcpy(poffset,cache);
poffset += strlen(cache);
cache[0] = '\0';
ip_t = ip;
ip_t = ip_t&0xFF00;
ip_t = ip_t >> 8;
itoa(ip_t,cache,10);
strcat(cache,".");
strcpy(poffset,cache);
poffset += strlen(cache);
cache[0] = '\0';
ip_t = ip;
ip_t = ip_t&0xFF;
itoa(ip_t,cache,10);
strcpy(poffset,cache);
return 0;
}
unsigned int get_ip_by_sockaddr(SOCKADDR_IN * addr){
return get_ip_by_string(inet_ntoa(addr->sin_addr));
}
u_short get_port_by_sockaddr(SOCKADDR_IN * addr){
u_short port = ntohs(addr->sin_port);
return port;
}
/************************************************************************/
/* helper proc */
/************************************************************************/
__int64 GetTime64(){
__int64 i64;
i64 = 100;
return i64;
}
int GetSecondsByint64(__int64 i64){
int iSeconds;
iSeconds = 10;
return iSeconds;
}
int GetMillionSecondsByint64(__int64 i64){
int iMS = 10000;
return iMS;
}
void init_get_basepath(){
char * poffset = NULL;
base_path[0] = '\0';
GetModuleFileName(NULL,base_path,FILENAME_MAX);
poffset = strrchr(base_path,'\\');
if (poffset == NULL) {
x_ui_display_error("Error When Get Basepath");
return;
}
*poffset = '\0';
return;
}
void init_read_specify_file(char * filename,int nlines){
int i;
int j;
FILE * fp;
char cache[FILENAME_MAX];
char c;
if (pcfg_buf != NULL) {
/* free(pcfg_buf) need first? FIXME! */
pcfg_buf = NULL;
}
pcfg_buf = (char **)ms_malloc(nlines * sizeof(char *));
for(i = 0; i < nlines; i++){
pcfg_buf[i] = (char *)ms_malloc(500*sizeof(char));
}
fp = fopen(filename,"rb");
if (fp == NULL) {
cache[0] = '\0';
sprintf(cache,"Error When Use fopen file == %s with %d lines",filename,nlines);
x_ui_display_error(cache);
return;
}
c = fgetc(fp);
i = 0;
j = 0;
while (!feof(fp)) {
if (c == '\n') {
*(*(pcfg_buf + i) +j) = '\0';
i++;
j = 0;
}else{
*(*(pcfg_buf + i) +j) = c;
j++;
}
c = fgetc(fp);
}
if (c != '\n') {
*(*(pcfg_buf + i) +j) = '\0';
}
fclose(fp);
return;
}
void init_free_cache(int nlines){
int i;
for(i = 0; i < nlines; i++){
ms_free(pcfg_buf[i]);
}
ms_free(pcfg_buf);
pcfg_buf = NULL;
return;
}
int init_get_valid_num_config(int nlines){
int iret = 0;
int i;
for(i = 0;i < nlines;i++){
DeleteCommentAndSpace(pcfg_buf[i]);
}
for(i = 0;i < nlines;i++){
if (*(*(pcfg_buf + i) + 0) != '\0'){
iret++;
}
}
return iret;
}
int GetnLinesOfFile(char * filename){
int iLines = 0;
FILE * fp;
char c;
fp = fopen(filename,"rb");
if (fp == NULL) {
return (-1);
}
if (feof(fp)) {
fclose(fp);
return iLines;
}
c = fgetc(fp);
while (!feof(fp)) {
if (c == '\n') {
iLines++;
}
c = fgetc(fp);
}
if (c != '\n') {
iLines++;
}
return iLines;
}
BOOL PopLeftString(char * src,char * dest){
char * poffset;
DeleteCommentAndSpace(src);
for(poffset = src;*poffset != '\0';poffset++){
if ((*poffset == ' ')||(*poffset == '\t')) {
break;
}
}
if ((poffset - src) == strlen(src)-1) {
strcpy(dest,src);
src[0] = '\0';
}else{
strncpy(dest,src,poffset - src);
dest[poffset-src] = '\0';
strncpy(src,poffset,strlen(src) - (poffset - src)+1);
}
return TRUE;
}
BOOL DeleteCommentAndSpace(char * strLine){
char * poffset;
int iLen = 0;
poffset = strstr(strLine,"//");
if (poffset != NULL) {
*poffset = '\0';
}
for(poffset = strLine;*poffset != '\0';){
if ((*poffset == ' ')||(*poffset == '\t')) {
poffset++;
iLen = strlen(poffset);
strcpy(strLine,poffset);
strLine[iLen] = '\0';
poffset = strLine;
continue;
}else if (*poffset == '#') {
strLine[0] = '\0';
break;
}else{
break;
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -