File: qembed.cpp

package info (click to toggle)
explorer 0.72-2.1
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 1,556 kB
  • ctags: 1,203
  • sloc: cpp: 10,226; makefile: 384; sh: 206; ansic: 106; lex: 95; csh: 6
file content (112 lines) | stat: -rw-r--r-- 2,779 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/****************************************************************************
** $Id: qembed.cpp,v 1.2 1997/04/17 03:20:53 jharris Exp $
**
** Utility program for embedding binary data into a C/C++ source code.
**
** Copyright (C) 1995 by Troll Tech AS.  Use, modification and
** distribution is allowed without limitation, warranty, or liability
** of any kind.
**
*****************************************************************************/

#include <qstring.h>
#include <qfile.h>
#include <qlist.h>
#include <qtstream.h>
#include <qdatetm.h>
#include <ctype.h>

void    embedData( const QByteArray &input, QFile *output );
QString convertFileNameToCIdentifier( const char * );

int main( int argc, char **argv )
{
    if ( argc < 4 ) {
	warning( "Usage:\n\t%s header source files", argv[0] );
	return 1;
    }

    QFile source( argv[2] ), header( argv[1] );
    source.open( IO_WriteOnly );
    header.open( IO_WriteOnly );

    QTextStream sourceStream( &source );
    QTextStream headerStream( &header );

    sourceStream << "#include \"" << argv[1] << "\"\n";

  // Embed data for all input files

    QString cname;
    
    for ( int i=3; i<argc; i++ ) {
	QFile f( argv[i] );
	if ( !f.open(IO_ReadOnly) ) {
	    warning( "Cannot open file %s, ignoring it", argv[i] );
	    continue;
	}
	QByteArray a( f.size() );
	if ( f.readBlock(a.data(), f.size()) != (int)f.size() ) {
	    warning( "Cannot read file %s, ignoring it", argv[i] );
	    f.close();
	    continue;
	}

	cname = convertFileNameToCIdentifier( argv[i] );

	headerStream << "extern const unsigned int " << cname << "_len;\n";
	headerStream << "extern const unsigned char " << cname << 
	    "_data[];\n\n";

	sourceStream << "const unsigned int " << cname << "_len = " <<
	    f.size() << ";\n";
	sourceStream << "const unsigned char " << cname << "_data[] = {";

	embedData( a, &source );
	sourceStream << "\n};\n\n";
	f.close();
    }

    return 0;
}


QString convertFileNameToCIdentifier( const char *s )
{
    QString r = s;
    int len = r.length();
    if ( len > 0 && !isalpha(r[0]) )
	r[0] = '_';
    for ( int i=1; i<len; i++ ) {
	if ( !isalnum(r[i]) )
	    r[i] = '_';
    }
    return r;
}


void embedData( const QByteArray &input, QFile *output )
{
    static char hexdigits[] = "0123456789abcdef";
    QString s( 100 );
    int nbytes = input.size();
    char *p = s.data();
    for ( int i=0; i<nbytes; i++ ) {
	if ( (i%14) == 0 ) {
	    strcpy( p, "\n    " );
	    output->writeBlock( s.data(), s.length() );
	    p = s.data();
	}
	int v = (int)((uchar)input[i]);
	*p++ = '0';
	*p++ = 'x';
	*p++ = hexdigits[(v >> 4) & 15];
	*p++ = hexdigits[v & 15];
	if ( i < nbytes-1 )
	    *p++ = ',';
    }
    if ( p > s.data() ) {
	*p = '\0';
	output->writeBlock( s.data(), s.length() );
    }
}