Liste des Groupes | Revenir à cl c |
On 01/06/2024 01:53, Malcolm McLean wrote:The Baby X resource compiler can of course do the trivial job of loading binary data and writing it out as a C array of unsigned char. If that was all it did, it would hardly be worth having. But is is an essential facility for the program that it is.On 31/05/2024 13:55, bart wrote:Sorry, I don't understand what that does; what is the input and what is the output?On 30/05/2024 16:03, Michael S wrote:Here's my solution. It's a bit more complicated.On Thu, 30 May 2024 15:48:39 +0100I gave myself the simple task of incorporating the source text of hello.c into a program, and printing it out.
bart <bc@freeuk.com> wrote:
>>>
Where do the _binary_logo_bmp_start and ...-size symbols come from?
That is, how do they get into the object file.
>
objcopy generates names of the symbols from the name of input binary
file. I would think that it is possible to change these symbols to
something else, but I am not sure that it is possible withing the same
invocation of objcopy. It certainly is possible with a second pass.
Lawrence probably can give more authoritative answer.
Or as a last resort you can RTFM.
>
>
Here's how builtin embedding worked using a feature of my older C compiler:
>
#include <stdio.h>
#include <string.h>
>
char hello[] = strinclude("hello.c");
>
int main(void) {
printf("hello =\n%s\n", hello);
printf("strlen(hello) = %zu\n", strlen(hello));
printf("sizeof(hello) = %zu\n", sizeof(hello));
}
>
>
I build it and run it like this:
>
C:\c>bcc c
Compiling c.c to c.exe
>
C:\c>c
hello =
#include "stdio.h"
>
int main(void) {
printf("Hello, World!\n");
}
>
strlen(hello) = 70
sizeof(hello) = 71
>
C:\c>dir hello.c
31/05/2024 13:48 70 hello.c
>
>
It just works; no messing about with objcopy parameters; no long unwieldy names; no link errors due to unsupported file formats; no problems with missing terminators for embedded text files imported as strings; no funny ways of getting size info.
>
>
>
int bbx_write_source (const char *source_xml, char *path, const char *source_xml_file, const char *source_xml_name)
{
XMLDOC *doc = 0;
char error[1024];
char buff[1024];
XMLNODE *root;
XMLNODE *node;
const char *name;
FILE *fpout;
FILE *fpin;
int ch;
>
doc = xmldocfromstring(source_xml, error, 1024);
if (!doc)
{
fprintf(stderr, "%s\n", error);
return -1;
}
root = xml_getroot(doc);
if (strcmp(xml_gettag(root), "FileSystem"))
return -1;
>
if (!root->child)
return -1;
if (strcmp(xml_gettag(root->child), "directory"))
return -1;
>
for (node = root->child->child; node != NULL; node = node->next)
{
if (!strcmp(xml_gettag(node), "file"))
{
name = xml_getattribute(node, "name");
snprintf(buff, 1024, "%s%s", path, name);
fpout = fopen(buff, "w");
if (!fpout)
break;
fpin = file_fopen(node);
if (!fpin)
break;
if (!strcmp(name, source_xml_file))
{
char *escaped = texttostring(source_xml);
if (!escaped)
break;
fprintf(fpout, "char %s[] = %s;\n", source_xml_name, escaped);
free(escaped);
}
else
{
while ((ch = fgetc(fpin)) != EOF)
fputc(ch, fpout);
}
fclose(fpout);
fclose(fpin);
fpout = 0;
fpin = 0;
}
}
if (fpin || fpout)
{
fclose(fpin);
fclose(fpout);
return -1;
}
>
return 0;
>
}
>
It's leveraging the Baby X resource compiler, the xmparser, and my filesystem programs. You can't include the source of a program in the program as a C string, because then the source changes to include that string. So what you do is this.
>
You first place a placeholder C source file containing a short dummy string.
The you convert the source to an XML file, and turn it into a string with the Baby X Resource compiler. Then you drop the source into the
file, removing the placeholder.
>
Then the program walks the file list, detects that file, and replaces it with the xml string it has been passed.
>
And this system works, and it's an easy way of adding source output to
ptograms. Of course the function now needs to be modified to walk the entire tree recursively and I will need a makedirectory function. I've got it to work for flat source directories.
In the case of a very simple requirement of incorporating a text file into a C program as data, usually string data (which I have to say is much more common for me than doing anything with XML), how would a BBX solution work?
This doesn't work:
char strdata[] = {
#include "file.txt"
}
Because the contents of file.txt, which let's say are:
one
two
three
are interpreted as C source code ('one' is a syntax error, or it might be the name of some identifier).
Some process is needed to either turn that file into:
"one\ntwo\nthree\n"
or into a bunch of numbers: '100, 110, 101, ...'. I think this is what 'xxd' does.
In the case of binary files, the process of embedding is usually blind to the actual format, or meaning, of the file. It is just a blob of data.
So here, I understand that the BBXRC solution goes much further. If I wanted to include a JPG file, then either #embed or my strinclude() would just incorporate the raw bytes. I would still need a JPEG decoder to use that data.
Whereas BBXRC, AIUI, does the decoding for you, and incorporates the data as a raw table of pixel values that can be directly used.
So it is at a different level from what is being discussed. But sometimes there is also a need for that cruder form of embedding: maybe that JPG just needs to be written out again; no need to get inside it.
Les messages affichés proviennent d'usenet.