예제 #1
0
/**
 * "readInput(FILE *fp, char src[])"
 * read each individual character into array passed from main
 * input file will be then store for use by other functions
 *
 */
void readInput(FILE *fp, char src[]){
    
    int i = 0;
    int c;
    int rc = 0;
    
    if (fp == NULL || src == NULL) {
        //if the file pointer or the char array is null, return error
        return;
    }
    
    // declare char array to store file input
    
    // return to the begining of file reader pointer
    rc = fseek(fp, 0L, SEEK_SET);
    if (rc != 0) {
        // error ocurred
        fileReadError(FNS[input_txt], input_txt);
        // this is a fatal error
        printError(err27, FNS[input_txt]);
        exit(EXIT_FAILURE);
    }
    // store each character into -array passed- from main
    while ( (c = fgetc(fp)) != EOF ){
        src[i++] = c;
    }
    
    return;
}
예제 #2
0
파일: utils.c 프로젝트: brahle/swSharp
extern char* fileReadToBuffer(char* filePath) {

    FILE* file = fileOpen(filePath);
    int fileLength = fileGetLength(file);

    char* fileBuffer = (char*) malloc(sizeof(char) * (fileLength + 1));
    fileBuffer[fileLength] = '\0';

    int bytesRead = fread(fileBuffer, 1, fileLength, file);
    if (bytesRead == -1) {
        fileReadError(filePath);
    }

    fclose(file);

    return fileBuffer;
}
예제 #3
0
/*
 * "createFilePointers()"
 * create all the file pointers and initialize them
 * file pointers are stored in an array for global use
 * handle null pointer exeptions immediatly
 */
void createFilePointers(){
    
    char *mode = "r";
    int i = 0;
    for (i = 0; i < MAX_FILES; i++) {
        // initialize the file pointer array index
        m_FPS[i] = NULL;
        
        if (i) {
            mode = "w";
        }
        // if file pointer fails to open filename
        // then a null is stored into array
        FILE *fp = NULL;
        fp = fopen(FNS[i], mode);
        if (fp == NULL) {
            fileReadError(FNS[i], i);
        }
        m_FPS[i] = fp;
    }
}
예제 #4
0
// -----------------Initial call to program  -----------------
int main(int argc, char *argv[]) {
    
    // if a file name for input is passed
    // use that name instead of the default in
    // FNS[0] = input.txt
    
    int i = 0;
    
    if(argc > 1) {
        FNS[input_txt] = argv[1];
    }
    
    // create file pointers for input output
    // createFilePointers handles null pointer exception
    createFilePointers();
    // copy input file pointer
    FILE *ifp = m_FPS[input_txt];
    // copy clean input file pointer
    FILE *cifp = m_FPS[cleaninput_txt];
    
    
    // how many characters in file
    int count = charCount(ifp);
    if (count < 0) {
        fileReadError(FNS[input_txt], input_txt);
        // this is fatal error
        exit(EXIT_FAILURE);
    }
    
    char code[count];
    // cleanCode will have input without comments
    char cleanCode[count];
    // initialize code arrays
    for (i = 0; i < count; i++) {
        code[i] = ' ';
        cleanCode[i] = ' ';
    }
    
    // read input file into array code[]
    readInput(ifp, code);
    // close the input file
    fclose(ifp);
    
    
    // remove comments from input
    //cleanCode[] will contain -comments free- input
    cleanInput(cifp, code, count, cleanCode);
    // close the -clean input- file
    fclose(cifp);
    
    // there will be at most m_nCleanCount separate string tokens
    char *caCleanInputTokens[m_nCleanCount];
    // separate cleanCode into tokens, allocate space as needed with calloc
    // need to free each caCleanInputTokens[] array index that calloc was done to
    // if caCleanInputTokens[] is not null, free it before exiting program
    // or after printing it to file
    splitInputTokens(cleanCode, caCleanInputTokens);
    
    // there will be at most m_nCleanCount separate namerecord_t tokens
    namerecord_t namerecord_table[m_nCleanCount];
    initializeNamerecord_table(namerecord_table);
    
    // identify what kind of lexeme each token is
    IdentifyInputToken(caCleanInputTokens, namerecord_table);
    
    // print each record to both lexemelist.txt and lexemetable.txt
    printNamerecord_table(namerecord_table);
    
    // call freeInputTokenCalloc after finishing the use of the array
    freeInputTokenCalloc(caCleanInputTokens);
    
    // program finished, if no error were found, it will reach this point
    printf("\nYou can find the input/ output files: \n");
    for (i = 0; i < MAX_FILES; i++) {
        // print the file names that were used and created
        printf("%s \n", FNS[i]);
        
    }
    printf("in the same folder where scanner.c is located \n\n");
    
    return 0;
}