코드 정리가 필요함.

그리고 c 로 만든 동적 array (벡터)가 필요한 시점이기두 하고.

buf 저거를 tian_array_add (array, buf) 이런 식으로 추가하려고~~

암튼 저런 식으로 작성한다는거 보셈~

malloc (calloc), realloc 이거 적절히 쓰면 동적으로 늘였다 줄였다 하면서 받을 수 있음.

malloc, realloc 사용 비용은 나도 모름. 어쩌면 buf[1024] 해놓고 strndup 쓰는게

성능상 나을 수도..(뇌피셜임) 암튼 속도 필요한 부분 아니라 편한데로 썼음. ㅋㅋㅋ

담번에 Array 구현한 소소를 올리겠음.

다만, 깔끔하게 정리해서 올리진 않아요~~ ㅋㅋㅋ

그리고 이 소소는 오픈소소 아님.


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


typedef struct {

  /* 여기에 array 넣어주까? */

} TianConf;


TianConf *tian_conf_new ()

{

  TianConf *conf;


  conf = calloc (1, sizeof (TianConf));


  return conf;

}


void tian_conf_free (TianConf *conf)

{

  free (conf);

}


int main (int argc, char **argv)

{

  TianConf *conf;


  conf = tian_conf_new ();


  FILE   *file;

  char   *line    = NULL;

  size_t  linecap = 0;

  ssize_t linelen;


  int   len = 1;

  char *buf = calloc (1, len);

  char *key = NULL;


  file = fopen ("sample.conf", "rw");


  while (fread (&buf[len - 1], 1, 1, file) > 0)

  {

    if (buf[len - 1] == '=')

    {

      buf[len - 1] = 0;

      key = buf;

      len = 1;

      buf = calloc (1, len);

      printf ("key: |%s|\n", key);

      continue;

    }


    if (buf[len - 1] == '\n')

    { /* 동일 코드 중복되는데.. 동일 코드 반복되는거 없애면 좋구 */

      buf[len - 1] = 0;

      key = buf;

      len = 1;

      buf = calloc (1, len);

      printf ("value: |%s|\n", key);

      continue;

    }


    if (!buf[len - 1])

      break;


    len++;


    buf = realloc (buf, len);

  }


  fclose (file);

  tian_conf_free (conf);


  return 0;

}