Guest User

filehandle.c

a guest
Jun 2nd, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. #define BUFFSIZE 2048
  7.  
  8. typedef struct file_data {
  9.     uint8_t *data;
  10.     size_t length;
  11. }file_data;
  12.  
  13.  
  14. struct file_data *read_file(const char *path){
  15.  
  16.         FILE *handler = NULL;
  17.         struct file_data *fdata = NULL;
  18.         size_t read_length = 0;
  19.         uint8_t buffer[BUFFSIZE] = {0x00};
  20.  
  21.         fdata = malloc(sizeof(struct file_data));
  22.         if(fdata == NULL)
  23.             return NULL;
  24.  
  25.         bzero(fdata, sizeof(struct file_data));
  26.  
  27.         fdata->data = malloc(1);
  28.         if(fdata->data == NULL){
  29.             free(fdata);
  30.             return NULL;
  31.         }
  32.  
  33.         handler = fopen(path,"rb");
  34.         if(handler == NULL){
  35.             free(fdata->data);
  36.             free(fdata);
  37.             return NULL;
  38.         }
  39.  
  40.         while(!feof(handler)){
  41.             read_length = fread(buffer,1,BUFFSIZE,handler);
  42.             fdata->data = realloc(fdata->data,fdata->length + read_length);
  43.             if(fdata->data == NULL){
  44.                 fclose(handler);
  45.                 free(fdata);
  46.                 return NULL;
  47.             }
  48.             memcpy(fdata->data +fdata->length, buffer, read_length);
  49.             fdata->length += read_length;
  50.         }
  51.  
  52.         fclose(handler);
  53.  
  54.         return fdata;
  55. }
  56.  
  57.  
  58. // open the requested file and return the file pointer
  59. FILE* requestfile(char *filename){
  60.     FILE *fp;
  61.     fp = fopen(filename,"r");
  62.     if(fp != NULL){
  63.         return fp;
  64.     }
  65.     else{
  66.         perror("Error opening file\n");
  67.     }
  68. }
  69.  
  70. // count the byte size of file
  71. int count_file_bytes(FILE *fp)
  72. {
  73.     int c,count = 0;
  74.     if(fp != NULL){
  75.         while((c = fgetc(fp)) != EOF)
  76.             count++;
  77.     }
  78.     fclose(fp);
  79.     return count;
  80. }
  81.  
  82. // read the file content to buffer and return
  83. char* content(char *filename)
  84. {
  85.     FILE *fp = requestfile(filename);
  86.     int bytecount = count_file_bytes(fp);
  87.     char *content = (char *) malloc(bytecount+1);
  88.  
  89.     FILE *file = requestfile(filename);
  90.     size_t i;
  91.  
  92.     for(i=0;i<(bytecount+1);++i)
  93.     {
  94.         int c = fgetc(file);
  95.         if( c == EOF)
  96.         {
  97.             break;
  98.         }
  99.  
  100.         content[i]= c;
  101.     }
  102.  
  103.     content[i] = '\0';
  104.     fclose(file);
  105.     return content;
  106.  
  107.  
  108.  
  109. }
  110.  
  111. // get file extension before last '.'
  112. char *get_file_ext(char *filename, int c)
  113. {
  114.     char *type = strrchr(filename,c);
  115.     return type;
  116. }
  117.  
  118.  
  119. // read the file in binary format
  120. char *read_binary_file(char *filename)
  121. {
  122.     FILE *fp = fopen(filename,"rb");
  123.     if(fp == NULL)
  124.         fprintf(stderr,"\t Error Opening file: %s\n",filename);
  125.     long image_size;
  126.     int c;
  127.     int count = 0;
  128.     fseek(fp,0,SEEK_END);
  129.     image_size = ftell(fp);
  130.     rewind(fp);
  131.     char *buffer = (char *)malloc(image_size);
  132.  
  133.     fread(buffer,1,image_size,fp);
  134.  
  135.     fclose(fp);
  136.     return buffer;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment