Skip to content

Commit

Permalink
make readSamples standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeplf committed Jul 4, 2023
1 parent 5355072 commit 879c914
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/readers/morphologySWC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@ class SWCTokenizer
morphio::readers::ErrorMessages err_;
};

std::vector<morphio::readers::Sample> readSamples(const std::string& contents,
const morphio::readers::ErrorMessages& err
) {
std::vector<morphio::readers::Sample> samples;
morphio::readers::Sample sample;

SWCTokenizer tokenizer{contents, err};
tokenizer.consume_line_and_trailing_comments();

while (!tokenizer.done()) {
sample.lineNumber = static_cast<unsigned int>(tokenizer.lineNumber());

int64_t id = tokenizer.read_int();
if (id < 0) {
throw morphio::RawDataError(err.ERROR_NEGATIVE_ID(sample.lineNumber));
}

sample.id = static_cast<unsigned int>(id);

sample.type = static_cast<morphio::SectionType>(tokenizer.read_int());

for (auto& point : sample.point) {
point = tokenizer.read_float();
}

sample.diameter = 2 * tokenizer.read_float();

int64_t parentId = tokenizer.read_int();
if (parentId < -1) {
throw morphio::RawDataError(err.ERROR_NEGATIVE_ID(sample.lineNumber));
} else if (parentId == SWC_UNDEFINED_PARENT) {
sample.parentId = SWC_ROOT;
} else {
sample.parentId = static_cast<unsigned int>(parentId);
}

if (!tokenizer.consume_line_and_trailing_comments()) {
throw morphio::RawDataError(err.ERROR_LINE_NON_PARSABLE(sample.lineNumber));
}
samples.push_back(sample);
}
return samples;
}

} // unnamed namespace

namespace morphio {
Expand Down

0 comments on commit 879c914

Please sign in to comment.