chxavnextline.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 114 行

CPP
114
字号
/*============================================================================*
 *
 * (c) 1995-2002 RealNetworks, Inc. Patents pending. All rights reserved.
 *
 *============================================================================*/

#include "chxavbuffer.h" 
#include "chxavnextline.h"

static const int InitialLineBufSize = 320;

CHXAvNextLine::CHXAvNextLine(const CHXString& name)
     : m_name(name),
       m_pFile(0),
       m_buf(InitialLineBufSize),
       m_lineNum(0)
{
}

CHXAvNextLine::~CHXAvNextLine()
{
    Close();
}

bool CHXAvNextLine::Open()
{
    m_pFile = (m_name == "") ? stdin : fopen(m_name, "r");

    return m_pFile != 0;
}

void CHXAvNextLine::Close()
{
    if (m_name != "" && m_pFile)
	fclose(m_pFile);
    m_pFile = 0;
}

bool CHXAvNextLine::IsOpen() const
{
    return m_pFile != 0;
}

bool CHXAvNextLine::Reset()
{
    bool ret = m_pFile && fseek(m_pFile, (long) 0, SEEK_SET) != -1;

    if (ret)
	m_lineNum = 0;

    return ret;
}

bool CHXAvNextLine::GetLine(CHXString& line)
{
    int len = 0;
    if (m_pFile)
    {
	if (FGetS(m_buf, m_pFile))
	{
	    line = (char *)m_buf;
	    len = line.GetLength();
	    m_lineNum++;
	}
    }
    return len > 0;
}

bool CHXAvNextLine::End() const
{
    return m_pFile == 0 || feof(m_pFile);
}

				// replacement for system fgets that
				// looks for \r or \r\n in addtion to \n
char* CHXAvNextLine::FGetS(CHXAvBuffer& buf, FILE* fp)
{
    bool eol = false;
    int i = 0;

    for (i = 0; !eol; ++i)
    {
	int c = fgetc(fp);

	if (c == EOF)
	    break;

	// Make sure the buffer is large enough
	buf.Resize(i + 2);

	((char*)buf)[i] = c;
	
	if (c == '\n')
	    eol = true;
	else if (c == '\r')
	{
	    eol = true;

	    // check for \n and eat it
	    if ((c = fgetc(fp)) == '\n')
		((char*)buf)[++i] = c;
	    else if (c != EOF)
		ungetc(c, fp);
	    else
		break;
	}
    }

    if (buf.MaxLength() > 0)
	((char*)buf)[i] = '\0';

    return (i == 0 ? 0 : (char*)buf);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?