News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

issue on NullLoader class

Started by blueshake, December 01, 2009, 12:07:41 PM

Previous topic - Next topic

Jenna

Quote from: blueshake on December 04, 2009, 06:00:57 AM
QuoteNevertheless this code should be changed to something that works correctly (or removed), but I think it can make sense to have it.


Waiting for your good news.

To make it clear: the code to change is not the NullLoader code (in my opinion), but the code that uses it.
But I don't think I find the time to dig into it soon.

blueshake

it is Ok if NullLoader class can load the text from the Editor when I set the args to true.

Anyway thanks your effort.
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

Jenna

This snippet (FileManager::Load) copies the bytes, m_pchData points to, into NullLoader's data, so no crash anymore (I'm not sure if everything is correct, as written before I have no time, to work on it seriously).

                if(ed && fileName == ed->GetFilename())
                {
                    wxString s(ed->GetControl()->GetText());
                    int len = s.length() + 1;
                    NullLoader *nl = new NullLoader(file, (char*)wxStrcpy(new wxChar[len], s.c_str()), len * sizeof(wxChar));
                    return nl;
                }


But you get a problem here:

If I see it correctly, FileLoader loads the content of the files into data (byte by byte from disk), but the editors hold a widechar-string (at least in unicode-builds), so loading a file from disk or via fileloader does not lead to the same content automatically and parsing might fail (happens here for example).

blueshake

#18
hi,jens:

just as what visualFc do ,what about using this way?


NullLoader *nl = new NullLoader(file, strcpy(new char[len* sizeof(wxChar)], (const char*)s.mb_str()), len * sizeof(wxChar));


Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

thomas

#19
Quote from: blueshake on December 04, 2009, 11:18:35 AM
it is Ok if NullLoader class can load the text from the Editor when I set the args to true.
No, because then it is not a NullLoader any more. However, if you insist of doing a fix on loader level, you can write a class like this:

class EditorReuser : public LoaderBase
{
 EditorReuser(cbEditor const& e)
   {
     wxString s(e.GetControl()->GetText());
     fileName = e.GetFilename();
     data = new char[s.length() * sizeof(wxChar)];
     len = s.length() * sizeof(wxChar);
     Ready();
   }
   void operator()(){};
};


Then use a EditorReuser instead of a NullLoader.

Note: not checked for typos or correctness.

Edit: also note that you may have to convert the editor's text, since that will be Unicode characters, not UTF-8 or some Windows codepage (which the parser might expect). Or, you will have to tell the parser that the encoding is Unicode.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

blueshake

Thanks. I will have a try when I get my time.
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?

blueshake

hi,devs:

I think we can learn something from file.h of wxWidget.

code snippet:

  bool Write(const wxString& s, const wxMBConv& conv = wxConvUTF8)
  {
      const wxWX2MBbuf buf = s.mb_str(conv);
      if (!buf)
          return false;
      size_t size = strlen(buf);
      return Write((const char *) buf, size) == size;
  }
Keep low and hear the sadness of little dog.
I fall in love with a girl,but I don't dare to tell her.What should I do?