Skip to content

Commit

Permalink
Merge pull request #42 from furuame/zhenw/stfpy-add-header-comments
Browse files Browse the repository at this point in the history
stfpy: Added support to get header comments
  • Loading branch information
bdutro authored Aug 8, 2024
2 parents 94074fa + e338162 commit 9bbe657
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/stf_reader_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ namespace stf {
return *trace_info_records_.back();
}

const std::vector<std::string>& STFReaderBase::getHeaderCommentsString() {
if(!header_comments_.empty() && header_comments_str_.empty()) {
for(const auto& c : header_comments_) {
header_comments_str_.emplace_back(c->getData());
}
}
return header_comments_str_;
}

int STFReaderBase::close() {
version_.reset();
header_comments_.clear();
Expand Down
13 changes: 13 additions & 0 deletions stf-inc/stf_reader_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace stf {
protected:
STFRecord::ConstHandle<VersionRecord> version_; /**< Version record */
std::vector<STFRecord::ConstHandle<CommentRecord>> header_comments_; /**< Header commment records */
std::vector<std::string> header_comments_str_; /** String type of header comments*/
std::vector<STFRecord::ConstHandle<TraceInfoRecord>> trace_info_records_; /**< Trace info records */
STFRecord::ConstHandle<TraceInfoFeatureRecord> trace_features_; /**< Trace feature records */

Expand Down Expand Up @@ -128,6 +129,18 @@ namespace stf {
return trace_features_;
}

/**
* Gets the header comments
*/
inline const std::vector<STFRecord::ConstHandle<CommentRecord>>& getHeaderComments() const {
return header_comments_;
}

/**
* Gets the header comments in vector type of std::string
*/
const std::vector<std::string>& getHeaderCommentsString();

/**
* Returns the number of records read so far
*/
Expand Down
22 changes: 22 additions & 0 deletions stfpy/stfpy/stf_inst_reader.pxd
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
# distutils: language = c++

from cython.operator cimport dereference
from stfpy.stf_lib.stf_inst_reader cimport STFInstReader as _STFInstReader
from stfpy.stf_lib.stf_inst_reader cimport HeaderCommentsType as _HeaderCommentsType, HeaderCommentsTypeIterator as _HeaderCommentsTypeIterator

ctypedef _STFInstReader.iterator _STFInstReaderIterator

cdef class HeaderCommentsTypeIterator:
cdef _HeaderCommentsTypeIterator c_it
cdef _HeaderCommentsTypeIterator c_end_it

@staticmethod
cdef inline HeaderCommentsTypeIterator _construct(const _HeaderCommentsType* vec):
it = HeaderCommentsTypeIterator()
it.c_it = dereference(vec).begin()
it.c_end_it = dereference(vec).end()
return it

cdef class HeaderCommentsType:
cdef const _HeaderCommentsType* c_vec

@staticmethod
cdef inline HeaderCommentsType _construct(const _HeaderCommentsType& vec):
new_vec = HeaderCommentsType()
new_vec.c_vec = &vec
return new_vec

cdef class STFInstReaderIterator:
cdef _STFInstReaderIterator c_it
cdef _STFInstReaderIterator c_end_it
Expand Down
40 changes: 35 additions & 5 deletions stfpy/stfpy/stf_inst_reader.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ from stfpy.stf_inst import STFInst
from cython.operator cimport dereference, preincrement
include "stfpy/stf_lib/stf_reader_constants.pxi"

cdef class HeaderCommentsTypeIterator:
def __next__(self):
if self.c_it == self.c_end_it:
raise StopIteration
value = dereference(self.c_it)
preincrement(self.c_it)
return value

cdef class HeaderCommentsType:
def __iter__(self):
return HeaderCommentsTypeIterator._construct(self.c_vec)

def __len__(self):
return dereference(self.c_vec).size()

def __getitem__(self, idx):
return dereference(self.c_vec).at(idx)

def __bool__(self):
return not dereference(self.c_vec).empty()

cdef class STFInstReaderIterator:
def __next__(self):
if self.c_it == self.c_end_it:
Expand All @@ -24,11 +45,11 @@ cdef class STFInstReader:
size_t buffer_size = __DEFAULT_BUFFER_SIZE,
bint force_single_threaded_stream = False):
self.c_reader = new _STFInstReader(filename,
only_user_mode,
enable_address_translation,
filter_mode_change_events,
buffer_size,
force_single_threaded_stream)
only_user_mode,
enable_address_translation,
filter_mode_change_events,
buffer_size,
force_single_threaded_stream)

def __dealloc__(self):
del self.c_reader
Expand All @@ -44,3 +65,12 @@ cdef class STFInstReader:

def close(self):
dereference(self.c_reader).close()

def getMajorVersion(self):
return self.c_reader.major()

def getMinorVersion(self):
return self.c_reader.minor()

def getHeaderComments(self):
return HeaderCommentsType._construct(dereference(self.c_reader).getHeaderCommentsString())
9 changes: 9 additions & 0 deletions stfpy/stfpy/stf_lib/stf_inst_reader.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# distutils: language = c++

from libc.stdint cimport *
from libcpp.vector cimport vector
from cython.cimports.libcpp.string import string
from stfpy.stf_lib.stf_inst cimport STFInst

ctypedef vector[string] HeaderCommentsType
ctypedef vector[string].const_iterator HeaderCommentsTypeIterator

cdef extern from "stf_inst_reader.hpp" namespace "stf":
cdef cppclass STFInstReader:
cppclass iterator:
Expand All @@ -17,3 +23,6 @@ cdef extern from "stf_inst_reader.hpp" namespace "stf":
void close()
iterator begin()
iterator end()
uint32_t major()
uint32_t minor()
const HeaderCommentsType& getHeaderCommentsString()

0 comments on commit 9bbe657

Please sign in to comment.