2
0
forked from Ivasoft/DSView
Files
DSView/DSView/pv/data/decode/annotation.cpp
2022-01-28 20:00:47 +08:00

177 lines
4.4 KiB
C++
Executable File

/*
* This file is part of the PulseView project.
*
* Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
* Copyright (C) 2016 DreamSourceLab <support@dreamsourcelab.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <libsigrokdecode4DSL/libsigrokdecode.h>
#include <vector>
#include <assert.h>
#include "annotation.h"
#include "annotationrestable.h"
#include <cstring>
#include <assert.h>
#include <string.h>
#include "../../config/appconfig.h"
#include "decoderstatus.h"
#include "../../dsvdef.h"
bool is_hex_number_str(const char *str)
{
char c = *str;
int len = 0;
while (c)
{
++len;
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')){
c = *str;
str++;
continue;
}
return false;
}
return len % 2 == 0 && len > 0;
}
namespace pv {
namespace data {
namespace decode {
Annotation::Annotation(const srd_proto_data *const pdata, DecoderStatus *status)
{
assert(pdata);
const srd_proto_data_annotation *const pda =
(const srd_proto_data_annotation*)pdata->data;
assert(pda);
assert(status);
_start_sample = pdata->start_sample;
_end_sample = pdata->end_sample;
_format = pda->ann_class;
_type = pda->ann_type;
_resIndex = 0;
_status = status;
//make resource find key
std::string key;
char **annotations = pda->ann_text;
while(annotations && *annotations) {
key.append(*annotations, strlen(*annotations));
annotations++;
}
if (pda->str_number_hex[0]){
//append numeric string
key.append(pda->str_number_hex, strlen(pda->str_number_hex));
}
AnnotationSourceItem *resItem = NULL;
_resIndex = _status->m_resTable.MakeIndex(key, resItem);
//is a new item
if (resItem != NULL){
char **annotations = pda->ann_text;
while(annotations && *annotations) {
resItem->src_lines.push_back(QString::fromUtf8(*annotations));
annotations++;
}
//get numeric data
if (pda->str_number_hex[0]){
strcpy(resItem->str_number_hex, pda->str_number_hex);
resItem->is_numeric = true;
}
/*
//disable auto convert to numberic format
else if (resItem->src_lines.size() == 1 && _type >= 100 && _type < 200){
if (is_hex_number_str(resItem->src_lines[0].toLatin1().data())){
resItem->is_numeric = true;
}
}
*/
_status->m_bNumeric |= resItem->is_numeric;
}
}
Annotation::Annotation()
{
_start_sample = 0;
_end_sample = 0;
}
Annotation::~Annotation()
{
}
const std::vector<QString>& Annotation::annotations() const
{
AnnotationSourceItem &resItem = *(_status->m_resTable.GetItem(_resIndex));
//get origin data, is not a numberic value
if (!resItem.is_numeric){
return resItem.src_lines;
}
//resItem.str_number_hex must be not null
if (resItem.str_number_hex[0] == 0){
assert(false);
}
if (resItem.cur_display_format != _status->m_format){
resItem.cur_display_format = _status->m_format;
resItem.cvt_lines.clear();
if (resItem.src_lines.size() > 0)
{
//have custom string
for (QString &rd_src : resItem.src_lines)
{
char sz_format_tmp_buf[50] = {0};
QString src = rd_src.replace("{$}", "%s");
const char *num_str = _status->m_resTable.format_numberic(resItem.str_number_hex, resItem.cur_display_format);
sprintf(sz_format_tmp_buf, src.toLatin1().data(), num_str);
resItem.cvt_lines.push_back(QString(sz_format_tmp_buf));
}
}
else{
//have only numberic value
const char *num_str = _status->m_resTable.format_numberic(resItem.str_number_hex, resItem.cur_display_format);
resItem.cvt_lines.push_back(QString(num_str));
}
}
return resItem.cvt_lines;
}
bool Annotation::is_numberic()
{
AnnotationSourceItem *resItem = _status->m_resTable.GetItem(_resIndex);
return resItem->is_numeric;
}
} // namespace decode
} // namespace data
} // namespace pv