Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
*.exe
*.out
*.app

*.sublime-*
*~
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
TARGET=libparser.a
SRCS=parser.cpp builtin_handler.cpp
TARGET=libdict_parser.a
SRCS=dict_parser.cpp builtin_handler.cpp
OBJS=$(SRCS:.cpp=.o)
PUBLIC=./output/

CC=g++
CFLAGS=-fPIC -Wall -finline-functions -pipe -g -rdynamic -Wno-invalid-offsetof -Werror

.PHONY: all clean output build
.PHONY: all clean output

all: $(TARGET) output

output:
rm -rf output
mkdir -p output/
cp parser.h ./output/
cp dict_parser.h ./output/
mv *.a ./output/

$(TARGET): $(OBJS)
rm -f $@
ar cr $@ $(OBJS)

%.o: %.cpp
%.o: %.cpp %.h
$(CC) $(CFLAGS) -o $@ -c $<

clean:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ A dict parser which allows users define their own type and parser function
* 其他用户自定义类型(需要灵活支持多种自定义类型)

每一列的数据中均不包含\t字符,每一行以\n结尾。

编译说明:
* 编译库libdict_parser.a: make clean; make;
* 编译运行demo: cd demo; make clean; make; ./demo
* 编译运行unitest: cd unitest; make clean; make; cd output; ./test_parser 2>log
170 changes: 107 additions & 63 deletions builtin_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include "builtin_handler.h"
#include <stdlib.h>

#define CHECK_PARAM(p, q) if (p) { ERRLOG(q); return PARAM_ERROR; }

ERR_CODE int_handler(void *dest, int dest_len, const void *src, int src_len)
ERR_CODE int_handler(void *dest, int *dest_size, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(int) || src_len <= 0),
CHECK_PARAM((!dest || !src || *dest_size < (int)sizeof(int) || src_len <= 0),
"param error!");

char *pend = NULL;
Expand All @@ -24,11 +22,11 @@ ERR_CODE int_handler(void *dest, int dest_len, const void *src, int src_len)
}
}

ERR_CODE float_handler(void *dest, int dest_len, const void *src, int src_len)
ERR_CODE float_handler(void *dest, int *dest_size, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(float) || src_len <= 0),
CHECK_PARAM((!dest || !src || *dest_size < (int)sizeof(float) || src_len <= 0),
"param error!");

char *pend = NULL;
*(float *)dest = strtof((const char *)src, &pend);

Expand All @@ -42,46 +40,43 @@ ERR_CODE float_handler(void *dest, int dest_len, const void *src, int src_len)
}
}

ERR_CODE double_handler(void *dest, int dest_len, const void *src, int src_len)
ERR_CODE string_handler(void *dest, int *dest_size, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(double) || src_len <= 0),
"param error!");

char *pend = NULL;
*(double *)dest = strtod((char *)src, &pend);
CHECK_PARAM((!dest || !src || *dest_size < (int)sizeof(char)), "param error!");

if (((char *)src)[0] != '\0' && *pend == '\0')
if (*dest_size < src_len)
{
return RET_RIGHT;
ERRLOG("string too long[%d/%d]!", src_len, *dest_size);
return LINE_TOO_LONG;
}
else
{
return RET_WRONG;
}
}

ERR_CODE string_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < src_len), "param error!");

memcpy(dest, src, src_len);
return RET_RIGHT;
}

ERR_CODE int_array_handler(void *dest, int dest_len, const void *src, int src_len)
ERR_CODE int_array_handler(void *dest, int *dest_size, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(int) || src_len <= 0),
CHECK_PARAM((!dest || !src || *dest_size < (int)sizeof(int) || src_len <= 0),
"param error!");

int *pint = (int *)dest;
int num = -1;
int i = 0;
char *str1 = NULL;
char *str2 = NULL;
char *pstr = (char *) src;
str1 = strtok_r(pstr, ",", &str2);
char *pstr = (char *)src;
str1 = strtok_r(pstr, ":", &str2);
while (str1)
{
char *pend = NULL;
*pint = (int)strtol((char *)str1, &pend, 10);
if (num == -1)
{
num = (int)strtol((char *)str1, &pend, 10);
}
else
{
*(pint + i) = (int)strtol((char *)str1, &pend, 10);
++i;
}

if (((char *)src)[0] != '\0' && *pend == '\0')
{
Expand All @@ -92,8 +87,7 @@ ERR_CODE int_array_handler(void *dest, int dest_len, const void *src, int src_le
ERRLOG("format error!");
return RET_WRONG;
}
++pint;
if (pint >= (int *)dest + sizeof(int) * dest_len)
if (i >= (*dest_size) / (int)sizeof(int))
{
ERRLOG("buffer not enough");
return RET_WRONG;
Expand All @@ -102,23 +96,44 @@ ERR_CODE int_array_handler(void *dest, int dest_len, const void *src, int src_le
str1 = strtok_r(NULL, ",", &str2);
}

return RET_RIGHT;
if (num >= 0 && num == i)
{
*dest_size = num * (int)sizeof(int);
return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}

}

ERR_CODE float_array_handler(void *dest, int dest_len, const void *src, int src_len)
ERR_CODE float_array_handler(void *dest, int *dest_size, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(float) || src_len <= 0),
CHECK_PARAM((!dest || !src || *dest_size < (int)sizeof(float) || src_len <= 0),
"param error!");

float *pfloat = (float *)dest;
int num = -1;
int i = 0;
char *str1 = NULL;
char *str2 = NULL;
char *pstr = (char *) src;
str1 = strtok_r(pstr, ",", &str2);
str1 = strtok_r(pstr, ":", &str2);
while (str1)
{
char *pend = NULL;
*pfloat = strtof((char *)src, &pend);
if (num == -1)
{
num = (int)strtol((char *)str1, &pend, 10);
}
else
{
*(pfloat + i) = (float)strtof((char *)str1, &pend);
++i;
}
//*pfloat = strtof((char *)src, &pend);

if (((char *)src)[0] != '\0' && *pend == '\0')
{
Expand All @@ -129,42 +144,64 @@ ERR_CODE float_array_handler(void *dest, int dest_len, const void *src, int src_
ERRLOG("format error!");
return RET_WRONG;
}
++pfloat;
if (pfloat >= (float *)dest + sizeof(float) * dest_len)
//++pfloat;
if (i >= (*dest_size) / (int)sizeof(float))
{
ERRLOG("buffer not enough[%d/%d]", src_len, dest_len);
ERRLOG("buffer not enough[%d/%d]", src_len, *dest_size);
return RET_WRONG;
}

str1 = strtok_r(NULL, ",", &str2);
}

return RET_RIGHT;
if (num >= 0 && num == i)
{
*dest_size = num * (int)sizeof(float);
return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}
}

ERR_CODE string_array_handler(void *dest, int dest_len, const void *src, int src_len)
ERR_CODE string_array_handler(void *dest, int *dest_size, const void *src, int src_len)
{
//char *pstr = (char *) dest;

return RET_RIGHT;
}
CHECK_PARAM((!dest || !src || *dest_size < (int)sizeof(char *) || src_len <= 0),
"param error!");

ERR_CODE double_array_handler(void *dest, int dest_len, const void *src, int src_len)
{
CHECK_PARAM((!dest || !src || dest_len < (int)sizeof(double) || src_len <= 0),
"param error!");

double *pdouble = (double *)dest;
char **pstring = (char **)dest;
int num = -1;
int i = 0;
char *str1 = NULL;
char *str2 = NULL;
char *pstr = (char *) src;
str1 = strtok_r(pstr, ",", &str2);
char *pstr = (char *)src;
str1 = strtok_r(pstr, ":", &str2);
while (str1)
{
char *pend = NULL;
*pdouble = strtod((char *)src, &pend);
if (num == -1)
{
char *pend = NULL;
num = (int)strtol(str1, &pend, 10);
if (*pend != '\0')
{
ERRLOG("format error!");
return RET_WRONG;
}
}
else
{
if (strlen(str1) + 1 > MAX_STR_LEN)
{
ERRLOG("string too long[%d/%d]!", (int)strlen(str1) + 1, (int)MAX_STR_LEN);
return LINE_TOO_LONG;
}
memcpy(*(pstring + i), str1, strlen(str1) + 1);
++i;
}

if (((char *)src)[0] != '\0' && *pend == '\0')
if (((char *)src)[0] != '\0')
{
//return RET_RIGHT;
}
Expand All @@ -173,9 +210,7 @@ ERR_CODE double_array_handler(void *dest, int dest_len, const void *src, int src
ERRLOG("format error!");
return RET_WRONG;
}
//*pdouble = atof(str1);
++pdouble;
if (pdouble >= (double *)dest + sizeof(double) * dest_len)
if (i >= (*dest_size) / (int)sizeof(char *))
{
ERRLOG("buffer not enough");
return RET_WRONG;
Expand All @@ -184,7 +219,16 @@ ERR_CODE double_array_handler(void *dest, int dest_len, const void *src, int src
str1 = strtok_r(NULL, ",", &str2);
}

return RET_RIGHT;
if (num >= 0 && num == i)
{
*dest_size = num * (int)sizeof(char *);
return RET_RIGHT;
}
else
{
ERRLOG("format error!");
return RET_WRONG;
}

}

Expand Down
34 changes: 23 additions & 11 deletions builtin_handler.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
/***************************************************************************
*
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
*
**************************************************************************/

/**
* @file parser.h
* @author chenming05(com@baidu.com)
* @date 2017/06/21 14:33:48
* @brief
*
**/

// handler declearation
#ifndef __CODE_MASTER_BUILTIN_HANDLER_H__
#define __CODE_MASTER_BUILTIN_HANDLER_H__
#ifndef GOODCODER_CHENMING05_BUILTIN_HANDLER_H
#define GOODCODER_CHENMING05_BUILTIN_HANDLER_H

#include "parser.h"
#include "dict_parser.h"

// builtin type handlers
ERR_CODE int_handler(void *dest, int , const void *src, int );
ERR_CODE float_handler(void *dest, int , const void *src, int );
ERR_CODE string_handler(void *dest, int , const void *src, int );
ERR_CODE double_handler(void *dest, int , const void *src, int );
ERR_CODE int_handler(void *dest, int *dest_size, const void *src, int src_len);
ERR_CODE float_handler(void *dest, int *dest_size, const void *src, int src_len);
ERR_CODE string_handler(void *dest, int *dest_size, const void *src, int src_len);

// builtin type array handlers
ERR_CODE int_array_handler(void *dest, int , const void *src, int );
ERR_CODE float_array_handler(void *dest, int , const void *src, int );
ERR_CODE string_array_handler(void *dest, int , const void *src, int );
ERR_CODE double_array_handler(void *dest, int , const void *src, int );
ERR_CODE int_array_handler(void *dest, int *dest_size, const void *src, int src_len);
ERR_CODE float_array_handler(void *dest, int *dest_size, const void *src, int src_len);
ERR_CODE string_array_handler(void *dest, int *dest_size, const void *src, int src_len);

#endif
/* vim: set ts=4 sw=4 sts=4 tw=100 */
2 changes: 1 addition & 1 deletion demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CFLAGS=-g -Werror
INCLUDEDIR=-I../output/

LIBDIR=-L../output/ \
-lparser
-ldict_parser

demo: main.cpp comp_data_t_handler.cpp
$(CC) $(CFLAGS) -o $@ $^ $(INCLUDEDIR) $(LIBDIR)
Expand Down
1 change: 1 addition & 0 deletions demo/compdict → demo/comp.dict
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
line1 234 4534543.032343 4,1.2,list
line2 -1 -0.000001 0,0,0
Loading