Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C++

Useful Managers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Aug 2001 80.5K   1.2K   16  
An add-in for Devstudio that provides tag indexing and search, window, bookmarks, session and other managers
/*****************************************************************************
*   $Id: read.c,v 8.1 1999/03/04 04:16:38 darren Exp $
*
*   Copyright (c) 1996-1999, Darren Hiebert
*
*   This source code is released for free distribution under the terms of the
*   GNU General Public License.
*
*   This module contains low level source and tag file read functions (newline
*   conversion for source files are performed at this level).
// Modified by Mike Melnikov
*****************************************************************************/

#include "stdafx.h"

#include "ctags.h"
#include "read.h"

#include "entry.h"
#include "options.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

inputFile File;			/* globally read through macros */

//  This function opens a source file, and resets the line counter.
bool fileOpen(CString fileName,const langType  language )
{
  if((File.hFile = CreateFile(fileName,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL)) == INVALID_HANDLE_VALUE )
  {
    AfxTrace("cannot open \"%s\"", fileName);
    return false;
  }

  DWORD dwFileSize = GetFileSize(File.hFile, NULL);
  if( (File.hFileMapping = CreateFileMapping( File.hFile,NULL,PAGE_READONLY,0,dwFileSize,NULL)) == NULL)
  {
    AfxTrace("cannot map file \"%s\"", fileName);
    return false;
  }
  if( (File.mapViewOfFile = (char*)MapViewOfFile(File.hFileMapping, FILE_MAP_READ, 0, 0, 0 )) == NULL)
  {
    AfxTrace("cannot map file \"%s\"", fileName);
    return false;
  }
  File.mapIndexOfFile = File.mapViewOfFile;
  File.mapLastIndexOfFile = File.mapViewOfFile + dwFileSize;

  File.m_name       = fileName;
  File.newLine      = true;
  File.language     = language;
  File.lineNumber   = 0L;
  return true;
}

void fileClose()
{
  if (File.hFileMapping)
  {
    if( File.mapViewOfFile) UnmapViewOfFile(File.mapViewOfFile);
    if( File.hFileMapping) CloseHandle(File.hFileMapping);
    if( File.hFile!= INVALID_HANDLE_VALUE) CloseHandle(File.hFile);
  }
  File.mapViewOfFile = NULL;
  File.hFileMapping = NULL;
  File.mapIndexOfFile = 0;
  File.hFile = INVALID_HANDLE_VALUE;
}

char* readLine(int ln)
{
  bool found = true;
  while(File.lineNumber!=ln)
  {
    if(fileGetc()==EOF_FILE)
    {
      found = false;
      break;
    }
  }
  if(found)
  {
    int c,i=0;
    char* line = new char[2000],*l=line;
    fileUngetc();
    do
      c = fileGetc();
    while (isspace(c));
    *l++ = c;
    while ((c = fileGetc()) != EOF_FILE && (c != NEWLINE))
      *l++ = c;
    *l = '\0';
    return line;
  }
  return 0;
}

/*  Action to take for each encountered source newline.
*/
static void fileNewline()
{
  File.newLine = false;
  File.lineNumber++;
//  AfxTrace("%6d: ", File.lineNumber);
}

static int fileGetC()
{
  if(File.mapIndexOfFile==File.mapLastIndexOfFile)
    return EOF_FILE;
  return *File.mapIndexOfFile++;
}

//  This function reads a single character from the stream, performing newline  canonicalization.

int fileGetc()
{
  int c = fileGetC();

  //	If previous character was a newline, then we're starting a line.
  if (File.newLine )
    fileNewline();

  if (c == CRETURN)
  {
  //  Turn line breaks into a canonical form. The three commonly
  //  used forms if line breaks: LF (UNIX), CR (MacIntosh), and
  // CR-LF (MS-DOS) are converted into a generic newline.
    c = fileGetC();
    ASSERT(c == NEWLINE); // is CR followed by LF?
    File.newLine = true;
  }
  return c;
}

void fileUngetc()
{
  File.mapIndexOfFile--;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
Mike has been programming in C/C++ for 11 years and Visual C++/MFC for 4 years. His background includes pure and applied mathematics, engineering and physics, and he is currently based in Moscow, Russia.

Comments and Discussions