int EcoString_delStr(ECOSTRTABLE *EcoSTabP, ECOSTRING String,int StrInd) { int LetInd; if (EcoSTabP->UseCount[StrInd]>0) { Error_write(EECOSTR,"EcoString_delStr",ERR_SEVERE, "Trying to delete ECOSTRING %s which is in use %d times", String, EcoSTabP->UseCount[StrInd]); return 0; } /* Look for it in array */ if (!EcoSTabP->CHashTab->HashFunc(String,&LetInd)) { Error_trace("EcoString_delStr",NULL); return 0; } else { EcoSTabP->CheckSums[StrInd]=0; if (!CHash_removeStr(EcoSTabP->CHashTab,String,StrInd,LetInd)) { Error_trace("EcoString_delStr",NULL); return 0; } } /* Return success */ return 1; }
int EcoString_subtractOne(ECOSTRTABLE *EcoSTabP, ECOSTRING String) { int StrInd; /* Look for it in array */ if (!CHash_find(String,EcoSTabP->CHashTab,&StrInd)) { Error_write(EECOSTR,"EcoString_subtractOne",ERR_SEVERE, "%s is not in EcoString table",String); return 0; } else { if (EcoSTabP->UseCount[StrInd]<=0) { Error_write(EECOSTR,"EcoString_subtractOne",ERR_SEVERE, "Use count for %s is invalid (= %d). StrInd = %d", String,EcoSTabP->UseCount[StrInd],StrInd); return 0; } (EcoSTabP->UseCount[StrInd])--; if (EcoSTabP->UseCount[StrInd] == 0) { if (!EcoString_delStr(EcoSTabP,String,StrInd)) { Error_trace("EcoString_subtractOne",NULL); return 0; } } } /* Return success */ return 1; }
int EcoString_copyTStr(ECOSTRTABLE *EcoSTabP, ECOSTRING *To, char *From,int StartPos) { char *TmpStr; *To = NULL; /* Generate the string */ if (!StrUtil_copyTString(&TmpStr,From,StartPos)) { Error_trace("EcoString_copyTStr",NULL); return 0; } if (!EcoString_getPointer(EcoSTabP,To,TmpStr)) { Error_trace("EcoString_copyTStr",NULL); return 0; } /* Return success */ return 1; }
int EcoString_freeStr(ECOSTRTABLE *EcoSTabP, ECOSTRING String) { if (!EcoString_subtractOne(EcoSTabP,String)) { Error_trace("EcoString_freeStr",NULL); return 0; } /* Return success */ return 1; }
int EcoString_initTable(ECOSTRTABLE **EcoSTabP) { if (((*EcoSTabP) = (ECOSTRTABLE *)calloc(1,sizeof(ECOSTRTABLE)))==NULL) { Error_write(ECALLERR,"EcoString_init",ERR_SEVERE,"*EcoSTabP"); return 0; } if (!CHash_setup(NULL,0,&((*EcoSTabP)->CHashTab),CHash_fourLets,1024)) { Error_trace("EcoString_init",NULL); return 0; } return 1; }
DBConnection *DBConnection_new(char *host, char *user, char *pass, char *dbname, unsigned int port) { DBConnection *dbc; MYSQL *mysql; if ((dbc = (DBConnection *)calloc(1,sizeof(DBConnection))) == NULL) { Error_write(ECALLERR,"DBConnection_new",ERR_SEVERE,"dbc"); return NULL; } if ((mysql = mysql_init(NULL)) == NULL) { Error_write(EMYSQLCONN, "DBConnection_new", ERR_SEVERE, " failed creating mysql object in mysql_init, mysql error %s",mysql_error(mysql)); return NULL; } /* on unix, localhost will connect via a local socket by default and ignore the port. If the * user has specified a port then force mysql to use it by setting the protocol to TCP (this is * necessary if using ssh tunnels). */ if (mysql && host && port && !strcmp(host, "localhost")) { const enum mysql_protocol_type arg = MYSQL_PROTOCOL_TCP ; mysql_options(mysql, MYSQL_OPT_PROTOCOL, &arg) ; } if ((mysql_real_connect(mysql,host, user, pass, dbname, port, NULL, 0)) == NULL) { Error_write(EMYSQLCONN, "DBConnection_new", ERR_SEVERE, " dbname %s (host %s user %s pass %s port %d), mysql error %s", dbname,host,user,pass,port,mysql_error(mysql)); return NULL; } EcoString_copyStr(ecoSTable, &(dbc->host), host, 0); EcoString_copyStr(ecoSTable, &(dbc->user), user, 0); if (pass) EcoString_copyStr(ecoSTable, &(dbc->pass), pass, 0); EcoString_copyStr(ecoSTable, &(dbc->dbName), dbname, 0); dbc->port = port; dbc->mysql = mysql; dbc->prepare = DBConnection_prepare; if (!dbc->host || !dbc->user || !dbc->dbName || (pass && !dbc->pass)) { Error_trace("DBConnnection_new",NULL); return NULL; } return dbc; }
int EcoString_getPointer(ECOSTRTABLE *EcoSTabP, ECOSTRING *To, char *From) { int StrInd; /* Look for it in array */ if (!CHash_find(From,EcoSTabP->CHashTab,&StrInd)) { /* If not found add it to array */ if (!EcoString_addStr(EcoSTabP,From,&StrInd)) { Error_trace("EcoString_getPointer",NULL); free(From); return 0; } } else { //printf("Old string %s\n",From); (EcoSTabP->UseCount[StrInd])++; free(From); } /* Set the pointer */ *To = EcoSTabP->CHashTab->Strings[StrInd]; /* Return success */ return 1; }
int EcoString_addStr(ECOSTRTABLE *EcoSTabP, char *String, int *StrInd) { int NElement; if (!CHash_addAllocedStr(EcoSTabP->CHashTab,String)) { Error_trace("EcoString_addStr",NULL); return 0; } NElement = EcoSTabP->CHashTab->NElement; if (NElement==1 || !(NElement%10)) { if ((EcoSTabP->UseCount = (int *)realloc(EcoSTabP->UseCount, (NElement+10)*sizeof(int)))==NULL) { Error_write(EREALLERR,"EcoString_addStr",ERR_SEVERE, "EcoSTabP->UseCount"); return 0; } if ((EcoSTabP->CheckSums = (int *)realloc(EcoSTabP->CheckSums, (NElement+10)*sizeof(int)))==NULL) { Error_write(EREALLERR,"EcoString_addStr",ERR_SEVERE,"EcoSTabP->UseCount"); return 0; } } if (!EcoSTabP->UseCount || !EcoSTabP->CheckSums) { Error_write(EREALLERR,"EcoString_addStr",ERR_SEVERE,"EcoSTabP->UseCount"); return 0; } EcoSTabP->UseCount[NElement-1] = 1; EcoSTabP->CheckSums[NElement-1] = EcoString_genCheckSum(String); *StrInd = NElement-1; /* Return success */ return 1; }