/*
* Copyright (c) 2020 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//ini_file_reader.c
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <dlfcn.h>
#include "shared_func.h"
#include "logger.h"
#include "http_func.h"
#include "local_ip_func.h"
#include "pthread_func.h"
#include "fc_memory.h"
#include "ini_file_reader.h"
#define _LINE_BUFFER_SIZE 512
#define _INIT_ALLOC_ITEM_COUNT 32
#define _PREPROCESS_TAG_STR_IF "#@if "
#define _PREPROCESS_TAG_STR_ELSE "#@else"
#define _PREPROCESS_TAG_STR_ENDIF "#@endif"
#define _PREPROCESS_TAG_STR_FOR "#@for "
#define _PREPROCESS_TAG_STR_ENDFOR "#@endfor"
#define _PREPROCESS_TAG_STR_SET "#@set "
#define _PREPROCESS_TAG_LEN_IF (sizeof(_PREPROCESS_TAG_STR_IF) - 1)
#define _PREPROCESS_TAG_LEN_ELSE (sizeof(_PREPROCESS_TAG_STR_ELSE) - 1)
#define _PREPROCESS_TAG_LEN_ENDIF (sizeof(_PREPROCESS_TAG_STR_ENDIF) - 1)
#define _PREPROCESS_TAG_LEN_FOR (sizeof(_PREPROCESS_TAG_STR_FOR) - 1)
#define _PREPROCESS_TAG_LEN_ENDFOR (sizeof(_PREPROCESS_TAG_STR_ENDFOR) - 1)
#define _PREPROCESS_TAG_LEN_SET (sizeof(_PREPROCESS_TAG_STR_SET) - 1)
#define _PREPROCESS_VARIABLE_STR_LOCAL_IP "%{LOCAL_IP}"
#define _PREPROCESS_VARIABLE_STR_LOCAL_HOST "%{LOCAL_HOST}"
#define _PREPROCESS_VARIABLE_LEN_LOCAL_IP \
(sizeof(_PREPROCESS_VARIABLE_STR_LOCAL_IP) - 1)
#define _PREPROCESS_VARIABLE_LEN_LOCAL_HOST \
(sizeof(_PREPROCESS_VARIABLE_STR_LOCAL_HOST) - 1)
#define _PREPROCESS_TAG_STR_FOR_FROM "from"
#define _PREPROCESS_TAG_LEN_FOR_FROM (sizeof(_PREPROCESS_TAG_STR_FOR_FROM) - 1)
#define _PREPROCESS_TAG_STR_FOR_TO "to"
#define _PREPROCESS_TAG_LEN_FOR_TO (sizeof(_PREPROCESS_TAG_STR_FOR_TO) - 1)
#define _PREPROCESS_TAG_STR_FOR_STEP "step"
#define _PREPROCESS_TAG_LEN_FOR_STEP (sizeof(_PREPROCESS_TAG_STR_FOR_STEP) - 1)
#define _INIT_DYNAMIC_CONTENTS 8
#define _BUILTIN_ANNOTATION_COUNT 3
static AnnotationEntry *g_annotations = NULL;
static int g_annotation_count = 0;
typedef struct {
int count;
int alloc;
char **contents;
} DynamicContents;
typedef struct {
int offset; //deal offset
HashArray *vars; //variables with #@set
} SetDirectiveVars;
typedef struct {
int count;
int alloc;
AnnotationEntry *annotations;
} DynamicAnnotations;
typedef struct {
bool used;
IniContext *context;
DynamicContents dynamicContents;
SetDirectiveVars set;
DynamicAnnotations dynamicAnnotations;
} CDCPair;
typedef struct {
volatile int init_counter;
int alloc;
int count;
int index;
CDCPair *contents;
pthread_mutex_t lock;
} DynamicContentArray;
//dynamic alloced contents which will be freed when destroy
static DynamicContentArray g_dynamic_content_array = {0, 0, 0, 0, NULL};
static int remallocSection(IniSection *pSection, IniItem **pItem);
static int iniDoLoadFromFile(const char *szFilename, \
IniContext *pContext);
static int iniLoadItemsFromBuffer(char *content, \
IniContext *pContext);
static DynamicAnnotations *iniAllocAnnotations(IniContext *pContext,
const int annotation_count);
static AnnotationEntry *iniGetAnnotations(IniContext *pContext);
static SetDirectiveVars *iniGetVars(IniContext *pContext);
#define RETRY_FETCH_GLOBAL(szSectionName, bRetryGlobal) \
((szSectionName != NULL && *szSectionName != '\0') && bRetryGlobal)
static void iniDoSetAnnotations(AnnotationEntry *src, const int src_count,
AnnotationEntry *dest, int *dest_count)
{
AnnotationEntry *pSrc;
AnnotationEntry *pSrcEnd;
AnnotationEntry *pDest;
AnnotationEntry *pDestEnd;
pSrcEnd = src + src_count;
pDestEnd = dest + *dest_count;
for (pSrc=src; pSrc<pSrcEnd; pSrc++)
{
for (pDest=dest; pDest<pDestEnd; pDest++)
{
if (strcmp(pSrc->func_name, pDest->func_name) == 0)
{
break;
}
}
pDest->func_name = pSrc->func_name;
pDest->arg = pSrc->arg;
pDest->func_init = pSrc->func_init;
pDest->func_destroy = pSrc->func_destroy;
pDest->func_get = pSrc->func_get;
pDest->func_free = pSrc->func_free;
pDest->dlhandle = pSrc->dlhandle;
pDest->inited = false;
if (pDest == pDestEnd) //insert
{
++(*dest_count);
pDestEnd = dest + *dest_count;
}
}
}
static AnnotationEntry *iniFindAnnotation(AnnotationEntry *annotatios,
const char *func_name)
{
AnnotationEntry *pAnnoEntry;
if (annotatios == NULL)
{
return NULL;
}
pAnnoEntry = annotatios;
while (pAnnoEntry->func_name != NULL)
{
if (strcmp(func_name, pAnnoEntry->func_name) == 0)
{
return pAnnoEntry;
}
pAnnoEntry++;
}
return NULL;
}
static int iniAnnotationFuncLocalIpGet(IniContext *context,
struct ini_annotation_entry *annotation,
const IniItem *item, char **pOutValue, int max_values)
{
bool need_private_ip;
int count;
int index;
char param[FAST_INI_ITEM_VALUE_SIZE];
const char *next_ip;
char *square_start;
char name_part[16];
strcpy(param, item->value);
memset(name_part, 0, sizeof(name_part));
square_start = strchr(param, '[');
if (square_start != NULL && param[strlen(param) - 1] == ']') {
snprintf(name_part, sizeof(name_part) - 1, "%.*s",
(int)(square_start - param), param);
index = atoi(square_start + 1);
} else {
snprintf(name_part, sizeof(name_part) - 1, "%s", param);
index = -2;
}
need_private_ip = strcasecmp(name_part, "inner") == 0 ||
strcasecmp(name_part, "private") == 0;
next_ip = NULL;
count = 0;
while ((next_ip=get_next_local_ip(next_ip)) != NULL) {
if (count >= max_values) {
break;
}
if (is_private_ip(next_ip)) {
if (need_private_ip) {
pOutValue[count++] = (char *)next_ip;
}
} else {
if (!need_private_ip) {
pOutValue[count++] = (char *)next_ip;
}
}
}
if (count == 0) {
pOutValue[count++] = "";
} else if (index > -2) {
if (index == -1) { //get the last one
if (count > 1) {
pOutValue[0] = pOutValue[count - 1];
}
} else if (index >= count) { //index overflow
logWarning("file: "__FILE__", line: %d, "
"index: %d >= count: %d, set value to empty",
__LINE__, index, count);
pOutValue[0] = "";
} else if (index > 0) {
pOutValue[0] = pOutValue[index];
}
count = 1;
}
return count;
}
static int iniAnnotationFuncShellExec(IniContext *context,
struct ini_annotation_entry *annotation,
const IniItem *item, char **pOutValue, int max_values)
{
int count;
int result;
char *output;
count = 0;
output = (char *)fc_malloc(FAST_INI_ITEM_VALUE_SIZE);
if (output == NULL) {
return count;
}
if ((result=getExecResult(item->value, output, FAST_INI_ITEM_VALUE_SIZE)) != 0)
{
logWarning("file: "__FILE__", line: %d, "
"exec %s fail, errno: %d, error info: %s",
__LINE__, item->value, result, STRERROR(result));
free(output);
return count;
}
if (*output == '\
没有合适的资源?快使用搜索试试~ 我知道了~
libfastcommon的最新版本的源码安装包libfastcommon-1.0.70.tar.gz
需积分: 5 0 下载量 145 浏览量
2023-12-05
15:32:52
上传
评论
收藏 243KB GZ 举报
温馨提示
共164个文件
c:79个
h:54个
md:3个
libfastcommon的最新版本的源码安装包libfastcommon-1.0.70.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
libfastcommon的最新版本的源码安装包libfastcommon-1.0.70.tar.gz (164个子文件)
ini_file_reader.c 92KB
shared_func.c 87KB
sockopt.c 57KB
server_id_func.c 55KB
fastcommon.c 38KB
fast_mblock.c 35KB
logger.c 32KB
hash.c 30KB
connection_pool.c 26KB
system_info.c 25KB
sched_thread.c 24KB
uniq_skiplist.c 18KB
fast_allocator.c 17KB
json_parser.c 17KB
avl_tree.c 15KB
multi_socket_client.c 13KB
http_func.c 13KB
multi_skiplist.c 12KB
locked_timer.c 11KB
test_skiplist.c 11KB
flat_skiplist.c 10KB
base64.c 10KB
md5.c 10KB
skiplist_set.c 9KB
process_ctrl.c 9KB
pthread_func.c 9KB
test_uniq_skiplist.c 8KB
fast_task_queue.c 8KB
thread_pool.c 8KB
ioevent_loop.c 8KB
test_mblock.c 7KB
common_blocked_queue.c 7KB
fc_queue.c 7KB
test_multi_skiplist.c 7KB
fast_mpool.c 7KB
id_generator.c 7KB
test_file_lock.c 7KB
char_converter.c 6KB
fast_timer.c 6KB
ioevent.c 6KB
fast_buffer.c 6KB
sorted_queue.c 6KB
buffered_file_writer.c 6KB
test_sorted_array.c 5KB
test_skiplist_set.c 5KB
chain.c 5KB
test_queue_perf.c 5KB
sorted_array.c 5KB
test_sorted_queue.c 5KB
char_convert_loader.c 5KB
local_ip_func.c 4KB
test_data_visible.c 4KB
test_thread_pool.c 4KB
test_blocked_queue.c 4KB
test_json_parser.c 4KB
array_allocator.c 4KB
test_server_id_func.c 4KB
test_pthread_wait.c 3KB
test_file_write_hole.c 3KB
fast_blocked_queue.c 3KB
test_atomic.c 3KB
test_pipe.c 3KB
test_crc32.c 3KB
test_ini_parser.c 2KB
test_pthread_lock.c 2KB
test_mutex_lock_perf.c 2KB
test_normalize_path.c 2KB
test_allocator.c 2KB
test_split_string.c 2KB
test_thread_local.c 2KB
test_sched_thread.c 2KB
test_char_convert_loader.c 2KB
test_char_convert.c 2KB
shared_buffer.c 2KB
test_id_generator.c 2KB
test_logger.c 2KB
io_opt.c 2KB
test_thourands_seperator.c 1KB
fc_memory.c 703B
changelog 3KB
compat 3B
servers.conf 1KB
control 845B
copyright 22KB
README.Debian 201B
format 12B
.gitignore 2KB
shared_func.h 36KB
sockopt.h 24KB
ini_file_reader.h 23KB
connection_pool.h 17KB
fast_mblock.h 14KB
logger.h 13KB
common_define.h 12KB
hash.h 12KB
fast_task_queue.h 9KB
uniq_skiplist.h 8KB
server_id_func.h 8KB
skiplist.h 7KB
php7_ext_wrapper.h 7KB
共 164 条
- 1
- 2
资源评论
一剑
- 粉丝: 601
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功