Complete the luidgen tool

calling 'luidgen' with a list of filenames replaces the word LUIDGEN in
each of this files with a octal escaped string consisting a unique
identifier

add luid formatter help macros to luid.h

let compilation of files which contain the word 'LUIDGEN' fail with a
hopefully self-explaining error message
This commit is contained in:
Christian Thaeter 2008-10-26 03:11:02 +01:00
parent 1886f41bf5
commit 194e4b9fec
3 changed files with 96 additions and 7 deletions

View file

@ -48,10 +48,26 @@ typedef lumiera_uid* LumieraUid;
#define LUMIERA_UID_INITIALIZER(l) l
#endif
#define LUMIERA_UID_FMT \
"\\%.3hho\\%.3hho\\%.3hho\\%.3hho" \
"\\%.3hho\\%.3hho\\%.3hho\\%.3hho" \
"\\%.3hho\\%.3hho\\%.3hho\\%.3hho" \
"\\%.3hho\\%.3hho\\%.3hho\\%.3hho"
#define LUMIERA_UID_CHAR(l,n) ((unsigned char*)l)[n]
#define LUMIERA_UID_ELEMENTS(l) \
LUMIERA_UID_CHAR(l,0), LUMIERA_UID_CHAR(l,1), LUMIERA_UID_CHAR(l,2), LUMIERA_UID_CHAR(l,3), \
LUMIERA_UID_CHAR(l,4), LUMIERA_UID_CHAR(l,5), LUMIERA_UID_CHAR(l,6), LUMIERA_UID_CHAR(l,7), \
LUMIERA_UID_CHAR(l,8), LUMIERA_UID_CHAR(l,9), LUMIERA_UID_CHAR(l,10), LUMIERA_UID_CHAR(l,11), \
LUMIERA_UID_CHAR(l,12), LUMIERA_UID_CHAR(l,13), LUMIERA_UID_CHAR(l,14), LUMIERA_UID_CHAR(l,15)
/**
* LUIDGEN will be replaced by the 'luidgen' tool with a random uuid
*/
#define LUIDGEN "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
#define LUIDGEN PLEASE_RUN_THE_LUIDGEN_TOOL_ON_THIS_FILE
/**
* Retrieve a generic pointer stored in a luid

View file

@ -24,4 +24,4 @@ luidgen_CPPFLAGS = -I$(top_srcdir)/src/
luidgen_SOURCES = \
$(lumitool_srcdir)/luidgen.c
luidgen_LDADD = liblumiera.a
luidgen_LDADD = liblumiera.a $(NOBUGMT_LUMIERA_LIBS)

View file

@ -19,25 +19,98 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "lib/safeclib.h"
#include "lib/luid.h"
#include <unistd.h>
#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <nobug.h>
/**
* @file
* Generate amd print a Lumiera uid as octal escaped string
* or process a file replaceing 'LUIDGEN' with a octal escaped string
*/
int
main (int argc, char** argv)
{
NOBUG_INIT;
lumiera_uid luid;
lumiera_uid_gen (&luid);
printf ("\"");
for (int i = 0; i < 16; ++i)
printf ("\\%.3hho", *(((char*)&luid)+i));
printf ("\"\n");
if (argc == 1)
{
lumiera_uid_gen (&luid);
printf ("\"");
for (int i = 0; i < 16; ++i)
printf ("\\%.3hho", *(((char*)&luid)+i));
printf ("\"\n");
}
else
{
for (int i = 1; i < argc; ++i)
{
FILE* in = fopen (argv[i], "r");
if (!in)
{
fprintf (stderr, "Failed to open file %s for reading: %s\n", argv[i], strerror (errno));
continue;
}
char* outname = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s.luidgen", argv[i]);
FILE* out = fopen (outname, "wx");
if (!out)
{
fprintf (stderr, "Failed to open file %s for writing: %s\n", outname, strerror (errno));
fclose (in);
continue;
}
char buf[4096];
char luidbuf[67];
printf ("Luidgen %s ", argv[i]); fflush (stdout);
while (fgets (buf, 4096, in))
{
char* pos;
while ((pos = strstr(buf, "LUIDGEN")))
{
memmove (pos+66, pos+7, strlen (pos+7)+1);
lumiera_uid_gen (&luid);
sprintf (luidbuf, "\""LUMIERA_UID_FMT"\"", LUMIERA_UID_ELEMENTS(luid));
memcpy (pos, luidbuf, 66);
putchar ('.'); fflush (stdout);
}
fputs (buf, out);
}
fclose (out);
fclose (in);
char* backup = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s~", argv[i]);
unlink (backup);
if (!!rename (argv[i], backup))
{
fprintf (stderr, "Failed to create backupfile %s: %s\n", backup, strerror (errno));
continue;
}
if (!!rename (outname, argv[i]))
{
fprintf (stderr, "Renaming %s to %s failed: %s\n", outname, argv[i], strerror (errno));
rename (backup, argv[i]);
continue;
}
printf (" done\n");
}
}
return 0;
}