summaryrefslogtreecommitdiff
path: root/plugin/business/tsg-http/test/edit_element_tool.cpp
blob: 74d27ee85172ca6511d02bd8530288180d5ec60e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <sys/types.h>//fstat
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <sys/stat.h>
#include "edit_element.h"

void print_help(void)
{
	printf("Edit element help\n");
	printf("-i Input File Name\n");
	printf("-f Find KeyWords\n");
	printf("-d Distance From Matching\n");
	printf("-t File Content Type, 0: html 1: json\n");
}

char *edit_element_read_file(const char *filename, size_t *input_sz)
{
	struct stat file_info;
	stat(filename, &file_info);
	*input_sz=file_info.st_size;

	FILE* fp=fopen(filename,"r");
	char* input=(char*)malloc(*input_sz);
	fread(input,1,*input_sz,fp);
	fclose(fp);

	return input;
}

int main(int argc, char ** argv)
{
	int distane_from_matching = 0, option=0;
	const char* find=NULL, *filename=NULL;
	
	int oc=0;
	if(argc<3)
	{
		print_help();
		return -1;
	}
	while((oc=getopt(argc,argv,"f:d:t:i:"))!=-1)
	{
		switch(oc)
		{
			case 'i':
				filename=strdup(optarg);
				break;
			case 't':
				option=atoi(optarg);
				break;
			case 'f':
				find=strdup(optarg);
				break;
			case 'd':
				distane_from_matching=atoi(optarg);
				break;
			default:
				printf("Invalid parameter.\n");
				print_help();
				return -1;
		}
	}

	if(distane_from_matching > 128)
	{
		printf("Distance From Matching supports up to 128.\n");
		print_help();
		return -1;
	}

	char *output=NULL, *input=NULL;
	size_t input_sz=0, n_got_rule=0;

	input=edit_element_read_file(filename, &input_sz);
	
	struct edit_element_rule rules[128];
	 memset(rules, 0, sizeof(rules));
	for(int i = 0; i <= distane_from_matching; i++)
	{
		rules[i].debug_for_log=1;
		rules[i].scope=kScopeInside;
		rules[i].distane_from_matching=i;
		rules[i].start_indicator=(char *)"log";
		rules[i].element_treatment=(char *)"mark";
		rules[i].contained_keyword=(char *)find;

		n_got_rule=1;
		printf("distane_from_matching: %d\n", i);
		execute_edit_element_rule(input, strlen(input), &rules[i], n_got_rule, &output, option);
		free(output);	
	}
	free(input);
	return 0;
}