Last active
March 5, 2021 08:28
-
-
Save daschr/1bc9c474d1de6a3eaf09ab80cba62eb8 to your computer and use it in GitHub Desktop.
convert stream from https://github.com/easytarget/esp32-cam-webserver to images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) <2021> <David Schramm <[email protected]>> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
// cc -Wall -Werror -pedantic -O3 -o strip strip.c | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define KEY "Content-Length: " | |
#define NAMEBUF_L 512 | |
int parse_stream(const char *fname, const char *prepend); | |
void strip(FILE *f, const char *out_n, size_t length); | |
int main(int ac, char *as[]) { | |
if(ac<2) { | |
fprintf(stderr, "Usage: %s [file] [?prepend]\n", as[0]); | |
return EXIT_FAILURE; | |
} | |
char *inp=strcmp(as[1], "-")==0?NULL:as[1]; | |
return parse_stream(inp, ac>2?as[2]:NULL)?EXIT_FAILURE:EXIT_SUCCESS; | |
} | |
inline int seek_to(FILE *f, const char *text, size_t length) { | |
char buf[length+1]; | |
if(fread(buf, sizeof(char), length, f)!=length) | |
return 0; | |
buf[length]=0; | |
for(int c;;) { | |
if(strcmp(buf, text)==0) | |
return 1; | |
if((c=fgetc(f))==-1) | |
return 0; | |
memcpy(buf, buf+1, length-1); | |
buf[length-1]=c; | |
buf[length]=0; | |
} | |
} | |
int parse_stream(const char *fname, const char *prepend) { | |
FILE *f=fname==NULL?stdin:fopen(fname,"r"); | |
if(f==NULL) { | |
fprintf(stderr, "could not open \"%s\"!\n", fname); | |
return 1; | |
} | |
const char *pre=prepend==NULL?"out-":prepend; | |
char namebuf[NAMEBUF_L]; | |
size_t clength, num=1, keyl=strlen(KEY); | |
while(!feof(f)) { | |
if(!seek_to(f, KEY, keyl)) | |
break; | |
if(fscanf(f, "%lu\r\n\r\n", &clength)==1) { | |
//printf("%lu\n", clength); | |
snprintf(namebuf, NAMEBUF_L-1, "%s%lu.jpg", pre, num++); | |
strip(f, namebuf, clength); | |
} | |
} | |
fclose(f); | |
return 0; | |
} | |
void strip(FILE *f, const char *out_n, size_t length) { | |
FILE *out; | |
if((out=fopen(out_n, "w"))==NULL) | |
return; | |
int c=0; | |
for(size_t i=0; i<length; ++i) { | |
if((c=fgetc(f))==-1) | |
break; | |
putc(c, out); | |
} | |
fclose(out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment