| version 1.1, 2003/04/21 23:50:20 |
version 1.2, 2003/04/23 23:05:18 |
| Line 49 dsn_free_ptr(ptr) |
|
| Line 49 dsn_free_ptr(ptr) |
|
| void |
void |
| dsn_free(el) |
dsn_free(el) |
| struct dsn_elements *el; |
struct dsn_elements *el; |
| { |
{ /* {{{ */ |
| dsn_free_ptr(&(el->phptype)); |
dsn_free_ptr(&(el->phptype)); |
| dsn_free_ptr(&(el->dbsyntax)); |
dsn_free_ptr(&(el->dbsyntax)); |
| dsn_free_ptr(&(el->username)); |
dsn_free_ptr(&(el->username)); |
|
|
| dsn_free_ptr(&(el->socket)); |
dsn_free_ptr(&(el->socket)); |
| dsn_free_ptr(&(el->database)); |
dsn_free_ptr(&(el->database)); |
| dsn_free_ptr(&(el->query)); |
dsn_free_ptr(&(el->query)); |
| } |
} /* }}} */ |
| |
|
| |
#define DSN_LEN(s) (s == NULL ? 0 : strlen(s)) |
| |
|
| |
char * |
| |
dsn_glue(el) |
| |
const struct dsn_elements *el; |
| |
{ /* {{{ */ |
| |
register int len; |
| |
register char *dsn; |
| |
|
| |
/* How much memory we have to allocate for DSN string */ |
| |
len = DSN_LEN(el->phptype) + DSN_LEN(el->dbsyntax) |
| |
+ DSN_LEN(el->username) + DSN_LEN(el->password) |
| |
+ DSN_LEN(el->protocol) + DSN_LEN(el->hostspec) |
| |
+ DSN_LEN(el->port) + DSN_LEN(el->socket) |
| |
+ DSN_LEN(el->database) + DSN_LEN(el->query); |
| |
|
| |
/* 30 characters is enough for element separators */ |
| |
if ((dsn = (char *) malloc((len + 30) * sizeof(char))) == NULL) |
| |
return NULL; /* failure */ |
| |
|
| |
/* Initialize DSN to empty string */ |
| |
*dsn = '\0'; |
| |
|
| |
/* dbsyntax */ |
| |
if (el->dbsyntax != NULL) { |
| |
strcat(dsn, el->dbsyntax); |
| |
} |
| |
/* phptype */ |
| |
if (el->phptype != NULL && strcmp(el->phptype, dsn)) { |
| |
/* Who understands this will get one beer free on my own. :-) */ |
| |
memmove(dsn + strlen(el->phptype) + 1, dsn, strlen(dsn) + 1); |
| |
strcpy(dsn, el->phptype); |
| |
dsn[strlen(dsn)] = '('; /* THIS WON'T WORK: strncat(dsn, "(", 1); */ |
| |
strcat(dsn, ")"); |
| |
} |
| |
if (len - DSN_LEN(el->dbsyntax) - DSN_LEN(el->phptype) > 0) { |
| |
strcat(dsn, "://"); |
| |
} |
| |
/* username */ |
| |
if (el->username != NULL) { |
| |
strcat(dsn, el->username); |
| |
/* password */ |
| |
if (el->password != NULL) { |
| |
strcat(dsn, ":"); |
| |
strcat(dsn, el->password); |
| |
} |
| |
} |
| |
/* protocol */ |
| |
if (el->protocol != NULL && ! strcmp(el->protocol, "tcp")) { |
| |
if (0) { /* strict DSN case, part 1/2 */ |
| |
strcat(dsn, "tcp("); |
| |
} |
| |
/* hostspec */ |
| |
if (el->hostspec != NULL) { |
| |
strcat(dsn, el->hostspec); |
| |
} |
| |
/* port */ |
| |
if (el->port != NULL && strcmp(el->port, "") && atoi(el->port) > 0) { |
| |
strcat(dsn, ":"); |
| |
strcat(dsn, el->port); |
| |
} |
| |
if (0) { /* strict DSN case, part 2/2 */ |
| |
strcat(dsn, ")"); |
| |
} |
| |
} else if (el->protocol != NULL && ! strcmp(el->protocol, "unix")) { |
| |
strcat(dsn, "unix("); |
| |
/* socket */ |
| |
if (el->socket != NULL) { |
| |
strcat(dsn, el->socket); |
| |
} |
| |
strcat(dsn, ")"); |
| |
} else { |
| |
/* hostspec */ |
| |
if (el->hostspec != NULL) { |
| |
strcat(dsn, el->hostspec); |
| |
} |
| |
} |
| |
/* database */ |
| |
if (el->database != NULL) { |
| |
strcat(dsn, "/"); |
| |
strcat(dsn, el->database); |
| |
} |
| |
/* query */ /* TODO: query handling imrpovements */ |
| |
if (el->query != NULL) { |
| |
strcat(dsn, "?"); |
| |
strcat(dsn, el->query); |
| |
} |
| |
|
| |
return dsn; /* success */ |
| |
} /* }}} */ |
| |
|
| #define DSN_TRIM(str, nptr, n) do { back[n] = str[nptr - str]; str[nptr - str] = 0; } while (0) |
#define DSN_TRIM(str, nptr, n) do { back[n] = str[nptr - str]; str[nptr - str] = 0; } while (0) |
| #define DSN_RST(str, nptr, n) (str[nptr - str] = back[n]) |
#define DSN_RST(str, nptr, n) (str[nptr - str] = back[n]) |
|
|
| dsn_parse(input_dsn, el) |
dsn_parse(input_dsn, el) |
| const char *input_dsn; |
const char *input_dsn; |
| struct dsn_elements *el; |
struct dsn_elements *el; |
| { |
{ /* {{{ */ |
| char back[8]; |
char back[8]; |
| char *dsn = strdup(input_dsn); |
char *dsn = strdup(input_dsn); |
| char *c; |
char *c; |
|
|
| dsn_free_ptr(&socket); |
dsn_free_ptr(&socket); |
| dsn_free_ptr(&database); |
dsn_free_ptr(&database); |
| return 0; /* memory allocation failure */ |
return 0; /* memory allocation failure */ |
| } |
} /* }}} */ |
| |
|
| #if defined(SELF) || defined(SELFTEST) || defined(SELF_DSN) |
#if defined(SELF) || defined(SELFTEST) || defined(SELF_DSN) /* {{{ */ |
| |
|
| #include <stdio.h> |
#include <stdio.h> |
| |
|
| int test(const char *dsn) |
int test(const char *dsn) |
| { |
{ |
| |
register char *dsn2; |
| struct dsn_elements e; |
struct dsn_elements e; |
| if (! dsn_parse(dsn, &e)) |
if (! dsn_parse(dsn, &e)) { |
| |
printf("Memory allocation error when parsing DSN string.\n"); |
| return 0; /* failure */ |
return 0; /* failure */ |
| |
} |
| |
|
| |
if ((dsn2 = dsn_glue(&e)) == NULL) { |
| |
printf("Memory allocation error when compiling DSN string.\n"); |
| |
return 0; |
| |
} |
| |
|
| |
if (strcmp(dsn, dsn2)) { |
| |
printf("DSN strings do not match.\n"); |
| |
printf(" original DSN: %s\n", dsn); |
| |
printf(" compiled DSN: %s\n", dsn2); |
| |
} |
| |
free(dsn2); |
| |
|
| printf ("DSN: %s\nArray\n(\n", dsn); |
printf ("DSN: %s\nArray\n(\n", dsn); |
| printf (" [phptype] => %s\n", e.phptype ? e.phptype : ""); |
printf (" [phptype] => %s\n", e.phptype ? e.phptype : ""); |
|
|
| return 0; |
return 0; |
| } |
} |
| |
|
| #endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_DSN) */ |
#endif /* #if defined(SELF) || defined(SELFTEST) || defined(SELF_DSN) */ /* }}} */ |
| |
|
| |
/* Modeline for ViM {{{ |
| |
* vim: set ts=4: |
| |
* vim600: fdm=marker fdl=0 fdc=3: |
| |
* }}} */ |
| |
|