Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <assert.h>
- #define EXPECT_EXT "bmp"
- #define WIDTH_OFFSET 0x12
- #define HEIGHT_OFFSET 0x16
- void checkArgs(int argc, char *argv[]) {
- if (argc == 1) {
- fprintf(stderr, "one more arg needed!\n");
- exit(-1);
- }
- char *ext = strrchr(argv[1], '.') + 1;
- if (strcmp(ext, EXPECT_EXT) != 0) {
- fprintf(stderr, "wrong extension, expected: %s, found: %s\n", EXPECT_EXT, ext);
- exit(-1);
- }
- }
- FILE *safeOpenFile(const char path[], const char mode[]) {
- FILE *file = NULL;
- if ((file = fopen(path, mode)) == NULL) {
- perror("");
- exit(EXIT_FAILURE);
- }
- return file;
- }
- void safeCloseFile(FILE *file) {
- if (fclose(file) == EOF) {
- perror("");
- exit(EXIT_FAILURE);
- }
- }
- uint32_t read(FILE *file, long offset) {
- fseek(file, offset, SEEK_SET);
- uint32_t data;
- assert(fread(&data, sizeof(uint32_t), 1, file) == 1);
- return data;
- }
- int main(int argc, char *argv[]) {
- checkArgs(argc, argv);
- FILE *file = safeOpenFile(argv[1], "rb");
- int width = read(file, WIDTH_OFFSET);
- int height = read(file, HEIGHT_OFFSET);
- printf("dimension: %u X %u\n", width, height);
- safeCloseFile(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement