Last active
January 26, 2016 13:55
-
-
Save akimasa/eea0e0550689d2de0ff9 to your computer and use it in GitHub Desktop.
http://technica-blog.jp/1159 の下にあるコードをC言語で高速に? 読み込んだファイルは頭出し後、STDOUTに出力
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
#define _LARGEFILE_SOURCE | |
#define _FILE_OFFSET_BITS 64 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#ifdef _WIN32 | |
#include <io.h> | |
#include <fcntl.h> | |
#endif | |
int main(int argc, char *argv[0]){ | |
#ifdef _WIN32 | |
setmode(fileno(stdout),O_BINARY); | |
setmode(fileno(stdin),O_BINARY); | |
#endif | |
FILE *fp; | |
unsigned char buf[188]; | |
int pid,pmt,fore_pid; | |
pmt = -1; | |
fore_pid = 0; | |
if((fp = fopen(argv[1], "rb")) == NULL) { | |
fprintf(stderr,"file open error"); | |
exit(EXIT_FAILURE); | |
} | |
//printf("%d,%d\n",(int)sizeof(buf[0]),(int)(sizeof(buf)/sizeof(buf[0]))); | |
while(fread(buf, sizeof(buf[0]),sizeof(buf)/sizeof(buf[0]),fp)){ | |
if(buf[0] != 0x47){ | |
fprintf(stderr,"not sync\n"); | |
fclose(fp); | |
exit(EXIT_FAILURE); | |
} | |
pid = (((buf[1] & 0x1f) << 8) | buf[2]); | |
if(pid == 0x00 && pmt == -1){ | |
pmt = (((buf[19] & 0x1f) << 8) | buf[20]); | |
} | |
if(pmt != 0 && pid == pmt && ((buf[1]&0x40) == 0x40)){ | |
int plen = ((buf[15]&0xf) * 16 * 16) + buf[16]; | |
int index = 17 + plen; | |
int currentpid; | |
if(index+19 > 188){ | |
fprintf(stderr,"index err\n"); | |
fclose(fp); | |
exit(EXIT_FAILURE); | |
} | |
if(buf[index] == 0x02){ | |
currentpid = (buf[index+1]<<8) | buf[index+2]; | |
} | |
if(fore_pid != 0 && fore_pid != currentpid){ | |
fprintf(stderr,"pid changed%d,%d\n",fore_pid,currentpid); | |
size_t rcount; | |
//unsigned char largebuf[1024*1024*1]; | |
int largebufsize = 1024*1024*1; | |
unsigned char *largebuf = (unsigned char *)malloc(largebufsize); | |
while((rcount = fread(largebuf, 1,largebufsize,fp))){ | |
fwrite(largebuf, 1, rcount,stdout); | |
} | |
fclose(fp); | |
exit(EXIT_SUCCESS); | |
} | |
fore_pid = currentpid; | |
} | |
} | |
printf("hoge\n"); | |
fclose(fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment