modified: SpatialOmicsCoord.py
[GalaxyCodeBases.git] / c_cpp / etc / tiny / combine.c
blob6f4e029487b0870cd903e8420aaf8b8b7099a7f4
1 #include <stdlib.h> //EXIT_FAILURE
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/mman.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
10 #define handle_error(msg) \
11 do { perror(msg); exit(EXIT_FAILURE); } while (0)
13 #define EXTLEN 32
14 // 8 should be enough
17 [HTTP/1.1 404 File Not Found
18 Content-Type: text/octet
19 User-ReturnCode: -29104
20 Content-Length: 16
22 «Í˜vÿÿŽP]
24 Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F
26 00100000 48 54 54 50 2F 31 2E 31 20 34 30 34 20 46 69 6C HTTP/1.1 404 Fil
27 00100010 65 20 4E 6F 74 20 46 6F 75 6E 64 0D 0A 43 6F 6E e Not Found Con
28 00100020 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F tent-Type: text/
29 00100030 6F 63 74 65 74 0D 0A 55 73 65 72 2D 52 65 74 75 octet User-Retu
30 00100040 72 6E 43 6F 64 65 3A 20 2D 32 39 31 30 34 0D 0A rnCode: -29104
31 00100050 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 Content-Length:
32 00100060 31 36 0D 0A 0D 0A AB CD 98 76 FF FF 8E 50 16 «Í˜vÿÿŽP
34 char theStr[]="HTTP/1.1 404 File Not Found\r\nContent-Type: text/octet\r\nUser-ReturnCode: -29104\r\nContent-Length: 16\r\n\r\n\xAB\xCD\x98\x76\xFF\xFF\x8E\x50";
35 int theStrLen = sizeof(theStr) - 1;
37 int main (int argc, char *argv[]) {
38 printf("Searching for:\n[%s] %d %zd\n",theStr,theStrLen,strlen(theStr));
39 if (argc!=4) {
40 fprintf(stderr,"Usage: %s <main file> <2nd file> <out file>\n",argv[0]);
41 exit(EXIT_FAILURE);
43 int in1, in2;
44 FILE *outf;
45 struct stat sb1, sb2;
46 char *p1, *p2;
47 in1 = open(argv[1], O_RDONLY);
48 if (in1 == -1)
49 handle_error("open1");
50 in2 = open(argv[2], O_RDONLY);
51 if (in2 == -1)
52 handle_error("open2");
54 if (fstat(in1, &sb1) == -1) /* To obtain file size */
55 handle_error("fstat1");
56 if (fstat(in2, &sb2) == -1) /* To obtain file size */
57 handle_error("fstat2");
58 if (sb1.st_size != sb2.st_size) {
59 fprintf(stderr,"Input files with different size: %zd,%zd .\n",sb1.st_size,sb2.st_size);
60 exit(EXIT_FAILURE);
62 if ( !(sb1.st_size && sb2.st_size) ) {
63 fprintf(stderr,"Input files are empty.\n");
64 exit(EXIT_FAILURE);
67 p1 = mmap(NULL, sb1.st_size, PROT_READ, MAP_PRIVATE, in1, 0);
68 if (p1 == MAP_FAILED)
69 handle_error("mmap1");
70 p2 = mmap(NULL, sb2.st_size, PROT_READ, MAP_PRIVATE, in2, 0);
71 if (p2 == MAP_FAILED)
72 handle_error("mmap2");
74 outf = fopen(argv[3],"w+x");
75 if (outf == NULL)
76 handle_error("open3");
78 char *thisp, *lastp;
79 thisp = p1;
80 while ( thisp - p1 < sb1.st_size ) {
81 if (*thisp == 'H') {
82 if ( memcmp(thisp,theStr,theStrLen) == 0 ) {
83 printf("1 %zd\t(%zx)\t",thisp-p1,thisp-p1);
84 char *p2thisp = thisp - p1 + p2;
85 if ( memcmp(p2thisp,theStr,theStrLen) == 0 ) {
86 printf("Also 2 ");
88 if ( fwrite(p2thisp,theStrLen,1,outf) < 1 ) {
89 handle_error("write");
91 thisp += theStrLen;
92 p2thisp += theStrLen;
93 lastp = thisp-1;
94 char *extsp = thisp + EXTLEN;
95 while ( (*thisp != *p2thisp) || (thisp < extsp) ) {
96 fputc(*p2thisp,outf);
97 if (*thisp != *p2thisp)
98 printf("%zd:%02hhX-%02hhX ", thisp-lastp, *thisp, *p2thisp);
99 ++thisp;
100 ++p2thisp;
102 putchar('\n');
103 } else { fputc(*thisp,outf);++thisp; }
104 } else { fputc(*thisp,outf);++thisp; }
107 fclose(outf);
108 exit(EXIT_SUCCESS);