Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TracySourceContents.cpp « server - github.com/wolfpld/tracy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ab92f774d46719131dee4c39577acc989388482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "TracySourceContents.hpp"
#include "TracyView.hpp"
#include "TracyWorker.hpp"

namespace tracy
{

SourceContents::SourceContents()
    : m_file( nullptr )
    , m_fileStringIdx( 0 )
    , m_data( nullptr )
    , m_dataBuf( nullptr )
    , m_dataSize( 0 )
{
}

SourceContents::~SourceContents()
{
    delete[] m_dataBuf;
}

void SourceContents::Parse( const char* fileName, const Worker& worker, const View& view )
{
    if( m_file == fileName ) return;

    m_file = fileName;
    m_fileStringIdx = worker.FindStringIdx( fileName );
    m_lines.clear();
    if( fileName )
    {
        uint32_t sz;
        const auto srcCache = worker.GetSourceFileFromCache( fileName );
        if( srcCache.data != nullptr )
        {
            m_data = srcCache.data;
            sz = srcCache.len;
        }
        else
        {
            FILE* f = fopen( view.SourceSubstitution( fileName ), "rb" );
            if( f )
            {
                fseek( f, 0, SEEK_END );
                sz = ftell( f );
                fseek( f, 0, SEEK_SET );
                if( sz > m_dataSize )
                {
                    delete[] m_dataBuf;
                    m_dataBuf = new char[sz];
                    m_dataSize = sz;
                }
                fread( m_dataBuf, 1, sz, f );
                m_data = m_dataBuf;
                fclose( f );
            }
            else
            {
                m_file = nullptr;
            }
        }

        if( m_file )
        {
            Tokenizer tokenizer;
            auto txt = m_data;
            for(;;)
            {
                auto end = txt;
                while( *end != '\n' && *end != '\r' && end - m_data < sz ) end++;
                m_lines.emplace_back( Tokenizer::Line { txt, end, tokenizer.Tokenize( txt, end ) } );
                if( end - m_data == sz ) break;
                if( *end == '\n' )
                {
                    end++;
                    if( end - m_data < sz && *end == '\r' ) end++;
                }
                else if( *end == '\r' )
                {
                    end++;
                    if( end - m_data < sz && *end == '\n' ) end++;
                }
                if( end - m_data == sz ) break;
                txt = end;
            }
        }
    }
}

}