Differences From Artifact [3b6f189d5603825c]:
- File        
src/bga_melter.d
- 2015-04-21 10:46:55 - part of checkin [4e2933c620] on branch trunk - Rebased to the latest verson dmd 2.067. Not yet verified the outcome. (user: kinaba) [annotate]
 
To Artifact [cf24a8abe2b880fc]:
- File        
src/bga_melter.d
- 2015-04-30 23:29:51 - part of checkin [ab3b073ef6] on branch trunk - private import => import (since in the latest D it is the default.) (user: kinaba) [annotate]
 
    1  private import win32.windows;                                                   |     1  import win32.windows;
    2  private import std.string;                                                       |     2  import std.c.stdio;
    3  private import std.file;                                                         |     3  import std.file;
    4  private import std.c.stdio;                                                      <
    5  private import std.string;                                                       |     4  import std.string;
    6  private import etc.c.zlib;                                                       |     5  import etc.c.zlib;
    7  private import libbz2.bzlib;                                                     |     6  import libbz2.bzlib;
    8  private import util;                                                             |     7  import util;
    9                                                                                         8  
   10  //----------------------------------------------------------------                     9  //----------------------------------------------------------------
   11  // Bga書庫のファイルヘッダ形式                                                                    10  // Bga書庫のファイルヘッダ形式
   12  //----------------------------------------------------------------                    11  //----------------------------------------------------------------
   13                                                                                        12  
   14  align(1) struct BgaHeader                                                             13  align(1) struct BgaHeader
   15  {                                                                                     14  {
................................................................................................................................................................................
   93  class Filep                                                                           92  class Filep
   94  {                                                                                     93  {
   95          private FILE* fp;                                                             94          private FILE* fp;
   96          private this( FILE* p ) { fp = p; }                                           95          private this( FILE* p ) { fp = p; }
   97                                                                                        96  
   98          static Filep open( string filename, bool read )                               97          static Filep open( string filename, bool read )
   99          {                                                                             98          {
  100                  FILE* fp = fopen( toStringz(filename), read?"rb":"wb" );         |    99                  FILE* fp = fopen(filename.toStringz(), read?"rb":"wb");
  101                  return (fp ? new Filep(fp) : null);                                  100                  return (fp ? new Filep(fp) : null);
  102          }                                                                            101          }
  103                                                                                       102  
  104          int dup_han()                                                                103          int dup_han()
  105          {                                                                            104          {
  106                  int fd = dup( fileno(fp) );                                          105                  int fd = dup( fileno(fp) );
  107                  lseek( fd, cur(), 0 );                                               106                  lseek( fd, cur(), 0 );
................................................................................................................................................................................
  153  //----------------------------------------------------------------                   152  //----------------------------------------------------------------
  154  // メインクラス                                                                            153  // メインクラス
  155  //----------------------------------------------------------------                   154  //----------------------------------------------------------------
  156                                                                                       155  
  157  class BgaMelter                                                                      156  class BgaMelter
  158  {                                                                                    157  {
  159          alias BgaAnswer delegate(ref BgaHeader) FileHandler;                         158          alias BgaAnswer delegate(ref BgaHeader) FileHandler;
  160          alias BgaAnswer delegate(int, int)        ProgressHandler;               |   159          alias BgaAnswer delegate(int, int)      ProgressHandler;
  161                                                                                       160  
  162          private Filep fp = null;                                                     161          private Filep fp = null;
  163                                                                                       162  
  164          this( string arc_name )                                                      163          this( string arc_name )
  165          {                                                                            164          {
  166                  fp = Filep.open( arc_name, true );                                   165                  fp = Filep.open( arc_name, true );
  167                  if( fp is null )                                                     166                  if( fp is null )
................................................................................................................................................................................
  213                                                  bContinue = GzDec( hdr.compresse     212                                                  bContinue = GzDec( hdr.compresse
  214                                          else if( hdr.method == "BZ2\0" )             213                                          else if( hdr.method == "BZ2\0" )
  215                                                  bContinue = BzDec( hdr.original_     214                                                  bContinue = BzDec( hdr.original_
  216                                                                                       215  
  217                                          // 閉じて属性設定                                   216                                          // 閉じて属性設定
  218                                          outf.close();                                217                                          outf.close();
  219                                          dosSetFTime( hdr.fname, hdr.date, hdr.ti     218                                          dosSetFTime( hdr.fname, hdr.date, hdr.ti
  220                                          SetFileAttributesA( toStringz(hdr.fname) |   219                                          SetFileAttributesA( hdr.fname.toStringz(
  221                                          if( !bContinue )                             220                                          if( !bContinue )
  222                                                  return;                              221                                                  return;
  223                                  }                                                    222                                  }
  224                                  finally { fp.seek_to(nextpos); }                     223                                  finally { fp.seek_to(nextpos); }
  225                          }                                                            224                          }
  226                  }                                                                    225                  }
  227                  finally { close(); }                                                 226                  finally { close(); }
................................................................................................................................................................................
  284                  if( hdr.arc_type==2 )                                                283                  if( hdr.arc_type==2 )
  285                          return false;                                                284                          return false;
  286                  if( hdr.arc_type==0 && hdr.compressed_size==hdr.original_size )      285                  if( hdr.arc_type==0 && hdr.compressed_size==hdr.original_size )
  287                  {                                                                    286                  {
  288                          int x = hdr.fname.lastIndexOf('.');                          287                          int x = hdr.fname.lastIndexOf('.');
  289                          if( x == -1 )                                                288                          if( x == -1 )
  290                                  return true;                                         289                                  return true;
  291                          string ext = toLower(hdr.fname[x+1 .. $]);               |   290                          string ext = hdr.fname[x+1 .. $].toLower();
  292                          if( ext=="arc" || ext=="arj" || ext=="bz2" || ext=="bza"     291                          if( ext=="arc" || ext=="arj" || ext=="bz2" || ext=="bza"
  293                           || ext=="cab" || ext=="gz"  || ext=="gza" || ext=="lzh"     292                           || ext=="cab" || ext=="gz"  || ext=="gza" || ext=="lzh"
  294                           || ext=="lzs" || ext=="pak" || ext=="rar" || ext=="taz"     293                           || ext=="lzs" || ext=="pak" || ext=="rar" || ext=="taz"
  295                           || ext=="tbz" || ext=="tgz" || ext=="z"   || ext=="zip"     294                           || ext=="tbz" || ext=="tgz" || ext=="z"   || ext=="zip"
  296                           || ext=="zoo" )                                             295                           || ext=="zoo" )
  297                                  return false;                                        296                                  return false;
  298                  }                                                                    297                  }
  299                  return true;                                                         298                  return true;
  300          }                                                                            299          }
  301                                                                                       300  
  302          static string pathMake( string path )                                        301          static string pathMake( string path )
  303          {                                                                            302          {
  304                  char* ps = cast(char*)toStringz(path);                           |   303                  char* ps = cast(char*)path.toStringz();
  305                  for(char* p=ps;;)                                                |   304                  for(char* p=ps;; ++p)
  306                  {                                                                    305                  {
  307                          for(; *p!=0&&*p!='\\'&&*p!='/'; p=CharNextA(p)) {}           306                          for(; *p!=0&&*p!='\\'&&*p!='/'; p=CharNextA(p)) {}
  308                          if( *p==0 )                                                  307                          if( *p==0 )
  309                                  break;                                               308                                  break;
  310                          CreateDirectoryA( toStringz(ps[0..(p-ps)]), null );      |   309                          CreateDirectoryA( ps[0..(p-ps)].toStringz(), null );
  311                          ++p;                                                     <
  312                  }                                                                    310                  }
  313                  return path;                                                         311                  return path;
  314          }                                                                            312          }
  315                                                                                       313  
  316          enum { BUFSIZ = 65536 }                                                  |   314          enum BUFSIZ = 65536;
  317                                                                                       315  
  318          private bool NcDec( uint usiz, Filep outf, ProgressHandler ph )              316          private bool NcDec( uint usiz, Filep outf, ProgressHandler ph )
  319          {                                                                            317          {
  320                  uint init_usiz = usiz;                                               318                  uint init_usiz = usiz;
  321                                                                                       319  
  322                  // 非圧縮。コピーするだけ                                                       320                  // 非圧縮。コピーするだけ
  323                  while( usiz )                                                        321                  while( usiz )