CodeWiki : IntroToComputing.CW2

WikiHome :: List Pages :: Login
cmantito.com

Revision [374]

Most recent edit made on 2009-04-26 15:08:32 by cmantito

Additions:
#define DELIMITER "."
#define NEWLINE_DELIM ".\n"
#define STATE_COMMAND 1
#define STATE_INPUT 2
#define STATE_EXIT 3
#define BUFFER_SIZE 1024
#define CMD_DELIMITER ";"
#define CMD_PRINT 'p'
#define CMD_QUIT 'q'
#define CMD_APPENDS 'a'
#define CMD_REPLACE 'r'
#define CMD_DELETE 'd'
#define CMD_INSERT 'i'
Setup whatever we need.
int appState = STATE_COMMAND;
int lineNumber = 0;
char valid_cmds[BUFFER_SIZE + 1] = {'\0'};
int location = 0;
int locationB = 0;
char commandLetter;
char notModifier;
char rangeModifier;
char* string = "";
int skipPrint = 0;
We're going to loop through here until we're all done. (ie, STATE_EXIT)
Read input into the buffer.
Make sure it exists.
Delimiters make the state increase.
if(strcmp(buffer, DELIMITER)
0 || strcmp(buffer, NEWLINE_DELIM)
0){
appState = appState + 1;
Command state.
if(appState
STATE_COMMAND){
Get some commands from the buffer.
Go through each character of the command, and see if it matches a
real command, if it does, put it in the valid commands buffer.
appState = STATE_EXIT;
Text input state.
if(appState
STATE_INPUT){
skipPrint = 0; This must must MUST get set to 0 on each loop, or everything will break.
lineNumber = lineNumber + 1;
Increment the line number each time we go through.
all_cmds = strdup(valid_cmds); And now all_cmds is the same as valid_cmds.
Get a command from the commands buffer.
Match this one first, because it's the most specific - put the various bits of the command
into the various variables. Also, make sure the range modifier is one of the valid ones,
and that the not modifier is what we expect.
if(sscanf(cmd_line, "%d%c%d%c%c%s", &location, &rangeModifier, &locationB, ¬Modifier, &commandLetter, string)
6
  • & (rangeModifier
',' || rangeModifier
'~')
  • & notModifier
  • '!'){
    Now check the command issued. Appends? Yeah, check the range --
    For comma, make sure the line number isn't in the range specified. (Remember the '!'.)
    For tilde, check that A) the line number isn't the one specified, B) the line number minux
    the starting point divded by the second number has a remainder, C) the line number is less than
    the starting point. If A and B or A and C are true, we'll match.
    if(commandLetter
    CMD_APPENDS && (
    (rangeModifier
    ',' && (location > lineNumber || lineNumber > locationB)) || (rangeModifier
    '~' && location != lineNumber && ((lineNumber - location) % locationB > 0 || lineNumber < location)) Append, stick the string to the end of the existing buffer and attach a newline.
    Append will use this same method for the rest off the programme.
    Same method for matching replace.
    if(commandLetter CMD_REPLACE && (
    (rangeModifier
    ',' && (location > lineNumber || lineNumber > locationB)) || (rangeModifier
    '~' && location != lineNumber && ((lineNumber - location) % locationB > 0 || lineNumber < location))
    Just replace the buffer with the new string and add a newline to the end.
    Replacement will be done the same way as this for the rest of the app.
    And insert.
    if(commandLetter
    CMD_INSERT && (
    (rangeModifier
    ',' && (location > lineNumber || lineNumber > locationB)) || (rangeModifier
    '~' && location != lineNumber && ((lineNumber - location) % locationB > 0 || lineNumber < location)) Add a newline to the end of the new string, stick the buffer on after that, and then
    move it all in to the buffer. We'll reuse this method for insert throughout the app.
    We're moving down in order of most specific matching regime to least specific. So this one's next.
    }else if(sscanf(cmd_line, "%d%c%d%c%c", &location, &rangeModifier, &locationB, ¬Modifier, &commandLetter) 5
    • & (rangeModifier
      ',' || rangeModifier
      '~')
    • & notModifier
      '!'){ We're using the same matching system as before to match the ranges for print, remembering that
      there's still an !.
      if(commandLetter
      CMD_PRINT && (
      (rangeModifier
      ',' && (location > lineNumber || lineNumber > locationB)) || (rangeModifier
      '~' && location != lineNumber && ((lineNumber - location) % locationB > 0 || lineNumber < location))
      And delete.
      if(commandLetter
      CMD_DELETE && (
      (rangeModifier
      ',' && (location > lineNumber || lineNumber > locationB)) || (rangeModifier
      '~' && location != lineNumber && ((lineNumber - location) % locationB > 0 || lineNumber < location)) skipPrint = 1;
    This is the next one in order, this time no !
    }else if(sscanf(cmd_line, "%d%c%d%c%s", &location, &rangeModifier, &locationB, &commandLetter, string) 5
    • & (rangeModifier
      ',' || rangeModifier
      '~')){
      For appends, we're checking if the range is a comma or a tilde. If it's comma, is the line number
      in between the addresses specified? If it's a tilde, check A) is line number equal to the location
      specified, B) does the line number minus the location, divided by the second number,
      have NO remainder? and C) is the line number greater than the location specified?
      If A is true, or B and C are true, we match.
      if(commandLetter
      CMD_APPENDS && (
      (rangeModifier
      ',' && location <= lineNumber && lineNumber <= locationB) || (rangeModifier
      '~' && (location
      lineNumber || (((lineNumber - location) % locationB)
      0 && lineNumber > location)))
    if(commandLetter CMD_REPLACE && (
    (rangeModifier
    ',' && location
    lineNumber) || (rangeModifier
    '~' && (location
    lineNumber || (((lineNumber - location) % locationB)
    0 && lineNumber > location)))
    Using this to for the ranged replace for a comma-range only, once we've replaced the first line,
    We make sure that none of the lines after it print until we're out of the range.
    Ref: http://cnfolio.com/IntroComputingTutorial05#example05
    if(commandLetter CMD_REPLACE && (
    (rangeModifier
    ',' && lineNumber > location && lineNumber <= locationB) skipPrint = 1;
    if(commandLetter CMD_INSERT && (
    (rangeModifier
    ',' && location <= lineNumber && lineNumber <= locationB) || (rangeModifier
    '~' && (location
    lineNumber || (((lineNumber - location) % locationB)
    0 && lineNumber > location)))
    And then this one goes next.
    }else if(sscanf(cmd_line, "%d%c%d%c", &location, &rangeModifier, &locationB, &commandLetter)
    4
    • & (rangeModifier
    ',' || rangeModifier
    '~')){ Same method as before for dealing with the different ranges.
    if(commandLetter
    CMD_PRINT && (
    (rangeModifier
    ',' && location <= lineNumber && lineNumber <= locationB) || (rangeModifier
    '~' && (location
    lineNumber || (((lineNumber - location) % locationB)
    0 && lineNumber > location)))
    if(commandLetter
    CMD_DELETE && (
    (rangeModifier
    ',' && location <= lineNumber && lineNumber <= locationB) || (rangeModifier
    '~' && (location
    lineNumber || (((lineNumber - location) % locationB)
    0 && lineNumber > location))) skipPrint = 1;
    This one's next but it doesn't have a range, just the not modifier.
    }else if(sscanf(cmd_line, "%d%c%c%s", &location, ¬Modifier, &commandLetter, string) 4 We do have a not modifier here, so we need to not match everything.
    }else if(sscanf(cmd_line, "%d%c%c", &location, ¬Modifier, &commandLetter) 3
    This time we don't have the not modifier.
    }else if(sscanf(cmd_line, "%d%c%s", &location, &commandLetter, string) 3){
    So we check that the line numbers DO match. Again, no worry about ranges.
    if(commandLetter
    CMD_APPENDS && location
    lineNumber){
    Same here.
    if(commandLetter
    CMD_REPLACE && location
    lineNumber){ And here.
    if(commandLetter
    CMD_INSERT && location
    lineNumber){
    We don't really have to make sure location > 0, but it's better to
    validate as much of the input as you can, to weed out falsely-matching
    input.
    }else if(sscanf(cmd_line, "%d%c", &location, &commandLetter)
    2
    CMD_PRINT && location
    lineNumber){ Here too.
    if(commandLetter
    CMD_QUIT && location
    lineNumber){
    appState = STATE_EXIT;
    This one as well.
    if(commandLetter
    CMD_DELETE && location
    lineNumber){
    skipPrint = 1;
    We have a not modifier here.
    }else if(sscanf(cmd_line, "%c%c", ¬Modifier, &commandLetter)
    2 We're back to a simple command that effects every line, but not ! modifier.
    }else if(sscanf(cmd_line, "%c%s", &commandLetter, string) 2){
    So we don't need to worry about line matching.
    if(commandLetter
    CMD_APPENDS){ Or here.
    if(commandLetter
    CMD_REPLACE){ Or here.
    if(commandLetter
    CMD_INSERT){ }
    Very simple one letter commands.
    }else if(sscanf(cmd_line, "%c", &commandLetter) 1){
    Which match every line.
    if(commandLetter
    CMD_PRINT){ And here.
    if(commandLetter
    CMD_QUIT){
    appState = STATE_EXIT;
    Same here.
    if(commandLetter
    CMD_DELETE){
    skipPrint = 1;
    If nothing's told it not to print since the beginning of the loop,
    print the buffer. By this point, the buffer may have been modified
    by a replace, append, or insert. Doesn't matter, print it anyway as
    long as nothing set skipPrint to 1.
    if(skipPrint 0){
    } while(appState != STATE_EXIT);

    Deletions:
    #define CMDS_DEL "."
    #define CMDS_DEL_NL ".\n"
    #define MODE_CMD 1
    #define MODE_TEXT 2
    #define MODE_END 3
    #define CMD_BUFFER 1024
    #define CMD_DELIMITER ";"
    #define CMD_PRINT 'p'
    #define CMD_QUIT 'q'
    #define CMD_APPENDS 'a'
    #define CMD_REPLACE 'r'
    #define CMD_DELETE 'd'
    #define CMD_INSERT 'i'
    int program_mode = MODE_CMD;
    int line_number = 0;
    char valid_cmds[CMD_BUFFER + 1] = {'\0'};
    int number = 0;
    int number_b = 0;
    char letter;
    char symbol;
    char commatilde;
    char* string = NULL;
    int skip_print = 0;
    if(strcmp(buffer, CMDS_DEL)
    0 || strcmp(buffer, CMDS_DEL_NL)
    0){
    program_mode = program_mode + 1;
    if(program_mode
    MODE_CMD){
    program_mode = MODE_END;
    if(program_mode
    MODE_TEXT){
    skip_print = 0;
    line_number = line_number + 1;
    all_cmds = strdup(valid_cmds);
    most specific - 6 parameters, 2 of which are verified
    if(sscanf(cmd_line, "%d%c%d%c%c%s", &number, &commatilde, &number_b, &symbol, &letter, string)
    6
    • & (commatilde
    ',' || commatilde
    '~')
  • & symbol
  • '!'){ if(letter
    CMD_APPENDS && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    if(letter CMD_REPLACE && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    if(letter
    CMD_INSERT && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    this is next cause it's 5, 2 of which are verified.
    }else if(sscanf(cmd_line, "%d%c%d%c%c", &number, &commatilde, &number_b, &symbol, &letter) 5
    • & (commatilde
      ',' || commatilde
      '~')
    • & symbol
      '!'){ if(letter
      CMD_PRINT && (
      (commatilde
      ',' && (number > line_number || line_number > number_b)) || (commatilde
      '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
      if(letter
      CMD_DELETE && (
      (commatilde
      ',' && (number > line_number || line_number > number_b)) || (commatilde
      '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number)) skip_print = 1;
    then this because it's also 5, 1 of which is verified.
    }else if(sscanf(cmd_line, "%d%c%d%c%s", &number, &commatilde, &number_b, &letter, string) 5
    • & (commatilde
      ',' || commatilde
      '~')){ if(letter
      CMD_APPENDS && (
      (commatilde
      ',' && number <= line_number && line_number <= number_b) || (commatilde
      '~' && (number
      line_number || (((line_number - number) % number_b)
      0 && line_number > number)))
    if(letter CMD_REPLACE && (
    (commatilde
    ',' && number
    line_number) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    This is to replace several lines of text with one line of text. See http://cnfolio.com/IntroComputingTutorial05#example05
    if(letter CMD_REPLACE && (
    (commatilde
    ',' && line_number > number && line_number <= number_b) skip_print = 1;
    if(letter CMD_INSERT && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    and then this which is 4 paramters, 1 of which is verified.
    this and the one below shouldn't conflict cause of the verification in the same position.
    }else if(sscanf(cmd_line, "%d%c%d%c", &number, &commatilde, &number_b, &letter)
    4
    ',' || commatilde
    '~')){ if(letter
    CMD_PRINT && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    if(letter
    CMD_DELETE && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number))) skip_print = 1;
    and then this which is also 4, 1 of which is verified.
    this and the one above shouldn't conflict cause of the verification in the same position.
    }else if(sscanf(cmd_line, "%d%c%c%s", &number, &symbol, &letter, string) 4 this one's three paramters, one which we verify.
    }else if(sscanf(cmd_line, "%d%c%c", &number, &symbol, &letter) 3
    and this one is three, none which we can verify, ergo, less specific.
    }else if(sscanf(cmd_line, "%d%c%s", &number, &letter, string) 3){
    if(letter
    CMD_APPENDS && number
    line_number){ if(letter
    CMD_REPLACE && number
    line_number){ if(letter
    CMD_INSERT && number
    line_number){
    this one is two. check the number is > 0 can be a verification, although
    it's not strictly necessary.
    }else if(sscanf(cmd_line, "%d%c", &number, &letter)
    2
    CMD_PRINT && number
    line_number){ if(letter
    CMD_QUIT && number
    line_number){
    program_mode = MODE_END;
    if(letter
    CMD_DELETE && number
    line_number){
    skip_print = 1;
    this one is also two, and we can verify the symbol.
    }else if(sscanf(cmd_line, "%c%c", &symbol, &letter)
    2 and this one is also two, with no verification.
    }else if(sscanf(cmd_line, "%c%s", &letter, string) 2){
    if(letter
    CMD_APPENDS){ if(letter
    CMD_REPLACE){ if(letter
    CMD_INSERT){ }
    and this one is one paramter, being least specific.
    }else if(sscanf(cmd_line, "%c", &letter) 1){
    if(letter
    CMD_PRINT){ if(letter
    CMD_QUIT){
    program_mode = MODE_END;
    if(letter
    CMD_DELETE){
    skip_print = 1;
    if(skip_print 0){ } while(program_mode != MODE_END);



    Revision [373]

    Edited on 2009-04-22 01:58:10 by cmantito [minor bugfixes.]

    Additions:

    if(sscanf(cmd_line, "%d%c%d%c%c%s", &number, &commatilde, &number_b, &symbol, &letter, string)
    6
    if(letter
    CMD_APPENDS && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    if(letter
    CMD_REPLACE && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    if(letter CMD_INSERT && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    }else if(sscanf(cmd_line, "%d%c%d%c%c", &number, &commatilde, &number_b, &symbol, &letter)
    5
    if(letter
    CMD_PRINT && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    if(letter
    CMD_DELETE && (
    (commatilde
    ',' && (number > line_number || line_number > number_b)) || (commatilde
    '~' && number != line_number && ((line_number - number) % number_b > 0 || line_number < number))
    }else if(sscanf(cmd_line, "%d%c%d%c%s", &number, &commatilde, &number_b, &letter, string) 5
    if(letter
    CMD_APPENDS && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    if(letter CMD_REPLACE && (
    (commatilde
    ',' && number
    line_number) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    This is to replace several lines of text with one line of text. See http://cnfolio.com/IntroComputingTutorial05#example05
    if(letter CMD_REPLACE && (
    (commatilde
    ',' && line_number > number && line_number <= number_b)
    if(letter CMD_INSERT && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    }else if(sscanf(cmd_line, "%d%c%d%c", &number, &commatilde, &number_b, &letter)
    4
    if(letter
    CMD_PRINT && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number)))
    if(letter
    CMD_DELETE && (
    (commatilde
    ',' && number <= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (((line_number - number) % number_b)
    0 && line_number > number))) }else if(sscanf(cmd_line, "%d%c%c%s", &number, &symbol, &letter, string)
    4 }else if(sscanf(cmd_line, "%d%c%c", &number, &symbol, &letter)
    3 }else if(sscanf(cmd_line, "%d%c%s", &number, &letter, string)
    3){ }else if(sscanf(cmd_line, "%d%c", &number, &letter)
    2 }else if(sscanf(cmd_line, "%c%c", &symbol, &letter)
    2 }else if(sscanf(cmd_line, "%c%s", &letter, string)
    2){ }else if(sscanf(cmd_line, "%c", &letter)
    1){


    Deletions:

    if(sscanf("%d%c%d%c%c%s", &number, &commatilde, &number_b, &symbol, &letter, string)
    6
    if(letter = CMD_APPENDS && (
    (commatilde
    ',' && number < line_number || line_number > number_b) || (commatilde
    '~' && (number != line_number && (line_number - number % number_b) > 0))
    if(letter = CMD_REPLACE && (
    (commatilde ',' && number < line_number || line_number > number_b) || (commatilde
    '~' && (number != line_number && (line_number - number % number_b) > 0))
    if(letter = CMD_INSERT && (
    (commatilde ',' && number < line_number || line_number > number_b) || (commatilde
    '~' && (number != line_number && (line_number - number % number_b) > 0))
    }else if(sscanf("%d%c%d%c%c", &number, &commatilde, &number_b, &symbol, &letter) 5
    if(letter = CMD_PRINT && (
    (commatilde
    ',' && number < line_number || line_number > number_b) || (commatilde
    '~' && (number != line_number && (line_number - number % number_b) > 0))
    if(letter = CMD_DELETE && (
    (commatilde
    ',' && number < line_number || line_number > number_b) || (commatilde
    '~' && (number != line_number && (line_number - number % number_b) > 0))
    }else if(sscanf("%d%c%d%c%s", &number, &commatilde, &number_b, &letter, string)
    5
    if(letter = CMD_APPENDS && (
    (commatilde
    ',' && number >= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (line_number - number % number_b)
    0))
    if(letter = CMD_REPLACE && (
    (commatilde ',' && number >= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (line_number - number % number_b)
    0))
    if(letter = CMD_INSERT && (
    (commatilde ',' && number >= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (line_number - number % number_b)
    0))
    }else if(sscanf("%d%c%d%c", &number, &commatilde, &number_b, &letter) 4
    if(letter = CMD_PRINT && (
    (commatilde
    ',' && number >= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (line_number - number % number_b)
    0))
    if(letter = CMD_DELETE && (
    (commatilde
    ',' && number >= line_number && line_number <= number_b) || (commatilde
    '~' && (number
    line_number || (line_number - number % number_b)
    0))
    }else if(sscanf("%d%c%c%s", &number, &symbol, &letter, string)
    4 }else if(sscanf("%d%c%c", &number, &symbol, &letter)
    3 }else if(sscanf("%d%c%s", &number, &letter, string)
    3){ }else if(sscanf("%d%c", &number, &letter)
    2 }else if(sscanf("%c%c", &symbol, &letter)
    2
    if(letter = CMD_DELETE){
    }else if(sscanf("%c%s", &letter, string)
    2){ }else if(sscanf("%c", &letter)
    1){




    Revision [372]

    Edited on 2009-04-21 16:35:34 by cmantito [initial commit!]

    Additions:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define CMDS_DEL        "."
    #define CMDS_DEL_NL     ".\n"

    #define MODE_CMD        1   
    #define MODE_TEXT       2
    #define MODE_END        3

    #define CMD_BUFFER      1024
    #define CMD_DELIMITER   ";"

    #define CMD_PRINT       'p'
    #define CMD_QUIT        'q'
    #define CMD_APPENDS     'a'
    #define CMD_REPLACE     'r'
    #define CMD_DELETE      'd'
    #define CMD_INSERT      'i'

    int main (void) {
        char* buffer = NULL;
        int buffer_size = 0;
        int buffer_length = 0;
        int chars_read = 0;
        int program_mode = MODE_CMD;
        int line_number = 0;
        int char_pos = 0;
       
        char valid_cmds[CMD_BUFFER + 1] = {'\0'};
        char* all_cmds = NULL;
        char* cmd_line = NULL;
       
        int number = 0;
        int number_b = 0;
        char letter;
        char symbol;
        char commatilde;
        char* string = NULL;
       
        int skip_print = 0;
       
        do {
            chars_read = getline(&buffer, &buffer_size, stdin);
           
            if(chars_read < 1){
                break;
            }
           
            if(strcmp(buffer, CMDS_DEL) == 0 || strcmp(buffer, CMDS_DEL_NL) == 0){
                program_mode = program_mode + 1;
                continue;
            }
           
            if(program_mode == MODE_CMD){
               
                buffer_length = strlen(buffer);
                for(char_pos = 0; char_pos < buffer_length; char_pos++){
                        if(islower(buffer[char_pos])){
                            if(buffer[char_pos] == CMD_PRINT ||
                                buffer[char_pos] == CMD_QUIT ||
                                buffer[char_pos] == CMD_APPENDS ||
                                buffer[char_pos] == CMD_REPLACE ||
                                buffer[char_pos] == CMD_DELETE ||
                                buffer[char_pos] == CMD_INSERT ){
                                    strcat(valid_cmds, buffer);
                                    strcat(valid_cmds, CMD_DELIMITER);
                                    break;
                            }
                       
                        program_mode  = MODE_END;
                        break;
                    }
                }
            }
           
            if(program_mode == MODE_TEXT){
                skip_print = 0;
                line_number = line_number + 1;
                all_cmds = strdup(valid_cmds);
               
                for(cmd_line = strtok(all_cmds, CMD_DELIMITER); cmd_line != NULL; cmd_line = strtok(NULL, CMD_DELIMITER)){
               
                    // most specific - 6 parameters, 2 of which are verified
                    if(sscanf("%d%c%d%c%c%s", &number, &commatilde, &number_b, &symbol, &letter, string) == 6
                        && (commatilde == ',' || commatilde == '~')
                        && symbol == '!'){
                           
                        if(letter = CMD_APPENDS && (
                            (commatilde == ',' && number < line_number || line_number > number_b)
                            || (commatilde == '~' && (number != line_number && (line_number - number % number_b) > 0))
                        )){
                            strcat(buffer, "\n");
                            strcat(buffer, string);
                        }
                       
                        if(letter = CMD_REPLACE && (
                            (commatilde == ',' && number < line_number || line_number > number_b)
                            || (commatilde == '~' && (number != line_number && (line_number - number % number_b) > 0))
                        )){
                            strcpy(buffer, string);
                        }
                       
                        if(letter = CMD_INSERT && (
                            (commatilde == ',' && number < line_number || line_number > number_b)
                            || (commatilde == '~' && (number != line_number && (line_number - number % number_b) > 0))
                        )){
                            strcat(string, "\n");
                            strcat(string, buffer);
                            strcpy(buffer, string);
                        }
                   
                    // this is next cause it's 5, 2 of which are verified.
                    }else if(sscanf("%d%c%d%c%c", &number, &commatilde, &number_b, &symbol, &letter) == 5
                        && (commatilde == ',' || commatilde == '~')
                        && symbol == '!'){
                   
                        if(letter = CMD_PRINT && (
                            (commatilde == ',' && number < line_number || line_number > number_b)
                            || (commatilde == '~' && (number != line_number && (line_number - number % number_b) > 0))
                        )){
                            printf("%s", buffer);
                        }
                       
                        if(letter = CMD_DELETE && (
                            (commatilde == ',' && number < line_number || line_number > number_b)
                            || (commatilde == '~' && (number != line_number && (line_number - number % number_b) > 0))
                        )){
                            skip_print = 1;
                        }
                   
                    // then this because it's also 5, 1 of which is verified.
                    }else if(sscanf("%d%c%d%c%s", &number, &commatilde, &number_b, &letter, string) == 5
                        && (commatilde == ',' || commatilde == '~')){   
                           
                        if(letter = CMD_APPENDS && (
                            (commatilde == ',' && number >= line_number && line_number <= number_b)
                            || (commatilde == '~' && (number == line_number || (line_number - number % number_b) == 0))
                        )){
                            strcat(buffer, "\n");
                            strcat(buffer, string);
                        }
                       
                        if(letter = CMD_REPLACE && (
                            (commatilde == ',' && number >= line_number && line_number <= number_b)
                            || (commatilde == '~' && (number == line_number || (line_number - number % number_b) == 0))
                        )){
                            strcpy(buffer, string);
                        }
                       
                        if(letter = CMD_INSERT && (
                            (commatilde == ',' && number >= line_number && line_number <= number_b)
                            || (commatilde == '~' && (number == line_number || (line_number - number % number_b) == 0))
                        )){
                            strcat(string, "\n");
                            strcat(string, buffer);
                            strcpy(buffer, string);
                        }
                   
                    // and then this which is 4 paramters, 1 of which is verified.
                    // this and the one below shouldn't conflict cause of the verification in the same position.
                    }else if(sscanf("%d%c%d%c", &number, &commatilde, &number_b, &letter) == 4
                        && (commatilde == ',' || commatilde == '~')){
                           
                        if(letter = CMD_PRINT && (
                            (commatilde == ',' && number >= line_number && line_number <= number_b)
                            || (commatilde == '~' && (number == line_number || (line_number - number % number_b) == 0))
                        )){
                            printf("%s", buffer);
                        }
                       
                        if(letter = CMD_DELETE && (
                            (commatilde == ',' && number >= line_number && line_number <= number_b)
                            || (commatilde == '~' && (number == line_number || (line_number - number % number_b) == 0))
                        )){
                            skip_print = 1;
                        }
                   
                    // and then this which is also 4, 1 of which is verified.
                    // this and the one above shouldn't conflict cause of the verification in the same position.
                    }else if(sscanf("%d%c%c%s", &number, &symbol, &letter, string) == 4
                        && symbol == '!'){
                   
                        if(letter == CMD_APPENDS && number != line_number){
                            strcat(buffer, "\n");
                            strcat(buffer, string);
                        }
                       
                        if(letter == CMD_REPLACE && number != line_number){
                            strcpy(buffer, string);
                        }
                       
                        if(letter == CMD_INSERT && number == line_number){
                            strcat(string, "\n");
                            strcat(string, buffer);
                            strcpy(buffer, string);
                        }
                   
                    // this one's three paramters, one which we verify.
                    }else if(sscanf("%d%c%c", &number, &symbol, &letter) == 3
                        && symbol == '!'){
                           
                        if(letter == CMD_PRINT && number != line_number){
                            printf("%s", buffer);
                        }
                       
                        if(letter == CMD_DELETE && number != line_number){
                            skip_print = 1;
                        }
                   
                    // and this one is three, none which we can verify, ergo, less specific.
                    }else if(sscanf("%d%c%s", &number, &letter, string) == 3){
                       
                        if(letter == CMD_APPENDS && number == line_number){
                            strcat(buffer, "\n");
                            strcat(buffer, string);
                        }
                       
                        if(letter == CMD_REPLACE && number == line_number){
                            strcpy(buffer, string);
                        }
                       
                        if(letter == CMD_INSERT && number == line_number){
                            strcat(string, "\n");
                            strcat(string, buffer);
                            strcpy(buffer, string);
                        }
                       
                    // this one is two. check the number is > 0 can be a verification, although
                    // it's not strictly necessary.
                    }else if(sscanf("%d%c", &number, &letter) == 2
                        && number > 0){
                           
                        if(letter == CMD_PRINT && number == line_number){
                            printf("%s", buffer);
                        }
                       
                        if(letter == CMD_QUIT && number == line_number){
                            program_mode = MODE_END;
                        }
                       
                        if(letter == CMD_DELETE && number == line_number){
                            skip_print = 1;
                        }

                    // this one is also two, and we can verify the symbol.
                    }else if(sscanf("%c%c", &symbol, &letter) == 2
                        && symbol == '!'){
                   
                        if(letter == CMD_PRINT){
                            // well, if we don't print every line an additional time,
                            // then it just does what it normally does so this doesn't
                            // actually need to do anything?
                        }
                       
                        if(letter = CMD_DELETE){
                            // likewise, if we don't delete every line, then it acts
                            // normally and again, this doesn't do anything.
                            // I don't actually understand the point in !p and !d.
                        }
                   
                    // and this one is also two, with no verification.
                    }else if(sscanf("%c%s", &letter, string) == 2){
                   
                        if(letter == CMD_APPENDS){
                            strcat(buffer, "\n");
                            strcat(buffer, string);
                        }
                       
                        if(letter == CMD_REPLACE){
                            strcpy(buffer, string);
                        }
                       
                        if(letter == CMD_INSERT){
                            strcat(string, "\n");
                            strcat(string, buffer);
                            strcpy(buffer, string);
                        }   
                   
                    // and this one is one paramter, being least specific.
                    }else if(sscanf("%c", &letter) == 1){
                       
                        if(letter == CMD_PRINT){
                            printf("%s", buffer);
                        }
                       
                        if(letter == CMD_QUIT){
                            program_mode = MODE_END;
                        }
                       
                        if(letter == CMD_DELETE){
                            skip_print = 1;
                        }
                       
                    }
                   
                    if(skip_print == 0){
                        printf("%s", buffer);
                    }
                }
               
            }
        } while(program_mode != MODE_END);
       
        return 0;
    }


    Deletions:
    See B142LCoursework2Tasklist




    Revision [203]

    Edited on 2008-10-23 10:00:16 by cmantito

    Additions:
    See B142LCoursework2Tasklist


    Deletions:
    See B142LCoursework2Tasklist




    Revision [202]

    Edited on 2008-10-23 10:00:10 by cmantito

    Additions:
    See B142LCoursework2Tasklist




    Revision [137]

    Edited on 2008-10-19 08:11:12 by cmantito

    Additions:
    Categories: CategoryUni


    Deletions:
    CategoryUni




    Revision [104]

    The oldest known version of this page was edited on 2008-10-19 08:00:53 by cmantito

    CategoryUni
    Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki