Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. //plainTextHandle.h
  2. #include <stdio.>
  3. #include <stdlib.>
  4.  
  5. #include <unistd.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8.  
  9. typedef struct {
  10.     unsigned int    textLen;    //Length of actual file, Excluding terminating \0
  11.     char*       textContent;    //Pointer to memory containing the actual text file
  12.     int     textIndexCount; //Number of lines
  13.     char**      textIndex;  //Pointer to list of pointers to the beginning of each null-terminated line
  14. } t_textFile;
  15.  
  16. t_textFile* t_textFile_C(FILE* stream);
  17. void t_textFile_D(t_textFile* target);
  18.  
  19. //plainTextHandle.c
  20. #include "plainTextHandle.h"
  21.  
  22. t_textFile* t_textFile_C(FILE* stream) {
  23.     t_textFile* newStruct = malloc(sizeof(*newStruct));
  24.    
  25.     //Determine File Length
  26.     struct stat statBuff;
  27.     if(fstat(fileno(stream), &statBuff)) { free(newStruct); return(0); }
  28.     newStruct->textLen = statBuff.st_size;
  29.        
  30.     //Buffer
  31.     newStruct->textContent = malloc(newStruct->textLen+1);
  32.    
  33.     //Load
  34.     if( newStruct->textLen != fread(newStruct->textContent, 1, newStruct->textLen, stream) ) {
  35.         //Some error occurred, end of file was not reached
  36.         if(DEBUG_MODE){printf("Failed to read file");}
  37.         free(newStruct->textContent);
  38.         free(newStruct);
  39.         return(0);
  40.     }
  41.  
  42.     //Null Terminate
  43.     newStruct->textContent[newStruct->textLen] = '\0';
  44.    
  45.     //Get count of lines
  46.     newStruct->textIndexCount = 1;
  47.     for(unsigned int i=1; i < newStruct->textLen; i++){
  48.         if(newStruct->textContent[i] == '\n') {
  49.             (newStruct->textIndexCount)++;
  50.         }
  51.     }
  52.    
  53.     //Buffer for line index
  54.     newStruct->textIndex = malloc(sizeof(newStruct->textIndex) * newStruct->textIndexCount);
  55.    
  56.     //Populate index, replace \n with \0 to null-terminate each line
  57.    
  58.     newStruct->textIndex[0] = newStruct->textContent; //First line
  59.     int j = 1; //Position in the index
  60.    
  61.     for(unsigned int i=1; i < newStruct->textLen; i++){
  62.         if(newStruct->textContent[i]    ==  '\n') {
  63.             newStruct->textContent[i] = '\0';
  64.             newStruct->textIndex[j] = newStruct->textContent+i+1;
  65.             j++;
  66.         }
  67.     }
  68.    
  69.     return(newStruct);
  70. }
  71.  
  72. void t_textFile_D(t_textFile* target) {
  73.     free(target->textIndex);
  74.     target->textIndex = 0;
  75.    
  76.     free(target->textContent);
  77.     target->textContent = 0;
  78.    
  79.     free(target);
  80.    
  81.     return;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement