Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

offset2line.cpp


SourceForge.net Logo     CTTL on    
    SourceForge    
    Download    
    Latest    
    Documentation    
    Index    
    Library    
    News    
    CVS    
    Repository    
   Other    
   Links    

A utility that translates absolute offsets to the actual line numbers in the input file. The program may be helpful to understand CTTL trace file output (example), created during grammar debugging.

// offset2line.cpp
// args: path\file offset
// prints line number of the specified offset

//#define NDEBUG    // define before assert.h to stop assertions from being compiled 
//#define CTTL_TRACE_EVERYTHING //define to turn tracing on
//#define CTTL_TRACE_RULES  //define to turn light tracing on
#define CTTL_TRACE_TRIVIAL

#include <iostream>
#include "cttl/cttl.h"

#include "utils/fileio.h"
#include "utils/itos.h"

using namespace cttl;

int main(int argc, char* argv[])
{
    if ( argc != 3 ) {
        std::cout
            << "Usage: "
            << std::endl
            << '\t'
            << argv[ 0 ]
            << " sourcefile.ext N"
            << std::endl
            << "\t where N is the character offset inside the text file."
            << std::endl
            << "-or-"
            << std::endl
            << '\t'
            << argv[ 0 ]
            << " sourcefile.ext LN"
            << std::endl
            << "\t where N is the line number inside the text file."
            << std::endl
            ;
        return 1;
    }

    input<> inp;

    file2string( argv[ 1 ], inp.text() );
    assert( inp.length() );
    const_edge<> universe( new_edge( inp ) );

    size_t requested_offset = 0;
    if ( argv[ 2 ][ 0 ] == 'L' ) {
        requested_offset = atoi( argv[ 2 ] + 1 );
        assert( requested_offset );
        requested_offset = universe.first.go_line( requested_offset );
    
    } else {
        requested_offset = atoi( argv[ 2 ] );
        assert( requested_offset );
    }

    assert( requested_offset < inp.length() );


    std::cout
        << "File "
        << argv[ 1 ]
        << "; total lines: "
        << universe.second.line()
        << ", ("
        << universe.second.offset()
        << " characters)"
        << std::endl
        ;

    universe.first.offset( requested_offset );
    universe.first.go_line_home();
    universe.second.offset( requested_offset );
    universe.second.go_line_end();

    std::cout
        << "Line# "
        << universe.first.line()
        << ", (characters "
        << universe.first.offset()
        << '-'
        << universe.second.offset()
        << "):"
        << std::endl
//      << universe.text()
//      << std::endl
        ;

    std::string line_extract( universe.text() );

    std::cout
        // replace \r\n with dots, tabs with spaces:
        << xtl_trace_grammar::whitespace2monospace( line_extract )
        << std::endl
        ;

    size_t display_offset = requested_offset - universe.first.offset();
    for ( size_t pad = 0; pad < display_offset; ++pad )
        std::cout << ' ';

    std::cout
        << "^--offset="
        << requested_offset
        << "; ascii="
        << int( universe.first[ display_offset ] )
        << std::endl
        ;

    return 0;
}



Copyright © 1997-2006 Igor Kholodov mailto:cttl@users.sourceforge.net.

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.


Generated on Thu Nov 2 17:44:56 2006 for Common Text Transformation Library by  doxygen 1.3.9.1