
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#include"zither_lint.h"
#include<almemsys/almemsys.h>

int bufinfile(FILE * FP, char *** bufdata, int * lenp)
{
int stat, ret, delno, no, dno;

no = 0;
dno = 0;
delno = 0;
ret = 0;

char * chA = (char *)calloc(BUF_DEFLEN, sizeof(char));
char * chB = (char *)calloc(BUF_DEFLEN, sizeof(char));

while(1){
   chA = safe_fpcat(FP, chA, &stat, BUF_MAX);
   if(stat == -1){
      fprintf(stderr,"bufinfile(): Terminal of file.\n");
      break;
      }
   else{
      delno = char_count(chA, ','); 
      printf("len =%d dellim =%d :%s\n", (no + 1), delno, chA);
      if(delno != 13){
         printf("%d 行目のデリミターの数が合いません、現在(%d)個です \n", (no + 1), delno);
         exit(1);
         }
      for(dno = 0; dno <= 12; dno++){
         chB = delim_get(chA, chB, ',', &ret, dno);
         if(0 >= (int)strlen(chB) || ret < 0){
            printf("%d 行目 %d 個目のデリミター以降のデータが無いです \n", (no + 1), (dno + 1)); 
            exit(1);
            } 
         else{
            bufdata[no][dno] = safe_memcpy(bufdata[no][dno], chB, BUF_MAX);
//          printf("len %d dellim %d: <%s>\n", (no + 1), (dno + 1), bufdata[no][dno]);
            * lenp = no;
            }
         }
      no++;
      }
   }

free(chA);
free(chB);

return ret;
}



