Thread: Help with a stream editor

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22

    Help with a stream editor

    It's supposed to take a string, input commands, and print out an edited string. This assignment is here:

    Stream Editor Assignment

    Right now, it does this:
    Code:
     ./a.out cmd.txt < in.txt
    1 To have text prepended
    2 To have text prepended
    3 To have text prepended
    4 Unmodified except appended text
    5 Unmodified except appended text
    6 Unmodified except appended text
    7 This is one line for substitution
    8 This is one line for substitution
    9 This is to be deleted
    10 This is to be deleted
    11 The last line of the file
    Here is my code, does anyone have any ideas why it doesn't edit the text?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define STRING_LENGTH 256
    
    //-------String Type Declaration--------
    typedef char String[STRING_LENGTH];
    //-------Edit Node Type Definition------
    typedef struct {
    
        int firstLine;
        int lastLine;
    
    } lineSpecs;
    
    typedef enum {
        TEXT,
        RANGE,
        NONE,
    }lineRangeSpecs;
    
    typedef union {
        lineSpecs range;
        String replaceText;
    
    } lineRange;
    
    
    typedef struct {
    /*Line range specification type - lines of none, text, or line numbers. This must be an enumerated type. */
    
        lineRangeSpecs editType;
    
    /*Line range specification. Because there are three possible formats for the line range specification, this field must be a union. */
    
        lineRange lines;
    
    /*The edit operation specifier. */
    
        char howEdit;
    
    /*The data associated with the edit operation.*/
    
        char data[80];
    
    } eachEdit;
    
    //-----------------------------------------------------------------------------
    
    char *replace_str(char *string, char *original, char *replacement)
    {
      static char buffer[4096];
      char *p;
    
      if(!(p = strstr(string, original)))  // Is 'orig' even in 'str'?
        return string;
    
      strncpy(buffer, string, p-string); // Copy characters from 'str' start to 'orig' st$
      buffer[p-string] = '\0';
    
      sprintf(buffer+(p-string), "%s%s", replacement, p+strlen(original));
    
      return buffer;
    }
    
    //-----------------------------------------------------------------------------
    void Append(String input,int lineNumber, eachEdit append) {
    /*method that will edit A*/
    
        if (append.editType == RANGE) {
            if (lineNumber >= append.lines.range.firstLine && lineNumber <= append.lines.range.lastLine) {
            strcat(input,append.data);
            }
        }
        if (append.editType == NONE) {
        strcat(input,append.data);
        }
        if (append.editType == TEXT) {
        if (strstr(input,append.lines.replaceText)) {
            strcat(input,append.data);
        }
        }
    
    }
    //-----------------------------------------------------------------------------
    /*method that will edit I*/
    void Insert(String input,int lineNumber, eachEdit insert) {
    
        char *temp;
        temp = insert.data;
    
        if (insert.editType == RANGE) {
            if (lineNumber >= insert.lines.range.firstLine && lineNumber <= insert.lines.range.lastLine) {
            strcat(temp,input);
            strcpy(input,temp);
            }
        }
        if (insert.editType == NONE) {
        strcat(temp,input);
        strcpy(input,temp);
        }
        if (insert.editType == TEXT) {
        if (strstr(input,insert.lines.replaceText)) {
            strcat(temp,input);
            strcpy(input,temp);
        }
        }
    }
    //-----------------------------------------------------------------------------
    /*method that will edit O*/
    void Prepend(String input,int lineNumber, eachEdit prepend) {
    
        char *temp;
        temp = prepend.data;
    
        if (prepend.editType == RANGE) {
            if (lineNumber >= prepend.lines.range.firstLine && lineNumber <= prepend.lines.range.lastLine) {
            printf("%s\n",temp);
            }
        }
        if (prepend.editType == NONE) {
        printf("%s\n",temp);
        }
        if (prepend.editType == TEXT) {
        if (strstr(input,prepend.lines.replaceText)) {
            printf("%s\n",temp);
        }
        }
    
    }
    //-----------------------------------------------------------------------------
    /*method that will edit d*/
    void Delete(String input,int lineNumber, eachEdit delete) {
        if (delete.editType == RANGE) {
            if (lineNumber >= delete.lines.range.firstLine && lineNumber <= delete.lines.range.lastLine) {
             strcpy(input,"\0");
            }
        }
        if (delete.editType == NONE) {
        strcpy(input,"\0");
        }
        if (delete.editType == TEXT) {
        if (strstr(input,delete.lines.replaceText)) {
            strcpy(input,"\0");
        }
        }
    }
    //-----------------------------------------------------------------------------
    /*method that will edit s*/
    void Substitute(String input,int lineNumber, eachEdit substitute) {
        char *search = "/";
        char *original;
        char *replacement;
    
        if (substitute.editType == RANGE) {
            if (lineNumber >= substitute.lines.range.firstLine && lineNumber <= substitute.lines.range.lastLine) {
    
            original = strtok(substitute.data,search);
            replacement = strtok(NULL, search);
            strcpy(input,replace_str(input,original,replacement));
    
            }
        }
        if (substitute.editType == NONE) {
        original = strtok(substitute.data,search);
        replacement = strtok(NULL, search);
        strcpy(input,replace_str(input,original,replacement));
        }
        if (substitute.editType == TEXT) {
        if (strstr(input,substitute.lines.replaceText)) {
            original = strtok(substitute.data,search);
            replacement = strtok(NULL, search);
            strcpy(input,replace_str(input,original,replacement));
        }
        }
    
    }
    //-----------------------------------------------------------------------------
    void ReadIn(eachEdit EditCommands[100],int lineNumber,String readIn,int lastIndex) {
    
        int structIndex = 0;
    
        while (structIndex < lastIndex) {
    
        if (EditCommands[structIndex].howEdit == 'A'){
                Append(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 'I'){
                Insert(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 'O'){
                Prepend(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 'd'){
                Delete(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 's'){
                Substitute(readIn,lineNumber,EditCommands[structIndex]);
        }
        structIndex++;
        }
    
    }
    //-----------------------------------------------------------------------------
    
    
    int main(int argc, char *argv[]) {
    
        eachEdit EditCommands[100];
        String input;
        int index = 0;
        int structIndex = 0;
        eachEdit toSave;
        int lineNumber = 1;
        String toEdit;
        FILE *openFile;
    
        openFile = fopen(argv[1], "r");
        while (fgets(input,80,openFile)){
    
        input[strlen(input)-1] = '\0';
    
        if(input[0] == 0) {
            break;
        }
    
            if (input[0] >= '0' && input[0] <= '9'){
            index = 0;
            toSave.editType = RANGE;
            toSave.lines.range.firstLine = atoi(input);
            while (input[index] != ','){
                index++;
            }
            toSave.lines.range.lastLine = atoi(input+index+1);
    
            while (input[index] != '/'){
                index++;
            }
            index++;
    
            } else if (input[0] == '/') {
            toSave.editType = TEXT;
            index = 1;
            while(input[index] != '/') {
                toSave.lines.replaceText[index-1] = input[index];
                index++;
            }
            index++;
    
            } else { //in case of letters
            toSave.editType = NONE;
                index = 0;
    
            }
    
        toSave.howEdit = input[index];
        index++;
        strcpy(toSave.data,(input+index));
    
        EditCommands[structIndex] = toSave;
    
        structIndex++;
    
        }
        fclose(openFile);
    
        while (fgets(toEdit,256,stdin)){
    
        toEdit[strlen(toEdit)-1] = '\0';
    
            ReadIn(EditCommands,lineNumber,toEdit,structIndex);
        if(toEdit[0] != '\0') {
                printf("%s\n", toEdit);
        }
            lineNumber++;
        }
    
    
        return(EXIT_SUCCESS);
    }
    //-----------------------------------------------------------------------------
    and if you want to try it out, here:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define STRING_LENGTH 256
    
    //-------String Type Declaration--------
    typedef char String[STRING_LENGTH];
    //-------Edit Node Type Definition------
    typedef struct {
    
        int firstLine;
        int lastLine;
    
    } lineSpecs;
    
    typedef enum {
        TEXT,
        RANGE,
        NONE,
    }lineRangeSpecs;
    
    typedef union {
        lineSpecs range;
        String replaceText;
    
    } lineRange;
    
    
    typedef struct {
    /*Line range specification type - lines of none, text, or line numbers. This must be an enumerated type. */
    
        lineRangeSpecs editType;
    
    /*Line range specification. Because there are three possible formats for the line range specification, this field must be a union. */
    
        lineRange lines;
    
    /*The edit operation specifier. */
    
        char howEdit;
    
    /*The data associated with the edit operation.*/
    
        char data[80];
    
    } eachEdit;
    
    //-----------------------------------------------------------------------------
    
    char *replace_str(char *string, char *original, char *replacement)
    {
      static char buffer[4096];
      char *p;
    
      if(!(p = strstr(string, original)))  // Is 'orig' even in 'str'?
        return string;
    
      strncpy(buffer, string, p-string); // Copy characters from 'str' start to 'orig' st$
      buffer[p-string] = '\0';
    
      sprintf(buffer+(p-string), "%s%s", replacement, p+strlen(original));
    
      return buffer;
    }
    
    //-----------------------------------------------------------------------------
    void Append(String input,int lineNumber, eachEdit append) {
    /*method that will edit A*/
    
        if (append.editType == RANGE) {
            if (lineNumber >= append.lines.range.firstLine && lineNumber <= append.lines.range.lastLine) {
            strcat(input,append.data);
            }
        }
        if (append.editType == NONE) {
        strcat(input,append.data);
        }
        if (append.editType == TEXT) {
        if (strstr(input,append.lines.replaceText)) {
            strcat(input,append.data);
        }
        }
    
    }
    //-----------------------------------------------------------------------------
    /*method that will edit I*/
    void Insert(String input,int lineNumber, eachEdit insert) {
    
        char *temp;
        temp = insert.data;
    
        if (insert.editType == RANGE) {
            if (lineNumber >= insert.lines.range.firstLine && lineNumber <= insert.lines.range.lastLine) {
            strcat(temp,input);
            strcpy(input,temp);
            }
        }
        if (insert.editType == NONE) {
        strcat(temp,input);
        strcpy(input,temp);
        }
        if (insert.editType == TEXT) {
        if (strstr(input,insert.lines.replaceText)) {
            strcat(temp,input);
            strcpy(input,temp);
        }
        }
    }
    //-----------------------------------------------------------------------------
    /*method that will edit O*/
    void Prepend(String input,int lineNumber, eachEdit prepend) {
    
        char *temp;
        temp = prepend.data;
    
        if (prepend.editType == RANGE) {
            if (lineNumber >= prepend.lines.range.firstLine && lineNumber <= prepend.lines.range.lastLine) {
            printf("%s\n",temp);
            }
        }
        if (prepend.editType == NONE) {
        printf("%s\n",temp);
        }
        if (prepend.editType == TEXT) {
        if (strstr(input,prepend.lines.replaceText)) {
            printf("%s\n",temp);
        }
        }
    
    }
    //-----------------------------------------------------------------------------
    /*method that will edit d*/
    void Delete(String input,int lineNumber, eachEdit delete) {
        if (delete.editType == RANGE) {
            if (lineNumber >= delete.lines.range.firstLine && lineNumber <= delete.lines.range.lastLine) {
             strcpy(input,"\0");   
            }
        }
        if (delete.editType == NONE) {
        strcpy(input,"\0");
        }
        if (delete.editType == TEXT) {
        if (strstr(input,delete.lines.replaceText)) {
            strcpy(input,"\0");
        }
        }
    }
    //-----------------------------------------------------------------------------
    /*method that will edit s*/
    void Substitute(String input,int lineNumber, eachEdit substitute) {
        char *search = "/";
        char *original;
        char *replacement;
    
        if (substitute.editType == RANGE) {
            if (lineNumber >= substitute.lines.range.firstLine && lineNumber <= substitute.lines.range.lastLine) {
    
            original = strtok(substitute.data,search);
            replacement = strtok(NULL, search);
            strcpy(input,replace_str(input,original,replacement));
            
            }
        }
        if (substitute.editType == NONE) {
        original = strtok(substitute.data,search);
        replacement = strtok(NULL, search);
        strcpy(input,replace_str(input,original,replacement));
        }
        if (substitute.editType == TEXT) {
        if (strstr(input,substitute.lines.replaceText)) {
            original = strtok(substitute.data,search);
            replacement = strtok(NULL, search);
            strcpy(input,replace_str(input,original,replacement));
        }
        }
    
    }
    //-----------------------------------------------------------------------------
    void ReadIn(eachEdit EditCommands[100],int lineNumber,String readIn,int lastIndex) {
    
        int structIndex = 0;
    
        while (structIndex < lastIndex) {
    
        if (EditCommands[structIndex].howEdit == 'A'){
                Append(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 'I'){
                Insert(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 'O'){
                Prepend(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 'd'){
                Delete(readIn,lineNumber,EditCommands[structIndex]);
        }
        if (EditCommands[structIndex].howEdit == 's'){
                Substitute(readIn,lineNumber,EditCommands[structIndex]);
        }
        structIndex++;
        }
    
    }
    //-----------------------------------------------------------------------------
    
    
    int main(int argc, char *argv[]) {
    
        eachEdit EditCommands[100];
        String input;
        int index = 0;
        int structIndex = 0;
        eachEdit toSave;
        int lineNumber = 1;
        String toEdit;
        FILE *openFile;
    
        openFile = fopen(argv[1], "r");
        while (fgets(input,80,openFile)){
    
        input[strlen(input)-1] = '\0';
    
        if(input[0] == 0) {
            break;
        }
    
            if (input[0] >= '0' && input[0] <= '9'){
            index = 0;
            toSave.editType = RANGE;
            toSave.lines.range.firstLine = atoi(input);
            while (input[index] != ','){
                index++;
            }
            toSave.lines.range.lastLine = atoi(input+index+1);
    
            while (input[index] != '/'){
                index++;
            }
            index++;
    
            } else if (input[0] == '/') {
            toSave.editType = TEXT;
            index = 1;
            while(input[index] != '/') {
                toSave.lines.replaceText[index-1] = input[index];
                index++;
            }
            index++;
    
            } else { //in case of letters
            toSave.editType = NONE;
                index = 0;
    
            }
    
        toSave.howEdit = input[index];
        index++;
        strcpy(toSave.data,(input+index));
    
        EditCommands[structIndex] = toSave;
    
        structIndex++;
    
        }
        fclose(openFile);
    
        while (fgets(toEdit,256,stdin)){
    
        toEdit[strlen(toEdit)-1] = '\0';
    
            ReadIn(EditCommands,lineNumber,toEdit,structIndex);
        if(toEdit[0] != '\0') {
                printf("%s\n", toEdit);
        }
            lineNumber++;
        }
    
    
        return(EXIT_SUCCESS);
    }
    //-----------------------------------------------------------------------------
    Last edited by Salem; 04-17-2012 at 09:39 PM. Reason: Removed line numbers from code

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    i think it would have been alot easier cutting down on code if you used strncat to append the strings rather following the long processes you currently follow........

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Quote Originally Posted by Nyah Check View Post
    i think it would have been alot easier cutting down on code if you used strncat to append the strings rather following the long processes you currently follow........
    There is a lot I need to cut down on to make my program smaller, but in the meantime, do you have any ideas on how to make it work?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok let me see

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    looking at your code with my compiler the problem comes from line 20 to 24 with the typedef enum structure is TEXT, RANGE AND none char types, or typedef, or char * tell me so i can proceed

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    what are you trying to do here?

    HERE IS THE PROBLEM; what are you doing here?

    Code:
    ypedef enum {
    
         TEXT;
        RANGE;
        NONE;
    }lineRangeSpecs;

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What are you talking about Nyah? Please don't try to "help" if you don't know what you're talking about. It's just stupid.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Can anyone help with this?

  9. #9
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Quick update on this, I figured out what's wrong with it, but don't know how to fix it. Instead of taking lines 1-6 or whatever of the commands text file, it's taking the file's inner coding. I added:
    Code:
    printf("%c\n",input[0]);
    right before I determine what ENUM to label it as and I put
    Code:
    printf("%d,%d,%d,%c,%s\n",toSave.editType,toSave.lines.range.firstLine,toSave.lines.range.lastLine,toSave.howEdit,toSave.data);
    after every command has been decided. This is what I get:
    Code:
    {
    2,0,0,{,\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
    {
    2,0,0,{,\fonttbl\f0\fmodern\fcharset0 Courier;}
    {
    2,0,0,{,\colortbl;\red255\green255\blue255;}
    \
    2,0,0,\,margl1440\margr1440\vieww9000\viewh8400\viewkind0
    \
    2,0,0,\,deftab720
    \
    2,0,0,\,pard\pardeftab720\ql\qnatural
    Any ideas on what to do?

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    My brain isn't exactly working yet, but I'll be back to help later because you've shown yourself to be ready to "party".

    In the mean time, I have to wonder, is your command file saved as a true ASCII only text file? From what you've posted it seems like you may have saved the command file in some other format.

    Also, because you think you know where the problem comes from, it would help if you post the latest code, without manually adding line numbers, and mark with comments where you think the problem is originating or where the error is first observable.

    Soma

  11. #11
    Registered User
    Join Date
    Mar 2012
    Location
    Miami, Florida, United States
    Posts
    22
    Thank you so much Soma, however that middle paragraph explains purely what my problem was:

    My mac textedit wouldn't let me save it as a .txt, so I saved it as .rtf and renamed it to .txt. Instead, I opened up vi in.txt and cmd.txt and it now works

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You are welcome.

    *shrug*

    Regulars like posters who respond to questions and instructions the way you've done.

    As long as you post your future problems with the same spirit I have no doubt that you'll find the regulars are more than willing to help you solve your problems.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit Stream
    By Dae in forum C++ Programming
    Replies: 0
    Last Post: 11-26-2009, 10:04 PM
  2. cin stream
    By RaisinToe in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2009, 01:20 PM
  3. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  4. stream help!?
    By antex in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 02:18 PM
  5. I/O Stream
    By kylesbigdog in forum C++ Programming
    Replies: 6
    Last Post: 04-07-2003, 03:45 AM