[37] | 1 | #include <stdio.h> |
---|
| 2 | #include <ply.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <strings.h> |
---|
| 5 | |
---|
| 6 | #ifdef linux |
---|
| 7 | #include <string.h> |
---|
| 8 | #endif |
---|
| 9 | |
---|
| 10 | #define MAX_HEADER_LENGTH 10000 |
---|
| 11 | |
---|
| 12 | void usage(char *progname); |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | int |
---|
| 16 | main(int argc, char **argv) |
---|
| 17 | { |
---|
| 18 | int nelems; |
---|
| 19 | char **elist; |
---|
| 20 | int file_type; |
---|
| 21 | float version; |
---|
| 22 | char header[MAX_HEADER_LENGTH]; |
---|
| 23 | PlyFile *ply; |
---|
| 24 | char *end; |
---|
| 25 | char *progname; |
---|
| 26 | char *inName = NULL; |
---|
| 27 | FILE *inFile = stdin; |
---|
| 28 | |
---|
| 29 | progname = argv[0]; |
---|
| 30 | argc--; |
---|
| 31 | argv++; |
---|
| 32 | |
---|
| 33 | /* Print usage? */ |
---|
| 34 | if (argc > 0 && !strcmp(argv[0], "-h")) { |
---|
| 35 | usage(progname); |
---|
| 36 | exit(-1); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | /* optional input file (if not, read stdin ) */ |
---|
| 40 | if (argc > 0 && *argv[0] != '-') { |
---|
| 41 | inName = argv[0]; |
---|
| 42 | inFile = fopen(inName, "r"); |
---|
| 43 | if (inFile == NULL) { |
---|
| 44 | fprintf(stderr, "Error: Couldn't open input file %s\n", inName); |
---|
| 45 | usage(progname); |
---|
| 46 | exit(-1); |
---|
| 47 | } |
---|
| 48 | argc --; |
---|
| 49 | argv ++; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /* Check no extra args */ |
---|
| 53 | if (argc > 0) { |
---|
| 54 | fprintf(stderr, "Error: Unhandled arg: %s\n", argv[0]); |
---|
| 55 | usage(progname); |
---|
| 56 | exit(-1); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /* If it's a file, open it twice (first time checks it's a valid |
---|
| 60 | * ply file). If it's on stdin, just assume valid ply file, and |
---|
| 61 | * get the header in a single pass. |
---|
| 62 | */ |
---|
| 63 | if (inFile != stdin) { |
---|
| 64 | ply = ply_read(inFile, &nelems, &elist); |
---|
| 65 | |
---|
| 66 | if (!ply) { |
---|
| 67 | fprintf(stderr, "Not a Ply file.\n"); |
---|
| 68 | exit(1); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | ply_close(ply); |
---|
| 72 | |
---|
| 73 | inFile = fopen(inName, "r"); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | fread(header, 1, MAX_HEADER_LENGTH, inFile); |
---|
| 77 | header[MAX_HEADER_LENGTH-1] = 0; |
---|
| 78 | |
---|
| 79 | end = strstr(header, "end_header"); |
---|
| 80 | |
---|
| 81 | if (end == NULL) { |
---|
| 82 | fprintf(stderr, "Did not find end of header within the first "); |
---|
| 83 | fprintf(stderr, "%d bytes of the file.\n", MAX_HEADER_LENGTH); |
---|
| 84 | exit(1); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | *(end+strlen("end_header")+1) = 0; |
---|
| 88 | |
---|
| 89 | printf("\n%s\n", header); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | void |
---|
| 94 | usage(char *progname) |
---|
| 95 | { |
---|
| 96 | fprintf(stderr, "\n"); |
---|
| 97 | fprintf(stderr, "Usage: %s [ply-file]\n", progname); |
---|
| 98 | fprintf(stderr, " or: %s < ply-file\n", progname); |
---|
| 99 | fprintf(stderr, "\n"); |
---|
| 100 | fprintf(stderr, " %s prints the header information from a Ply file.\n\n", |
---|
| 101 | progname); |
---|
| 102 | fprintf(stderr, "Note: When reading on stdin, it makes the assumption\n"); |
---|
| 103 | fprintf(stderr, "that it is a valid ply file. If you're not sure,\n"); |
---|
| 104 | fprintf(stderr, "pass the filename as an argument.\n"); |
---|
| 105 | |
---|
| 106 | exit(-1); |
---|
| 107 | } |
---|
| 108 | |
---|