Ver código fonte

Refactor

master
Kirill Nikolaev 2 meses atrás
pai
commit
b70157a3b4
Acessado por: sokket <sokket@oceancraft.ru> ID da chave GPG: 082FDA12D688C5C1
1 arquivos alterados com 28 adições e 47 exclusões
  1. +28
    -47
      main.c

+ 28
- 47
main.c Ver arquivo

@@ -143,6 +143,30 @@ void cleanUpObj(Obj *obj) {
obj->depth = 0;
}

Obj * reallocate_buffer(int *tag_buffer_pos, int *buffer_size, Obj **tag_buffer, int init_buffer_size) {
if (++(*tag_buffer_pos) == *buffer_size) {
if ((*tag_buffer)[*buffer_size - 1].type != VALUE
&& (*tag_buffer)[*buffer_size - 2].type != VALUE
&& (*tag_buffer)[*buffer_size - 3].type != VALUE
) {
printObjBuffer((*tag_buffer), *buffer_size);
for (int j = init_buffer_size; j < *buffer_size; ++j)
cleanUpObj(&(*tag_buffer)[j]);
*buffer_size = init_buffer_size;
*tag_buffer = realloc(*tag_buffer, *buffer_size * sizeof(Obj));
*tag_buffer_pos = 0;
} else {
*buffer_size += 3;
*tag_buffer = realloc(*tag_buffer, *buffer_size * sizeof(Obj));
for (int ib = *tag_buffer_pos; ib < *buffer_size; ib++)
initObj(&(*tag_buffer)[ib]);
}
}
Obj* tag = &(*tag_buffer)[*tag_buffer_pos];
cleanUpObj(tag);
return tag;
}

int main() {
FILE *file = fopen("/home/dragon/CLionProjects/untitled17/example.xml", "r");
if (file) {
@@ -162,64 +186,21 @@ int main() {
if (buffer[i] == '<') {
if (tag->value && strlen(trim(tag->value)) != 0) {
tag->depth = depth + 1;
if (++tag_buffer_pos == buffer_size) {
if (tag_buffer[buffer_size - 1].type != VALUE
&& tag_buffer[buffer_size - 2].type != VALUE
&& tag_buffer[buffer_size - 3].type != VALUE
) {
printObjBuffer(tag_buffer, buffer_size);
for (int j = init_buffer_size; j < buffer_size; ++j)
cleanUpObj(&tag_buffer[j]);
buffer_size = init_buffer_size;
tag_buffer = realloc(tag_buffer, buffer_size * sizeof(Obj));
tag_buffer_pos = 0;
} else {
buffer_size += 3;
tag_buffer = realloc(tag_buffer, buffer_size * sizeof(Obj));
for (int ib = tag_buffer_pos; ib < buffer_size; ib++)
initObj(&tag_buffer[ib]);
}
}
tag = &tag_buffer[tag_buffer_pos];
cleanUpObj(tag);
tag = reallocate_buffer(&tag_buffer_pos, &buffer_size, &tag_buffer, init_buffer_size);
}
tag->type = UNKNOWN;
ignore_tag_options = 0;
} else if (buffer[i] == '/') {
if (tag->type == UNKNOWN) {
if (tag->type == UNKNOWN)
tag->type = CLOSE;
}
} else if (buffer[i] == '>') {
if (tag->type == UNKNOWN) {
if (tag->type == UNKNOWN)
tag->type = OPEN;
}

if (tag->type == CLOSE)
tag->depth = depth--;
if (tag->type == OPEN)
tag->depth = ++depth;

if (++tag_buffer_pos == buffer_size) {
if (tag_buffer[buffer_size - 1].type != VALUE
&& tag_buffer[buffer_size - 2].type != VALUE
&& tag_buffer[buffer_size - 3].type != VALUE
) {
printObjBuffer(tag_buffer, buffer_size);
for (int j = init_buffer_size; j < buffer_size; ++j)
cleanUpObj(&tag_buffer[j]);
buffer_size = init_buffer_size;
tag_buffer = realloc(tag_buffer, buffer_size * sizeof(Obj));
tag_buffer_pos = 0;
} else {
buffer_size += 3;
tag_buffer = realloc(tag_buffer, buffer_size * sizeof(Obj));
for (int ib = tag_buffer_pos; ib < buffer_size; ib++)
initObj(&tag_buffer[ib]);
}
}
tag = &tag_buffer[tag_buffer_pos];
cleanUpObj(tag);

tag = reallocate_buffer(&tag_buffer_pos, &buffer_size, &tag_buffer, init_buffer_size);
} else if (tag->type == UNKNOWN && buffer[i] == ' ')
ignore_tag_options = 1;
else if (tag->type == UNKNOWN && !ignore_tag_options)


Carregando…
Cancelar
Salvar