//*****************************************************************************
// GetBufLineStrMulti.cpp : 指定の文字列バッファ内の指定の行から指定行数分の
//                          行文字列を戻す。
//*****************************************************************************

#include "stdafx.h"

#define CR                      0x0D

int GetBufLineCount( BYTE* bbuf , int blen ) ;
int GetBufLineStrMulti( BYTE* bbuf , int blen , CString* plstr , int lineno , int linecount ) ;
CString MakeStr( BYTE* buf , int blen ) ;

//*****************************************************************************
// 機能 :指定文字列バッファ内の行数を戻す。
//  戻り値:行数
//  注意  :改行はCRLF前提
//*****************************************************************************
int GetBufLineCount( BYTE* bbuf , int blen )
{
    if ( blen == 0 )
        return 0 ;

    if ( blen == 1 )
        return 1 ;


    int i ;
    int crcount = 0 ;
    for ( i = 0 ; i < blen ; i++ )
    {
        if ( bbuf[i] == CR )
            crcount++ ;

    }

    if ( blen > 1 && bbuf[blen-2] != CR )
        crcount++ ;

    return crcount ;

}


//*****************************************************************************
// 機能 :指定の文字列バッファ内の指定の行から指定行数分の行文字列を戻す。
//  入力  :lineno : 0から
//  戻り値:設定した行数、plstr[] = 行文字列(CRLFは含まない)
//  注意  :plstr[]の要素はlinecount以上あること
//          改行はCRLF前提
//*****************************************************************************
int GetBufLineStrMulti( BYTE* bbuf , int blen , CString* plstr , int lineno , int linecount )
{
    int i ;
    for ( i = 0 ; i < linecount ; i++ )
        plstr[i] = "" ;                     // まず、各要素をEmptyにする

    CString lstr = "" ;
    
    if ( blen == 0 )
        return 0 ;

    int crcount = 0 ;

    int spos ;
    if ( lineno == 0 )
        spos = 0 ;
    else
    {

        for ( i = 0 ; i < blen ; i++ )
        {
            if ( bbuf[i] == CR )
                crcount++ ;

            if ( crcount == lineno )
                break ;

        }

        if ( i == blen )
            return 0 ;

        spos = i + 2 ;      // 2:CRLF分

    }

    if ( spos >= blen )
        return 0 ;


    int k ;
    for ( k = 0 ; k < linecount && spos < blen ; )
    {
        for ( i = spos ; i < blen ; i++ )
        {
            if ( bbuf[i] == CR )
                break ;
        }

        int npos = i ;

        if ( npos <= spos )
            plstr[k] = "" ;
        else
            plstr[k] = MakeStr( &bbuf[spos] , npos-spos ) ;

        k++ ;
        spos = npos+2 ;     // CRLFの分

    }

    return k ;

}

//***************************************************************************
//  機能 :指定バッファの指定文字数(バイト数)を CString形式で戻す。
//***************************************************************************
CString MakeStr( BYTE* buf , int blen )
{
    CString tstr( (const char*)buf , blen ) ;

    return tstr ;

}


//*****************************************************************************
// GetBufLineStrMulti.cpp : Ver1.00  2007/01/01
// Example :
//  int olen = ostr.GetLength( ) ;
//  int lcount = GetBufLineCount( (BYTE*)(const char*)ostr , olen ) ;
//
//  if ( lcount <= 0 )
//      return NULL ;
//
//  CString* plstr = new CString[lcount] ;      // 解放を忘れるな!!!
//
//  int gettedlcount = GetBufLineStrMulti( (BYTE*)(const char*)ostr , olen , plstr , 0 , lcount ) ;
//  if ( gettedlcount != lcount ) 
//  {
//      ASSERT( FALSE ) ;
//      lcount = gettedlcount ;
//  }
//
//  delete [] plstr ;
//
// Copyright(C) Edcom Inc. (http://www.edcom.jp/) All rights reserved. 
//*****************************************************************************