Sujet : Re: Baby X is bor nagain
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.cDate : 12. Jun 2024, 06:40:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4bcbj$1gqlo$1@raubtier-asyl.eternal-september.org>
References : 1 2 3
User-Agent : Mozilla Thunderbird
Am 11.06.2024 um 18:15 schrieb Malcolm McLean:
These are Baby programs. But they use a cut down GUI. So they need to get fonts and images into the program somehow. And so Baby X does that by converting to 32 bit C arrays which can be compiled and linked as normal. And for that, you need a tool. Writing a tiff file decoder is not a trivial exercise.
I converted my code into sth. that produces a C-string as an output.
Printing that is still very fast, i.e. the files produced are written
with about 2.6GiB/s. But the problem is still that all compilers don't
parse large files but quit with an out of memory error. So having a
.obj output along with a small header file would be the best.
That's my program called cdump:
#include <iostream>
#include <fstream>
#include <charconv>
#include <span>
#include <vector>
using namespace std;
int main( int argc, char **argv )
{
using u = unsigned char;
if( argc < 4 )
{
cout << "usage: " << argv[0] << " infile symbol outfile" << endl;
return EXIT_FAILURE;
}
char const
*inFile = argv[1],
*symbol = argv[2],
*outFile = argv[3];
ifstream ifs;
ifs.exceptions( ifstream::failbit | ifstream::badbit );
ifs.open( inFile, ifstream::binary | ifstream::ate );
streampos spSize( ifs.tellg() );
if( spSize > (size_t)-1 )
{
cout << "file too large" << endl;
return EXIT_FAILURE;
}
size_t size = (size_t)spSize;
ifs.seekg( ifstream::beg );
union ndi { u c; ndi() {} };
vector<ndi> rawBytes( size );
span<u> bytes( &rawBytes.data()->c, rawBytes.size() );
ifs.read( (char *)bytes.data(), bytes.size() );
ofstream ofs;
ofs.exceptions( ofstream::failbit | ofstream::badbit );
ofs.open( outFile, ofstream::binary | ofstream::trunc );
vector<ndi> rawBuf( 1ull << 20 );
span<u> buf( &rawBuf.begin()->c, rawBuf.size() );
ofs << "const unsigned char " << symbol << "[" << size + 1 << "] = \n";
auto rd = bytes.begin();
auto wrt = buf.begin();
auto flush = [&]
{
ofs.write( (char *)buf.data(), wrt - buf.begin() );
wrt = buf.begin();
};
#if defined(_WIN32)
constexpr bool CRLF = true;
#else
constexpr bool CRLF = false;
#endif
while( rd != bytes.end() ) [[likely]]
{
size_t
remaining = bytes.end() - rd,
n = remaining > 12 ? 12 : remaining;
auto rowEnd = rd + n;
*wrt++ = '\t';
*wrt++ = '"';
do
{
*wrt++ = '\';
*wrt++ = 'x';
auto toHex = []( u c ) -> u { return c + (c < 10 ? '0' : -10 + 'A'); };
*wrt++ = toHex( *rd >> 4 );
*wrt++ = toHex( *rd & 0xF );
} while( ++rd != bytes.end() && rd != rowEnd );
*wrt++ = '"';
if( rd == bytes.end() )
*wrt++ = ';';
if constexpr( CRLF )
*wrt++ = '\r';
*wrt++ = '\n';
if( buf.end() - wrt < 128 ) [[likely]]
flush();
}
flush();
}