Why can't I import files larger than 64KB into my MLE?

A MLE can not import or export text in bigger chunks than 64K. The below code shows how to solve this.

void Editor::impTxt(const char *filename)
{
  FILE *ptr;
  long dummy=-1;
  long result;

  ptr = fopen(filename,"rb");
  fseek(ptr,0,SEEK_END);
  unsigned long size = ftell(ptr);

  fseek(ptr,0,SEEK_SET);
  void *buf = malloc(0xF000);

  mle->sendEvent(MLM_SETIMPORTEXPORT, buf, (unsigned long)  0xF000);

  mle->sendEvent(MLM_FORMAT, MLFIE_CFTEXT, NULL);

  while (feof(ptr) == 0) {
    result = fread(buf, 1, 0xE000, ptr);
    mle->sendEvent(MLM_IMPORT, &dummy, result);
  }
  fclose(ptr);
  free(buf);
}

The same applies to export. If the MLE contains >64K text you have to export the text in chunks like this:

void Editor::saveFile(void)
{
  IString filename;
  IEventData rc;
  IFileDialog::Settings settings;

  settings.setTitle("Save textfile");
  settings.setFileName(fname);
  settings.setOKButtonText("Save file");
  settings.setSaveAsDialog();

  IFileDialog file(desktopWindow(),this,settings,
                   IFileDialog::defaultStyle()
                   | IFileDialog::selectableListbox);
  if (file.pressedOK() == false)
    return ;
  filename = file.fileName();
  if (filename.size() == 0)
    return;
  mle->exportToFile(filename);

  void *buf = malloc(0xF000);
  long ppt=0;

  mle->sendEvent(MLM_SETIMPORTEXPORT, buf, (unsigned long)  0xF000);

  mle->sendEvent(MLM_FORMAT, MLFIE_NOTRANS, NULL);
  ofstream fil(filename);

  do
    {
      long amount = 0xE000;
      rc = mle->sendEvent(MLM_EXPORT, &ppt, &amount);
      fil.write((char *) buf, rc.asUnsignedLong());
    } while (rc.asUnsignedLong() > 0);
}

Credit: Ivar E. Hosteng


[Back: How do I change the font in an MLE?]
[Next: How do I get PM screen size?]