chxavnextline.cpp
上传用户:zhongxx05
上传日期:2007-06-06
资源大小:33641k
文件大小:2k
- /*============================================================================*
- *
- * (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 rn 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] = ' ';
- return (i == 0 ? 0 : (char*)buf);
- }