Exemple #1
0
int main(int argc, const char *argv[])
{
    if (argc != 2) {
        printf("Usage: %s filename\n", argv[0]);
        exit(0);
    }
    FILE * fp = fopen(argv[1], "r");
    if (fp == NULL) {
        printf("file not found\n");
        exit(1);
    }
    char str[1024];
    int len = 0;
    len = fread(str, sizeof(char), sizeof(str) / sizeof(char), fp);
    fclose(fp);
    if (len <= 0) {
        printf("No data in file\n");
        exit(2);
    }
    const char * enc_name = get_file_encoding(str, len);
    if (enc_name != NULL) {
        printf("detect charset is [%s]\n", enc_name);
    } else {
        printf("Failed to detect charset\n");
    }

    return 0;
}
void test(const char* path) {
    tinydir_dir dir;
    tinydir_open(&dir, path);
    printf("dir: %s\n", dir.path);
    while(dir.has_next) {
        tinydir_file file;
        if(tinydir_readfile(&dir, &file)== -1) {
            perror("Error");
            return;
        }
        if(!strcmp(file.name, ".") || !strcmp(file.name, ".."))
            continue;

        if(strcmp(get_file_encoding(file.path), "UNKNOWN"))
            printf("%s\n", file.name);
        if(file.is_dir && strcmp(file.name, ".") && strcmp(file.name, "..")) {
            test(file.path);
        }
        tinydir_next(&dir);
    }
    tinydir_close(&dir);
}
Exemple #3
0
bool SciDoc::DoLoadFromFile(const char*filename,bool insert)
{
  _lasterror="";
  errno=0;
  memset(bom,0,sizeof(bom));
  bool rv=true;
  bool ro=GetReadOnly();
  FXTextCodec *codec=NULL;
  if (ro&&insert) {
    _lasterror=_("Document is marked read-only.");
    return false;
  }
  if (FXStat::isDirectory(filename)) {
    _lasterror=_("is a directory");
    return false;
  }
  bool DefaultToAscii=SettingsBase::instance()->DefaultToAscii;
  bool DefaultToSbcs=SettingsBase::instance()->DefaultToSbcs;
  switch (get_file_encoding(filename)) {
    case 'B': { // Binary file
      if ( !ConfirmOpenBinary(this,filename) ) {
        _lasterror=BinaryFileMessage();
        return false;
      }
      if (!insert) { SetUTF8(!DefaultToSbcs); }
      break;
    }
    case 'T': { // Plain US-ASCII text file
      if (!insert) { SetUTF8(!DefaultToAscii); }
      break;
    }
    case 'H': { // High (extended ASCII) text file.
      if (!insert) { SetUTF8(!DefaultToSbcs); }
      break;
    }
    case 'U': { // UTF-8 encoded text file w/o BOM.
      if (!insert) { SetUTF8(true); }
      break;
    }
    case 'M': { // UTF-8 BOM.
      if (!insert) {
        strcpy(bom,"\0357\0273\0277");
        SetUTF8(true);
      }
      break;
    }
    case 'Z': { // Zero-length (empty) file.
      if (!insert) { SetUTF8(!DefaultToAscii); }
      break;
    }
    case 'e': { // UTF-16LE BOM.
      if (!insert) {
        codec=new FXUTF16LECodec();
        strcpy(bom,"\377\376");
        SetUTF8(false);
      }
      break;
    }
    case 'E': { // UTF-16BE BOM.
      if (!insert) {
        codec=new FXUTF16BECodec();
        strcpy(bom,"\376\377");
        SetUTF8(false);
      }
      break;
    }
    case 'F': { // Failure, could not read the file.
      _lasterror=SystemErrorStr();
      return false;
      break;
    }
  }
  FXFile fh(filename, FXFile::Reading);
  if (fh.isOpen()) {
    if (ro) {
      // This might happen e.g. if we are updating a document that has been modified externally
      sendMessage(SCI_SETREADONLY,0,0);
    }
    static const int BUFSIZE=1025;
    char buf[BUFSIZE];
    fh.position(strlen(bom),FXIO::Begin);
    long p=0;
    _loading=!insert;
    if (insert) {
      p=sendMessage(SCI_GETCURRENTPOS, 0,0);
      sendMessage(SCI_BEGINUNDOACTION, 0, 0);
    } else {
      sendMessage(SCI_CLEARALL,0,0);
    }
    do {
      memset(buf,0,BUFSIZE);
      FXival n=fh.readBlock(buf,BUFSIZE-1);
      if (n<0) {
        _lasterror=SystemErrorStr();
        rv=false;
        break;
      }
      buf[n]='\0';
      if (insert) {
        _dirty=true;
        if (GetSelLength()>0) {
          sendString(SCI_REPLACESEL,0,buf);
          p=sendMessage(SCI_GETCURRENTPOS, 0,0);
        } else {
          sendString(SCI_INSERTTEXT,p,buf);
          p+=n;
        }
      } else {
        sendString(SCI_APPENDTEXT,n,buf);
      }
    } while (!fh.eof());
    fh.close();
    if (rv) {
      if (insert) {
        GoToPos(p);
        sendMessage(SCI_CONVERTEOLS,sendMessage(SCI_GETEOLMODE,0,0),0);
        sendMessage(SCI_ENDUNDOACTION,0,0);
      } else {
        _filename=FXPath::absolute(filename);
        _filetime=FXStat::modified(_filename);
        _dirty=false;
        need_backup=false;
        if (codec) {
          const char *orig=(const char*)sendMessage(SCI_GETCHARACTERPOINTER,0,0);
          FXString recode;
          recode.length(codec->mb2utflen(orig,sendMessage(SCI_GETLENGTH,0,0)));
          codec->mb2utf((char*)recode.text(),recode.length(),orig,sendMessage(SCI_GETLENGTH,0,0));
          delete codec;
          SetUTF8(true);
          sendString(SCI_SETTEXT,0,recode.text());
        }
        SetEolModeFromContent();
        sendMessage(SCI_EMPTYUNDOBUFFER,0,0);
      }
      AdjustHScroll();
    }
    if (ro) { sendMessage(SCI_SETREADONLY,1,0); }
  } else {
    _lasterror=SystemErrorStr();
    rv=false;
  }
  _loading=false;
  return rv;
}