
// mysql postgres の接続データを変数に入れる関数です。includ

#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<pthread.h>

#include"sitar.h"
#include"sitar_com.h"
#include<almemsys/almemsys.h>
#include"jpreturn.h"
#include<file_comp/file_comp.h>

// server,username,passwd,database,port,timeouti
// #define DBDATADELIM_BUG

char * db_data_delim(char * backdata, char * db_data, private_data_t *pd, int * ret)
{
int co, cco;

* ret = 0;
co = char_count(db_data,' ');

pd->db.server = safe_memcpy(pd->db.server, "", BUF_LEN);
pd->db.username = safe_memcpy(pd->db.username, "", BUF_LEN);
pd->db.passwd = safe_memcpy(pd->db.passwd, "", BUF_LEN);
pd->db.database = safe_memcpy(pd->db.database, "", BUF_LEN);
pd->db.port = 0;
pd->db.timeout = 0;

// data の大きさに不正がないか
if(1 <= strlen(db_data) || 512 > strlen(db_data)){ 
   // 複数のデータが存在
   if(co > 0){

      for(cco = 0; cco <= co; cco++){
         pd->db.swap = delim_get(db_data, pd->db.swap, ' ', ret, cco);
         rm_crlf(pd->db.swap, strlen(pd->db.swap));

//       printf("BASE_DATA = %s\n", pd->db.swap);

         if(1 == char_count(pd->db.swap,'=')){
            pd->db.swap1 = delim_get(pd->db.swap, pd->db.swap1, '=', ret, 0);
            pd->db.swap2 = delim_get(pd->db.swap, pd->db.swap2, '=', ret, 1);

//          printf("swap1=%s swap2=%s\n", pd->db.swap1, pd->db.swap2);

            if(0 == self_memcmp(pd->db.swap1, "server") && 0 < strlen(pd->db.swap2)){
               pd->db.server = safe_memcpy(pd->db.server, pd->db.swap2, BUF_LEN);
               }
            else if(0 == self_memcmp(pd->db.swap1, "username") && 0 < strlen(pd->db.swap2)){
               pd->db.username = safe_memcpy(pd->db.username, pd->db.swap2, BUF_LEN);
               }
            else if(0 == self_memcmp(pd->db.swap1, "passwd") && 0 < strlen(pd->db.swap2)){
               pd->db.passwd = safe_memcpy(pd->db.passwd, pd->db.swap2, BUF_LEN);
               }
            else if(0 == self_memcmp(pd->db.swap1, "database") && 0 < strlen(pd->db.swap2)){
               pd->db.database = safe_memcpy(pd->db.database, pd->db.swap2, BUF_LEN);
               }
            else if(0 == self_memcmp(pd->db.swap1, "port") && 0 < strlen(pd->db.swap2)){
               pd->db.port = strtol(pd->db.swap2, (char **)NULL, 10);
               }
            else if(0 == self_memcmp(pd->db.swap1, "timeout") && 0 < strlen(pd->db.swap2)){
               pd->db.timeout = strtol(pd->db.swap2, (char **)NULL, 10);
               }


            }

         }

      }
   // データの数がひとつ
   else{
      pd->db.server = safe_memcpy(pd->db.server, db_data, BUF_LEN);
      }  

   }  // 文字数に不正がある
else{
   backdata = safe_sprintf(backdata, BUF_LEN,"db_data_delim(): %s %s\n", db_data, DB_ERR1);
   * ret = -1;
   }


return backdata;
}




