SOL9 2.0 Class: HexView

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * HexView.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2008/09/04
// 2008/12/16 Modfied addHexData method.

#pragma once
#include <sol\ListView.h>
#include <sol\CourierFont.h>
#include <sol/StringT.h>
#include <sol/StringConverter.h>


namespace SOL {

/**
 * HexView class to display hex data into a listview.
 */
class HexView :public ListView {

private:
    CourierFont    courier;

public:
    HexView() { 
        courier.create(9);
    }


public:
    /**
     * Create a listview and insert columns for hex view
     */
    Boolean create(View* parent, const TCHAR* name, Args& args) {
        Boolean rc = ListView::create(parent, name,     
                args.set(XmNstyle,  (ulong)LVS_REPORT));

        ListViewColumn items[] = {
            {_T("ADDRESS"),     LVCFMT_LEFT, 84},    //2009/11/06
            {_T("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"),     LVCFMT_LEFT, 360},
            {_T("0123456789ABCDEF"), LVCFMT_LEFT, 134},    //2009/11/06
        };

        HFONT hf = courier.getFont();
        setColumn(items, sizeof(items)/sizeof(items[0]));
        send(WM_SETFONT, (WPARAM)hf, 1);

        setExtendedViewStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT); 

        return rc;

    }

public:
    /**
     * Insert hex data into this listview. with a format of the columns.
     * 
     */

    void addHexData(int& id, unsigned char* buff, DWORD cbRead) {
        const int SIXTEEN = 16;
        int lineNum = cbRead/SIXTEEN;
        int lineRem = cbRead % SIXTEEN;
        if (lineRem>0) {
            lineNum++;
        }
        unsigned char* ptr = (unsigned char*)buff;
        char ascString[SIXTEEN+1];
                
        for(int i = 0; i<lineNum; i++) {
            memset(ascString, 0, SIXTEEN+1);

            const int slen = SIXTEEN*3 + 1;
            char hexString[slen];
            memset(hexString, 0, slen); 
            char* b = hexString;

            int lineLen = SIXTEEN;
            if (lineRem >0 && i== (lineNum-1)) {
                lineLen = lineRem;
            }
            for (int j = 0; j<lineLen; j++) {
                char c = *ptr;
                if (c == '\0') {
                    c= '.';
                }
                ascString[j] = c;
                sprintf_s(b, SizeOf(hexString), "%02X ", *ptr++);
                b += 3;
            }
            //2008/12/16
            const TCHAR* items[3];
            TCHAR address[32];
            _stprintf_s(address, SizeOf(address), _T("0X%08X"), id*16);
            
            StringT<TCHAR> thex;
            StringT<TCHAR> tasc;
            StringConverter converter;
            converter.convert(hexString, thex);
            converter.convert(ascString, tasc);
            int n = 0;
            items[n++] = address;
            items[n++] = (const TCHAR*)thex;//hexString;
            items[n++] = (const TCHAR*)tasc;//ascString;

            insertLine(id++, items, n);
        }
    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.