1 | #include "xml.h"
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <string.h>
|
---|
4 |
|
---|
5 | extern xSemaphoreHandle MutexAccessFlash;
|
---|
6 | FIL FileConfig;
|
---|
7 |
|
---|
8 | xml_status_typeDef xml_get_data(const TCHAR *path, const char *str_tag, const char *str_attr, const char *tag, const char *attr, char *value);
|
---|
9 | uint32_t _substr(char *str, uint32_t PosStart, char end, char *s);
|
---|
10 |
|
---|
11 | xml_status_typeDef xml_get_tag(const TCHAR *path, const char *str_tag, const char *str_attr, const char *tag, char *value)
|
---|
12 | {
|
---|
13 | if((path == NULL) || (str_tag == NULL) || (value == NULL)) return XML_ERR;
|
---|
14 |
|
---|
15 | return xml_get_data(path, str_tag, str_attr, tag, NULL, value);
|
---|
16 | }
|
---|
17 |
|
---|
18 | xml_status_typeDef xml_get_attr(const TCHAR *path, const char *str_tag, const char *str_attr, const char *attr, char *value)
|
---|
19 | {
|
---|
20 | if((path == NULL) || (str_tag == NULL) || (value == NULL)) return XML_ERR;
|
---|
21 |
|
---|
22 | return xml_get_data(path, str_tag, str_attr, NULL, attr, value);
|
---|
23 | }
|
---|
24 |
|
---|
25 | /*
|
---|
26 | ×òåíèå àòðèáóòà ó òåãà ïî àòðèáóòó(âîçâðàùàåò ñòàòóñ âûïîëíåíèÿ îïåðàöèè)
|
---|
27 | path - óêàçàòåëü íà èìÿ ôàéëà äëÿ ÷òåíèÿ
|
---|
28 | str_tag - óêàçàòåëü íà òåã ó êîòîðîãî íóæíî ïîëó÷èòü àòðèáóò
|
---|
29 | str_attr - óêàçàòåëü íà àòðèáóò äëÿ ïîèñêà (åñëè NULL, òî ïîèñê ïðîèçâîäèòñÿ òîëüêî ïî òåãó)
|
---|
30 | tag - óêàçàòåëü íà òåã äëÿ ïîèñêà
|
---|
31 | attr - óêàçàòåëü íà àòðèáóò äëÿ ïîèñêà
|
---|
32 |
|
---|
33 | value - âîçâðàùàåìîå çíà÷åíèå (âîçâðàùàåò ñòðîêó)
|
---|
34 |
|
---|
35 | âîçâðàùàåìîå çíà÷åíèå: XML_ERR - îòðèáóò íå íàéäåí èëè îøèáêà îòêðûòèÿ ôàéëà, XML_OK - âñå õîðîøî
|
---|
36 | */
|
---|
37 | xml_status_typeDef xml_get_data(const TCHAR *path, const char *str_tag, const char *str_attr, const char *tag, const char *attr, char *value)
|
---|
38 | {
|
---|
39 | uint8_t state = 0;
|
---|
40 | char *p = NULL, BufferRead[128] = {0}/*, tagBuff[64] = {0}*/;
|
---|
41 | volatile _Bool find_attr = 0, find_tag = 0;
|
---|
42 | FRESULT res;
|
---|
43 |
|
---|
44 | if((path == NULL) || (str_tag == NULL) || (value == NULL)) return XML_ERR;
|
---|
45 |
|
---|
46 | memset(value, NULL, strlen(value));
|
---|
47 | xSemaphoreTake(MutexAccessFlash, portMAX_DELAY);
|
---|
48 |
|
---|
49 | res = f_open(&FileConfig, path, FA_READ);
|
---|
50 | if(res == FR_OK){
|
---|
51 | for(state = 0; state <= 2;){
|
---|
52 | switch(state){
|
---|
53 | case 0:
|
---|
54 | if(f_gets(BufferRead, 128, &FileConfig) != NULL) state = 1;
|
---|
55 | else if(f_eof(&FileConfig) != 0) state = 3;
|
---|
56 | break;
|
---|
57 | case 1:
|
---|
58 | if((p = strchr(BufferRead, '<')) != NULL){
|
---|
59 | if(!find_tag){
|
---|
60 | if(strstr(p, str_tag) != NULL){ // íàøëè òåã
|
---|
61 | find_tag = 1;
|
---|
62 | if(str_attr != NULL){
|
---|
63 | if(!find_attr){
|
---|
64 | if(strstr(p, str_attr) != NULL) find_attr = 1; // íàøëè àòðèáóò
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 | if((!find_tag) || ((!find_attr) && (str_attr != NULL))){
|
---|
70 | find_tag = 0;
|
---|
71 | state = 0;
|
---|
72 | }
|
---|
73 | else state = 2;
|
---|
74 | }
|
---|
75 | break;
|
---|
76 | case 2:
|
---|
77 | if(attr != NULL){ // ïîëó÷àåì àòðèáóò
|
---|
78 | if((p = strstr(p, attr)) != NULL){ // àòðèáóò íóæíûé íàì
|
---|
79 | if((p = strchr(p, '"')) != NULL){ // åñòü êàâû÷êà
|
---|
80 | _substr(p, 1, '"', value); // íàøëè àòðèáóò òåãà
|
---|
81 | /*çàêðûòèå íàéòè*/
|
---|
82 | state = 4;
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | else state = 3;
|
---|
87 | }
|
---|
88 | else if(tag != NULL){ // ïîëó÷àåì äàííûå òåãà
|
---|
89 | if((p = strstr(p, tag)) != NULL){ // íàøëè òåã
|
---|
90 | if((p = strchr(p, '>')) != NULL){
|
---|
91 | _substr(p, 1, '<', value);
|
---|
92 | /*çàêðûòèå íàéòè*/
|
---|
93 | state = 4;
|
---|
94 | break;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | else state = 0;
|
---|
98 | }
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | f_close(&FileConfig);
|
---|
103 | }
|
---|
104 | else if(res == FR_NO_FILE) state = 5; //ïðîáëåìû ñ îòêðèòèåì ôàéëà(åãî ìîæåò íå áûòü)
|
---|
105 | else state = 3;
|
---|
106 |
|
---|
107 | xSemaphoreGive(MutexAccessFlash);
|
---|
108 | return (state == 4) ? XML_OK : (state == 5) ? XML_ERR_NO_FILE : XML_ERR;
|
---|
109 | }
|
---|
110 |
|
---|
111 | uint32_t _substr(char *str, uint32_t PosStart, char end, char *s) // âîçâðàùàåò äëèíó ïîäñòðîêè, 0 åñëè íåò êîíå÷íîãî ñèìâîëà
|
---|
112 | {
|
---|
113 | uint8_t len = 0;
|
---|
114 | uint8_t PosEnd = 0;
|
---|
115 | uint8_t StrLen = strlen(str);
|
---|
116 | if(StrLen == 0) return 0;
|
---|
117 |
|
---|
118 | for(; str[PosStart + PosEnd] != end; PosEnd++)
|
---|
119 | {
|
---|
120 | len++;
|
---|
121 | if(StrLen == len) return 0;
|
---|
122 | }
|
---|
123 | for(uint8_t i = 0; i < len; i++) s[i] = str[i + PosStart];
|
---|
124 | s[len] = '\0';
|
---|
125 | return len;
|
---|
126 | }
|
---|