vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / model / FileSourceCode.cpp
blob792be1149739b657c162dbf7eb6f4fe517642dac
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "FileSourceCode.h"
9 #include <string.h>
11 #include "LocatableFile.h"
12 #include "SourceFile.h"
13 #include "SourceLanguage.h"
14 #include "SourceLocation.h"
17 FileSourceCode::FileSourceCode(LocatableFile* file, SourceFile* sourceFile,
18 SourceLanguage* language)
20 fLock("source code"),
21 fFile(file),
22 fSourceFile(sourceFile),
23 fLanguage(language)
25 fFile->AcquireReference();
26 fSourceFile->AcquireReference();
27 fLanguage->AcquireReference();
31 FileSourceCode::~FileSourceCode()
33 fLanguage->ReleaseReference();
34 fSourceFile->ReleaseReference();
35 fFile->ReleaseReference();
39 status_t
40 FileSourceCode::Init()
42 return fLock.InitCheck();
46 status_t
47 FileSourceCode::AddSourceLocation(const SourceLocation& location)
49 // Find the insertion index; don't insert twice.
50 bool foundMatch;
51 int32 index = _FindSourceLocationIndex(location, foundMatch);
52 if (foundMatch)
53 return B_OK;
55 return fSourceLocations.Insert(location, index) ? B_OK : B_NO_MEMORY;
59 bool
60 FileSourceCode::Lock()
62 return fLock.Lock();
66 void
67 FileSourceCode::Unlock()
69 fLock.Unlock();
73 SourceLanguage*
74 FileSourceCode::GetSourceLanguage() const
76 return fLanguage;
80 int32
81 FileSourceCode::CountLines() const
83 return fSourceFile->CountLines();
87 const char*
88 FileSourceCode::LineAt(int32 index) const
90 return fSourceFile->LineAt(index);
94 int32
95 FileSourceCode::LineLengthAt(int32 index) const
97 return fSourceFile->LineLengthAt(index);
101 bool
102 FileSourceCode::GetStatementLocationRange(const SourceLocation& location,
103 SourceLocation& _start, SourceLocation& _end) const
105 int32 lineCount = CountLines();
106 if (location.Line() >= lineCount)
107 return false;
109 bool foundMatch;
110 int32 index = _FindSourceLocationIndex(location, foundMatch);
112 if (!foundMatch) {
113 if (index == 0)
114 return false;
115 index--;
118 _start = fSourceLocations[index];
119 _end = index + 1 < lineCount
120 ? fSourceLocations[index + 1] : SourceLocation(lineCount);
121 return true;
125 LocatableFile*
126 FileSourceCode::GetSourceFile() const
128 return fFile;
132 int32
133 FileSourceCode::_FindSourceLocationIndex(const SourceLocation& location,
134 bool& _foundMatch) const
136 int32 lower = 0;
137 int32 upper = fSourceLocations.Size();
138 while (lower < upper) {
139 int32 mid = (lower + upper) / 2;
140 if (location <= fSourceLocations[mid])
141 upper = mid;
142 else
143 lower = mid + 1;
146 _foundMatch = lower < fSourceLocations.Size()
147 && location == fSourceLocations[lower];
148 return lower;