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);
}
//-----------------------------------------------------------------------------