/***************************************************************************
  FstatHandler.cpp
  -----------------------------------------
begin      : 2011/12/06
copyright  : (C) 2005 SolutionBox Inc.
author     : Service 1 Team
email      : svc1@solbox.com
version    : 1.0

CopyRight(C) 2005 SolutionBox Inc. All Rights reserved.
Redistribution and use in source and binary forms, with or with out
modification, are not permitted in outside of SolutionBox Inc.
 ***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "FstatHandler.h"
#include "Logger.h"
#include "DaemonConfigs.h"


CFstatHandler::CFstatHandler()
{
}

void CFstatHandler::Trim( string & str )
{
	if( str.length() == 0 )
		return ;

	// 문자열 뒤의 공백, TAB, CR 등의 문자 제거처리.
	string::size_type pos = str.find_last_not_of(" \a\b\f\n\r\t\v");
	if( pos != string::npos )
		str.erase( pos + 1 );

	// 문자열 앞의 공백, TAB, CR 등의 문자 제거처리.
	pos = str.find_first_not_of(" \a\b\f\n\r\t\v");
	if( pos != string::npos )
		str.erase( 0, pos );
}

int CFstatHandler::GetFdCountOnSystem( std::string szFileName )
{
	char szCmd[1024];	
	FILE *fp;
	char szResult[1024];
	int r = 0;
	
	
	if( CDeamonConfig::GetInstance() == NULL )
	{
		LOG( LERR, "Log: CDeamonConfig::GetInstance() is NULL.");
		return 0;
	}

	// commnad 생성.
    sprintf( szCmd, "%s %s", CDeamonConfig::GetInstance()->GetFdCntCMD().c_str(), szFileName.c_str() );  
	LOG( LDEV, "CMD : %s", szCmd);
    
	// cmd 구동.
	fp = popen( szCmd, "r" );
	if( fp == NULL )
	{
		LOG( LWAR, "Log: popen failed. cmd=%s", szCmd );
		return 0;
	}
    
	while( fgets( szResult, sizeof(szResult) -1, fp) != NULL )
	{
		LOG( LDEV, "%s", szResult);
		r = atoi(szResult);
		break;
	}
	        
	pclose( fp );    

	LOG( LDEV, "FD Count : %d", r);
	return r;
}
