NO

Author Topic: split source file using tags  (Read 291 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1777
split source file using tags
« on: August 25, 2018, 06:31:36 pm »
SplitSource.c
Code: [Select]
/// @SplitSource.h
typedef struct FILE FILE;
typedef int size_t;
FILE * fopen(const char *name, const char *mode);
int fclose(FILE *stream);
char * fgets(char * restrict dst, int max, FILE * restrict stream);
size_t fread(void * restrict dst, size_t size, size_t num, FILE * restrict stream);
size_t fwrite(const void * restrict src, size_t size, size_t num, FILE * restrict stream);
size_t strlen(const char *string);
char * strcpy(char * restrict dst, const char * restrict src);
int strncmp(const char *string1, const char *string2, size_t num);
int puts(const char *string);
/// @ comment
/// @SplitSource.c
/// @@#include "SplitSource.h"
char buf[512], fname[260], hdr[512];

int __cdecl main(int argc, char **argv)
{
FILE *fi, *fo, *fh;
//char buf[512], fname[260], hdr[512];
if (argc < 2) return 1;
fi = fopen(argv[1], "r");
fo = 0;
*hdr = 0;
int hdrlen = 0;
if (argc == 3) {
fh = fopen(argv[2], "r");
fread(hdr, 512, 1, fh);
fclose(fh);
hdrlen = strlen(hdr);
}
strcpy(fname, "src1/");
if (fi) {
while (fgets(buf, 512, fi)) {
int len = strlen(buf);
if (!strncmp(buf, "/// @", 5)) { // source tag "/// @"
if (*(buf+5) == '@') { // "/// @@" add line to file
if (fo) fwrite(buf+6, len-6, 1, fo);
} else {
if (fo) {
fclose(fo);
fo = 0;
}
//if (*(buf+len-2) == 'c' || *(buf+len-2) == 'h') { // source name
if (*(short*)(buf+len-3) == *(short*)".c"
|| *(short*)(buf+len-3) == *(short*)".h") { // source name
strcpy(fname+5, buf+5); // copy possible filename
*(fname+len-1) = 0;
puts(fname);
fo = fopen(fname, "w");
if (!fo) break;
if (hdrlen)
fwrite(hdr, hdrlen, 1, fo);
}
}
}
if (fo) fwrite(buf, len, 1, fo);
}
fclose(fi);
}
return 0;
}
it use tags
/// @ for filename.c
/// @@ for additional text line like /// @@#include <commdlg.h> to add #include <commdlg.h>

usage SplitSource.exe Source.c Header.h
Header.h is added to every file for headers
Files are created to src1 folder.

example for fix case between files
Code: [Select]
/// @@#endif
/// @CopyList2File.c
/// @@#include <commdlg.h>
/// @@#ifndef NOLISTTOOLS
int CopyList2File(HWND hWnd, HWND hWndLV, int iMode)
EDIT split that renamed example ;)
« Last Edit: August 29, 2018, 03:29:18 pm by TimoVJL »
May the source be with you