int OpenRcFile()
{
PGconn *conn;
PGresult *res;
Plugin *p;
Host *h;
char * str[1024], *tmp;
int i,j;
conn=connectDB();
if(PQstatus(conn) == CONNECTION_BAD)
{
#ifdef DEBUG
printf("can`t establish connection to database '%s' as user '%s' \n(%s)\n\n",getenv("PG_DBNAME"), getenv("PG_USER"),PQerrorMessage(conn));
#endif
PQfinish(conn);
return OCF_CANNOTCONNECT;
}
//Создание основной структуры Plugin
p=ListOfRc=(Plugin *)_malloc(sizeof(Plugin));
p->PathOfCfg=0;
p->NameOfCfg=_malloc(sizeof("Rc lines")+1);
strcpy(p->NameOfCfg,"Rc lines");
p->PathOfExec=0;
p->NameOfExec=0;
//Заполнение структуры Plugin структурами Host
sprintf (str,"select status,nameofexec,comment,color,loglevel from rc ORDER BY nameofexec,status;");
res=PQexec(conn,str);
if (PQresultStatus(res) != PGRES_TUPLES_OK) {
PQclear(res);
PQfinish(conn);
return OCF_CANNOTREAD;
}
for (i=0;i<PQntuples(res);i++){
h=__addhost(p,PQgetvalue(res,i,0),PQgetvalue(res,i,1),PQgetvalue(res,i,2));
h->h_addcount=2;
h->h_adddata=(char **) _malloc(2*sizeof(char *));
if (PQgetisnull(res,i,3)){
h->h_adddata[0]=NULL;
}
else {
tmp=PQgetvalue(res,i,3);
h->h_adddata[0]=(char *) _malloc(strlen(tmp)+1);
strcpy(h->h_adddata[0],tmp);
}
if (PQgetisnull(res,i,4)){
h->h_adddata[1]=NULL;
}
else {
tmp=PQgetvalue(res,i,4);
#if DEBUG
fprintf(stderr,"LogLevel of %d string is %s\n",i,tmp);
#endif
h->h_adddata[1]=(char *) _malloc(strlen(tmp)+1);
strcpy(h->h_adddata[1],tmp);
}
}
PQclear(res);
PQfinish(conn);
return OCF_OK;
}