Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6.  
  7. #define INITIALSIZE 1000 //initial size of the char array
  8.  
  9. /* A data structure is needed to store the content
  10.  * of the file and some other metadata about the
  11.  * input file.
  12.  */
  13. typedef struct _inputFileInfo
  14. {
  15.     int lineNumber;
  16.     char *lineData;
  17.     struct _inputFileInfo *next;
  18. } ds;
  19.  
  20. //Number of lines in a file
  21. int numOfLines = 0;
  22. ds *head = NULL;
  23. ds *tail = NULL;
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.     //Usage: ./textEditor <fileName>
  28.     if(argc != 2)
  29.     {
  30.         printf("Error: File name is not specified.\n");
  31.         printf("Usage: ./textEditor <textFileName>\n");
  32.         exit(1);
  33.     }
  34.  
  35.     //A file pointer to read a file
  36.     FILE *fp;
  37.  
  38.     //Open a file for both reading and writing. The file must exist.
  39.     fp = fopen(argv[argc-1], "r+");
  40.     if(fp == NULL)
  41.     {
  42.         //File does not exist, so it will be created for reading and writing.
  43.         fp = fopen(argv[argc-1], "w+");
  44.     }
  45.     else
  46.     {
  47.         //If file already exists and is not empty, then upload the
  48.         //  contents of the file into the data structure.
  49.         int c = 0;
  50.         int i = 0;
  51.         int lineNum = 0;
  52.         char charArray[INITIALSIZE] = {0}; //Initializes the array
  53.  
  54.         while( (c=getc(fp)) != EOF)
  55.         {
  56.             if(c == '\n')
  57.             {
  58.                 lineNum++;
  59.                 numOfLines++;
  60.                 charArray[i] = '\0';
  61.                 i = 0;
  62.  
  63.                 if(head == NULL)
  64.                 {
  65.                     ds *newNode = malloc(sizeof(ds));
  66.                     newNode->lineNumber = lineNum;
  67.                     newNode->lineData = malloc(strlen(charArray)+1); // 1 is added to make space for the terminating null character
  68.                     strcpy(newNode->lineData, charArray);
  69.                     newNode->next = NULL;
  70.                     head = newNode;
  71.                     tail = newNode;
  72.                 }
  73.                 else //iterate through the linked-list
  74.                 {
  75.                     ds *curr = head;
  76.                     while(curr->next != NULL)
  77.                     {
  78.                         curr = curr->next;
  79.                     }
  80.                     ds *newNode = malloc(sizeof(ds));
  81.                     newNode->lineNumber = lineNum;
  82.                     newNode->lineData = malloc(strlen(charArray)+1); // 1 is added to make space for the terminating null character
  83.                     strcpy(newNode->lineData, charArray);
  84.                     newNode->next = NULL;
  85.                     curr->next = newNode;
  86.                     tail = newNode;
  87.                 }
  88.  
  89.                 memset(charArray, 0, INITIALSIZE);
  90.                 continue;
  91.             }
  92.             charArray[i++] = c;
  93.         }
  94.     }
  95.  
  96.     char *enteredCommand = NULL;
  97.     size_t ec_size = 0;
  98.     while(1)
  99.     {
  100.         //Print the command prompt for the text editor
  101.         printf("textEditor-%s-$ ", argv[argc-1]);
  102.  
  103.         /*
  104.          * Read the user input. The possible inputs are:
  105.          *  p: Print the contents of the file.
  106.          *  a 3 I like cats!: Append "I like cats!" to the text at line number 3.
  107.          *  w: Write the changes back to the file (overwrites the file if it exists).
  108.          *  q: Quit
  109.          */
  110.         getline(&enteredCommand, &ec_size, stdin);
  111.         int ec_len = strlen(enteredCommand);
  112.         enteredCommand[ec_len-1] = '\0';
  113.  
  114.         if(strcmp(enteredCommand, "p") == 0)
  115.         {
  116.             //Print the contents of the file
  117.             ds *temp = head;
  118.             while(temp != NULL)
  119.             {
  120.                 printf("%s\n", temp->lineData);
  121.                 temp = temp->next;
  122.             }
  123.         }
  124.         else if(enteredCommand[0] == 'a')
  125.         {
  126.             //Append the content to the text at line number mentioned in the command
  127.             //First parse the line number
  128.             char* dest = malloc(strlen(enteredCommand)+1);
  129.             memset(dest, '\0', strlen(enteredCommand)+1);
  130.             strcpy(dest, enteredCommand);
  131.             const char delim = ' ';
  132.             char *token;
  133.             token = strtok(dest, &delim);
  134.             token = strtok(NULL, &delim);
  135.             if(token == NULL)
  136.             {
  137.                 printf("Error: The format to append is: \"a <lineNumber> <textToAppend>\"\n");
  138.                 exit(1);
  139.             }
  140.             int lineN = atoi(token);
  141.  
  142.             //Now parse the text to be appended
  143.             char *ret1, *ret2;
  144.             ret1 = strchr(enteredCommand, delim);
  145.             ret2 = strchr(ret1+1, delim);
  146.             if(ret2 == NULL)
  147.             {
  148.                 printf("Error: The format to append is: \"a <lineNumber> <textToAppend>\"\n");
  149.                 exit(1);
  150.             }
  151.             char *text = ret2 + 1;
  152.  
  153.             //Now update the datastore (ds) with the new append data
  154.             ds *temp = head;
  155.             if(lineN > numOfLines)
  156.             {
  157.                 numOfLines++;
  158.                 ds *newNode = malloc(sizeof(ds));
  159.                 newNode->lineNumber = numOfLines;
  160.                 newNode->lineData = malloc(strlen(text)+1); // 1 is added to make space for the terminating null character
  161.                 strcpy(newNode->lineData, text);
  162.                 newNode->next = NULL;
  163.                 if(tail == NULL)
  164.                 {
  165.                     head = newNode;
  166.                     tail = newNode;
  167.                 }
  168.                 else
  169.                 {
  170.                     tail->next = newNode;
  171.                     tail = newNode;
  172.                 }
  173.             }
  174.             else
  175.             {
  176.                 while(temp != NULL)
  177.                 {
  178.                     if(temp->lineNumber == lineN)
  179.                     {
  180.                         char *dest = malloc(strlen(temp->lineData) + strlen(text) + 1);
  181.                         strcat(dest, temp->lineData);
  182.                         strcat(dest, text);
  183.                         temp->lineData = dest;
  184.                     }
  185.                     temp = temp->next;
  186.                 }
  187.             }
  188.         }
  189.         else if(strcmp(enteredCommand, "w") == 0)
  190.         {
  191.             //Write the changes back to the file (overwrites the file if it exists)
  192.             fp = fopen(argv[argc-1], "w+");
  193.             ds *temp = head;
  194.             while(temp != NULL)
  195.             {
  196.                 fprintf(fp, "%s\n", temp->lineData);
  197.                 temp = temp->next;
  198.             }
  199.         }
  200.         else if(strcmp(enteredCommand, "q") == 0)
  201.         {
  202.             exit(0);
  203.         }
  204.  
  205.         free(enteredCommand);
  206.         enteredCommand = NULL;
  207.     }
  208.  
  209.     //Close all the file descriptors and free the allocated memories
  210.     fclose(fp);
  211.  
  212.     //We don't really need to free memory allocated for the linked-list, since the process is exiting now
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement