Code Nicer

mrt
2013-10-18
2013-10-25
  • mrt
    mrt
    2013-10-18

    Hello everybody,

    I have machine produced code like this:

    topleveltag {
    mainTag {
    valueTag: "mainName"
    }
    itemTag {
    tagID {
    valueTag: "img_1"
    }
    topleveltagType {
    valueTag: "der"
    }
    modelTag {
    keyTag {
    valueTag: "img"
    }
    mapTag {
    subTag1 {
    subTag2 {
    subTag3: ""
    }
    }
    }
    }
    }
    }

    I would like to write a plugin to enhance that code like this:

    topleveltag {
    mainTag{ valueTag: "mainName"}
    itemTag{ tagID{ valueTag: "img_1"} topleveltagType {valueTag: "der"}
    modelTag{ keyTag{valueTag: "img"} mapTag{
    subTag1{
    subTag2{ value: "3"}
    }
    }
    }
    }
    }

    So it would be muche more nice to read. Following I can compile, but does not work:

    LPTSTR currentLine = new TCHAR[1024];
    
    int currentEdit;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&currentEdit);
    HWND curScint = (currentEdit == 0)?nppData._scintillaMainHandle:nppData._scintillaSecondHandle;
    
    ::SendMessage(curScint, SCI_GETLINE , 0, currentLine);
    

    Do you have any suggestions? Appreciate any help. Thanks in advance..
    Mrt

     
  • dail8859
    dail8859
    2013-10-18

    Hi mrt,
    When you say it "does not work" what exactly does it do wrong? What have you tried? What you have there looks correct enough to get you started.

    Off the top of my head you may have a few issues. First, make sure you are using this code in the correct place in your plugin, such that your nppData has been set after setInfo() is called.

    Also, be aware that SCI_GETLINE does not add a null character, you must do that manually.

    EDIT: Also according to the Scintilla documentation, SCI_GETLINE takes a char pointer not a TCHAR pointer. I'm not sure how exactly this works for files that are in different encodings, so this may or may not be an issue.

     
    Last edit: dail8859 2013-10-18
  • mrt
    mrt
    2013-10-24

    Hello,

    please have a look at the attched screenshot and the content of my char pointer after SCI_GETLINE. I work on a xml file which has following in its first line:
    "object {"

    When I do this:
    long doclen = ::SendMessage(curScint, SCI_GETLENGTH , 0, 0);

    for(int t_pos = 0; t_pos < doclen; t_pos++)
    {
        TCHAR c = ::SendMessage(curScint, SCI_GETCHARAT , (WPARAM)t_pos, 0);
    }
    

    my characters are read correctly.

    Is that a unicode issue?

     
    Attachments
  • dail8859
    dail8859
    2013-10-24

    In my DoxyIt plugin, I created a function similar to this:

    char *getLine(int lineNum)
    {
        int lineLen = (int) ::SendMessage(curScint, SCI_LINELENGTH, lineNum, 0);
        char *buffer = new char[lineLen + 1];
    
        ::SendMessage(curScint, SCI_GETLINE, lineNum, (LPARAM) buffer);
        buffer[lineLen] = '\0';
        return buffer;
    }
    

    Hopefully this will get you pointed in the right direction. If you have any more questions feel free to ask. :)

     
    Last edit: dail8859 2013-10-24
  • mrt
    mrt
    2013-10-25

    Thank you for that snippet. I use that.

    I have a test file opened with Notepad++ with "test{" in its first line (screenshot). The debugger tells me, that my char * is properly filled with that string. But then I pass that char pointer to my MessageBox I got chinese symbols, as you can see in my attachment.

     
    Last edit: mrt 2013-10-25
    Attachments
  • dail8859
    dail8859
    2013-10-25

    It looks like the problem is the way you are calling MessageBox with currentLine. You are casting currentLine as a wide character string, however simply casting it doesn't convert the chars to wide chars. Unfortunately there isn't a super clean way of doing it. A quick way of doing it would be to convert currentLine into a string, then to a wstring (wide string).

    std::string s(currentLine);
    std::wstring ws(s.begin(), s.end());
    ::MessageBox(NULL, ws.c_str(), TEXT("Debug Msg"), MB_OK);
    

    There are also other ways of converting it, so do whatever you are comfortable with.

    P.S. I ran into these same issues when doing my first plugin development :)