mirror of
https://github.com/postgres/postgres.git
synced 2025-06-26 12:21:12 +03:00
psql cleanup
This commit is contained in:
@ -6,127 +6,145 @@
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
VariableSpace CreateVariableSpace(void)
|
||||
VariableSpace
|
||||
CreateVariableSpace(void)
|
||||
{
|
||||
struct _variable *ptr;
|
||||
struct _variable *ptr;
|
||||
|
||||
ptr = calloc(1, sizeof *ptr);
|
||||
if (!ptr) return NULL;
|
||||
ptr = calloc(1, sizeof *ptr);
|
||||
if (!ptr)
|
||||
return NULL;
|
||||
|
||||
ptr->name = strdup("@");
|
||||
ptr->value = strdup("");
|
||||
if (!ptr->name || !ptr->value) {
|
||||
free(ptr->name);
|
||||
free(ptr->value);
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const char * GetVariable(VariableSpace space, const char * name)
|
||||
{
|
||||
struct _variable *current;
|
||||
|
||||
if (!space)
|
||||
return NULL;
|
||||
|
||||
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return NULL;
|
||||
|
||||
for (current = space; current; current = current->next) {
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
assert(current->name);
|
||||
assert(current->value);
|
||||
#endif
|
||||
if (strcmp(current->name, name)==0)
|
||||
return current->value;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool GetVariableBool(VariableSpace space, const char * name)
|
||||
{
|
||||
return GetVariable(space, name)!=NULL ? true : false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool SetVariable(VariableSpace space, const char * name, const char * value)
|
||||
{
|
||||
struct _variable *current, *previous;
|
||||
|
||||
if (!space)
|
||||
return false;
|
||||
|
||||
if (!value)
|
||||
return DeleteVariable(space, name);
|
||||
|
||||
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false;
|
||||
|
||||
for (current = space; current; previous = current, current = current->next) {
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
assert(current->name);
|
||||
assert(current->value);
|
||||
#endif
|
||||
if (strcmp(current->name, name)==0) {
|
||||
free (current->value);
|
||||
current->value = strdup(value);
|
||||
return current->value ? true : false;
|
||||
ptr->name = strdup("@");
|
||||
ptr->value = strdup("");
|
||||
if (!ptr->name || !ptr->value)
|
||||
{
|
||||
free(ptr->name);
|
||||
free(ptr->value);
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
previous->next = calloc(1, sizeof *(previous->next));
|
||||
if (!previous->next)
|
||||
return false;
|
||||
previous->next->name = strdup(name);
|
||||
if (!previous->next->name)
|
||||
return false;
|
||||
previous->next->value = strdup(value);
|
||||
return previous->next->value ? true : false;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool DeleteVariable(VariableSpace space, const char * name)
|
||||
const char *
|
||||
GetVariable(VariableSpace space, const char *name)
|
||||
{
|
||||
struct _variable *current, *previous;
|
||||
struct _variable *current;
|
||||
|
||||
if (!space)
|
||||
return false;
|
||||
if (!space)
|
||||
return NULL;
|
||||
|
||||
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name)) return false;
|
||||
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
|
||||
return NULL;
|
||||
|
||||
for (current = space, previous = NULL; current; previous = current, current = current->next) {
|
||||
for (current = space; current; current = current->next)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
assert(current->name);
|
||||
assert(current->value);
|
||||
assert(current->name);
|
||||
assert(current->value);
|
||||
#endif
|
||||
if (strcmp(current->name, name)==0) {
|
||||
free (current->name);
|
||||
free (current->value);
|
||||
if (previous)
|
||||
previous->next = current->next;
|
||||
free(current);
|
||||
return true;
|
||||
if (strcmp(current->name, name) == 0)
|
||||
return current->value;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void DestroyVariableSpace(VariableSpace space)
|
||||
bool
|
||||
GetVariableBool(VariableSpace space, const char *name)
|
||||
{
|
||||
if (!space)
|
||||
return;
|
||||
|
||||
DestroyVariableSpace(space->next);
|
||||
free(space);
|
||||
return GetVariable(space, name) != NULL ? true : false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
SetVariable(VariableSpace space, const char *name, const char *value)
|
||||
{
|
||||
struct _variable *current,
|
||||
*previous;
|
||||
|
||||
if (!space)
|
||||
return false;
|
||||
|
||||
if (!value)
|
||||
return DeleteVariable(space, name);
|
||||
|
||||
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
|
||||
return false;
|
||||
|
||||
for (current = space; current; previous = current, current = current->next)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
assert(current->name);
|
||||
assert(current->value);
|
||||
#endif
|
||||
if (strcmp(current->name, name) == 0)
|
||||
{
|
||||
free(current->value);
|
||||
current->value = strdup(value);
|
||||
return current->value ? true : false;
|
||||
}
|
||||
}
|
||||
|
||||
previous->next = calloc(1, sizeof *(previous->next));
|
||||
if (!previous->next)
|
||||
return false;
|
||||
previous->next->name = strdup(name);
|
||||
if (!previous->next->name)
|
||||
return false;
|
||||
previous->next->value = strdup(value);
|
||||
return previous->next->value ? true : false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool
|
||||
DeleteVariable(VariableSpace space, const char *name)
|
||||
{
|
||||
struct _variable *current,
|
||||
*previous;
|
||||
|
||||
if (!space)
|
||||
return false;
|
||||
|
||||
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
|
||||
return false;
|
||||
|
||||
for (current = space, previous = NULL; current; previous = current, current = current->next)
|
||||
{
|
||||
#ifdef USE_ASSERT_CHECKING
|
||||
assert(current->name);
|
||||
assert(current->value);
|
||||
#endif
|
||||
if (strcmp(current->name, name) == 0)
|
||||
{
|
||||
free(current->name);
|
||||
free(current->value);
|
||||
if (previous)
|
||||
previous->next = current->next;
|
||||
free(current);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void
|
||||
DestroyVariableSpace(VariableSpace space)
|
||||
{
|
||||
if (!space)
|
||||
return;
|
||||
|
||||
DestroyVariableSpace(space->next);
|
||||
free(space);
|
||||
}
|
||||
|
Reference in New Issue
Block a user