File: sardec.cpp

package info (click to toggle)
onscripter 20161102-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 1,288 kB
  • ctags: 1,866
  • sloc: cpp: 31,303; makefile: 83; sh: 13
file content (97 lines) | stat: -rw-r--r-- 2,821 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* -*- C++ -*-
 * 
 *  sardec.cpp - SAR archive decoder
 *
 *  Copyright (c) 2001-2004 Ogapee. All rights reserved.
 *
 *  ogapee@aqua.dti2.ne.jp
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "SarReader.h"

extern int errno;

int main( int argc, char **argv )
{
    SarReader cSR;
    unsigned long length, buffer_length = 0;
    unsigned char *buffer = NULL;
    char file_name[256], dir_name[256];
    unsigned int i, j, count;
    FILE *fp;
    struct stat file_stat;

    if ( argc != 2 ){
        fprintf( stderr, "Usage: sardec arc_file\n");
        exit(-1);
    }
    if (cSR.open( argv[1] ) != 0){
        fprintf( stderr, "can't open file %s\n", argv[1] );
        exit(-1);
    }
    count = cSR.getNumFiles();

    SarReader::FileInfo sFI;
    
    for ( i=0 ; i<count ; i++ ){
        sFI = cSR.getFileByIndex( i );
        
        length = cSR.getFileLength( sFI.name );

        if ( length > buffer_length ){
            if ( buffer ) delete[] buffer;
            buffer = new unsigned char[length];
            buffer_length = length;
        }
        if ( cSR.getFile( sFI.name, buffer ) != length ){
            fprintf( stderr, "file %s can't be retrieved\n", sFI.name );
            continue;
        }
        
        strcpy( file_name, sFI.name );
        for ( j=0 ; j<strlen(file_name) ; j++ ){
            if ( file_name[j] == '\\' ){
                file_name[j] = '/';
                strncpy( dir_name, file_name, j );
                dir_name[j] = '\0';

                /* If the directory does'nt exist, create it */
                if ( stat ( dir_name, &file_stat ) == -1 && errno == ENOENT )
                    mkdir( dir_name, 00755 );
            }
        }
    
        printf("opening %s\n", file_name );
        if ( (fp = fopen( file_name, "wb" ) )){
            fwrite( buffer, 1, length, fp );
            fclose(fp);
        }
        else{
            printf(" ... falied\n");
        }
    }
    
    if ( buffer ) delete[] buffer;
    
    exit(0);
}