/*linecounter.c*/
/*----------------------------------------------------------------------------*/
/* include file                                                               */
/*----------------------------------------------------------------------------*/
//#include "StdAfx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#include "count_cmn.h"
#include "count_line.h"
#include "count_result.h"
#include "file_cmn.h"

/*----------------------------------------------------------------------------*/
/* define                                                                     */
/*----------------------------------------------------------------------------*/
//#define TESTFILE "D:\\work\\test.c"
#define OUT_FILENAME "LineCounter.csv"

/*----------------------------------------------------------------------------*/
/* static function declaration                                                */
/*----------------------------------------------------------------------------*/

static int executeLineCount(char *pcInfile, char *pcOufile);
static int executeLineCountDir(char *pcInfile, char *pcOutfile);
static int executeLineCountFile(char *pcInfile, char *pcOutfile);
static int getDefaultResultPath(char *pcToolPath, char *pcFilePath);

/*----------------------------------------------------------------------------*/
/* global function                                                            */
/*----------------------------------------------------------------------------*/

int main(int argc, char* argv[]) {
    int iRet;
    char cInpath[FILE_PATH_SIZE];         /*入力されたファイルパス*/
    char cOutpath[FILE_PATH_SIZE];        /*結果出力するファイル名*/
    char *pcToolPath;       /*ツールパス*/  
    
    /*TODO　とりあえず入力引数はひとつのみ*/
    if(argc != 2){
        /*エラー*/
        printf("InputFile not found!!\n");
        goto FUNC_END;
    }
    
    /*入力されたパスを絶対パスに変換して取得*/
    iRet = getAbsolutePath(argv[argc - 1], cInpath);
    if(iRet == RET_NG){
        /*エラー*/
        goto FUNC_END;
    }
#if CNT_DEBUG
    printf("INFILE = %s\n",cInpath);
#endif
    
    /*出力ファイルパス取得*/
    pcToolPath = argv[0];
    iRet = getDefaultResultPath(pcToolPath, cOutpath);
    if(iRet == RET_NG){
        /*エラー*/
        printf("line count error.\n");
        goto FUNC_END;
    }
#if CNT_DEBUG
    printf("OUTFILE = %s\n",cOutpath);//TODO DEBUG
#endif

    initDefaultSetComment();
    
    /*カウントと結果出力*/
    iRet = executeLineCount(cInpath, cOutpath);
    if(iRet == RET_NG){
        /*エラー*/
        printf("line count error.\n");
        goto FUNC_END;
    }
    printf("linecounter finish...\n");

FUNC_END:        

    return 0;
}


static int executeLineCount(char *pcInfile, char *pcOutfile)
{
    int iRet;
    
    iRet = RET_NG;
    
    /*ディレクトリか？*/
    if(isDir(pcInfile)){
        iRet = executeLineCountDir(pcInfile, pcOutfile);
    }else{
        iRet = executeLineCountFile(pcInfile, pcOutfile);        
    }
    return iRet;
}

static int executeLineCountDir(char *pcInfile, char *pcOutfile)
{
    int iRet;
    int i;
    _FileList *pflist;
    
    iRet = RET_NG;
    pflist = NULL;
    
    /*ファイル一覧取得*/
    iRet = getFileList(pcInfile, &pflist);
    if(iRet != RET_OK){
        goto FUNC_END;
    }
    
    for(i = 0; i < pflist->count; i++){
        iRet = executeLineCountFile(pflist->filename[i], pcOutfile);
    }
    
FUNC_END:
    freeFileList(pflist);
    return iRet;
}


static int executeLineCountFile(char *pcInfile, char *pcOutfile)
{
    int iRet;
    FILE *fin;
    
    iRet = RET_NG;
    
    /*ファイルオープン*/
    if( (fin = fopen(pcInfile,"rb") ) != NULL ) {
#if CNT_DEBUG
            printf("count start : %s\n", pcInfile);
#endif
        
        /*カウント*/
        iRet = getLineCount(fin);
        fclose(fin);
        
        if(iRet != RET_OK){
            goto FUNC_END;    
        }
#if CNT_DEBUG
        printf("count finish.\n");
#endif
        
        /*結果出力*/
        putResultLineCount(pcOutfile, pcInfile);
    }
FUNC_END:
    return iRet;
}

//#if 0
///*I:ファイル名、ツールパス名*/
///*O:絶対パス名(メモリ開放は呼び出し元で！)*/
//static int getAbsoluteInputPath(char *pcFile, char **ppcFilePath)
//{
//    int iRet;
//    char *pcPath;
//    char filebuf[FILE_PATH_SIZE];
//    
//    /*絶対パス取得*/
//    iRet = getAbsolutePath(pcFile, filebuf);
//    if(iRet == RET_NG){
//        /*エラー*/
//        goto FUNC_END;
//    }
//    pcPath = malloc(strlen(filebuf) + 1);
//    if(pcPath == NULL){
//        goto FUNC_END;
//    }
//    memset(pcPath, 0x00, strlen(filebuf) + 1);
//    strcpy(pcPath, filebuf);
//    
//    iRet = RET_OK;    
//    *ppcFilePath = pcPath;
//
//FUNC_END:
//    return iRet;
//}
//#endif

/*I:ツールパス名*/
/*O:出力ファイルパス名(メモリ開放は呼び出し元で！)*/
static int getDefaultResultPath(char *pcToolPath, char *pcFilePath)
{
    int iRet;
    char dirbuf[FILE_DIR_SIZE];
    char filebuf[FILE_NAME_SIZE];
    
    iRet = RET_NG;
    
    getDirFileName(pcToolPath, dirbuf, filebuf);
    setDirFileName(dirbuf, OUT_FILENAME, pcFilePath);

    iRet = RET_OK;

    return iRet;
}
