예제 #1
0
파일: cptest2.c 프로젝트: Spkordell/cs2303
/** Copies one file to another using raw unformatted I/O, one byte at a time.
 * @param infilenames Name of input file
 * @param outfilename Names of output file
 * @param fileCount Number of input files to copy
 * @param bufferSize Size of the copy buffer
 * @return 0 if successful, 1 if error.
 * @author Steven Kordell
*/
int copyfile3(char** infilenames, char* outfilename, int fileCount, int bufferSize) {
  int infile;
  int outfile;
  int sizeRead;
  DIR* dir;
  int i; //an iterator

  for (i = 0; i < fileCount; i++) {

    //make a buffer to hold the copied data
    unsigned char* buffer = (unsigned char*) malloc(sizeof(unsigned char)*bufferSize);
    if(buffer == NULL) {
      printf("Error: Buffer exceeds available memory.\n");
      return -1;
    }
    
    //open file for reading
    infile = open(infilenames[i],O_RDONLY);
    if (infile == -1) {
      open_file_error(infilenames[i]);
      return 1;
    }
    
    // Open or create file for writing
    dir = opendir(outfilename); //try to open the directory
    if (dir == NULL) {
      //the directoy doesn't exist, copy the file to a new name in the current directory
      outfile = open(outfilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
    } else {
      //the directory does exist, copy the file with the same name to the directory
      char* tempStr = (char*) malloc(sizeof(char)*(strlen(outfilename)+3+strlen(basename(infilenames[i]))));
      strcat(tempStr,"./"); 
      strcat(tempStr,outfilename);
      strcat(tempStr,"/");
      strcat(tempStr,basename(infilenames[i]));
      outfile = open(tempStr, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
      free(tempStr);
    }
    
    if (outfile == -1) {
      open_file_error(outfilename);
      return 1;
    }
    
    while((sizeRead = read(infile,buffer,bufferSize)) > 0) { //read each character, checking for EOF
      write(outfile,buffer,sizeRead); //Write the character to the output file
    }

    //close the files
    close(infile);
    close(outfile);
    closedir(dir);
    //free the buffer
    free(buffer);

  }

  return 0;
}
예제 #2
0
파일: cptest2.c 프로젝트: Spkordell/cs2303
/** Copies one file to another using formatted I/O, one character at a time.
 * @param infilenames Names of input files
 * @param outfilename Name of output file
 * @param fileCount Number of input files to copy
 * @return 0 if successful, 1 if error.
*/
int copyfile1(char** infilenames, char* outfilename, int fileCount) {
  FILE* infile; //File handles for source and destination.
  FILE* outfile;
  DIR* dir;
  int i; //an iterator

  //Loop through the array, copying all the files
  for (i = 0; i < fileCount; i++) {


    // Open the input file.
    infile = fopen(infilenames[i], "r");
    if (infile == NULL) {
      open_file_error(infilenames[i]);
      return 1;
    }
    
    //open the output file
    dir = opendir(outfilename); //try to open the directory
    if (dir == NULL) {
      //the directoy doesn't exist, copy the file to a new name in the current directory
      outfile = fopen(outfilename, "w");
    } else {
      //the directory does exist, copy the file with the same name to the directory
      char* tempStr = (char*) malloc(sizeof(char)*(strlen(outfilename)+3+strlen(basename(infilenames[i]))));
      strcat(tempStr,"./"); 
      strcat(tempStr,outfilename);
      strcat(tempStr,"/");
      strcat(tempStr,basename(infilenames[i]));
      outfile = fopen(tempStr, "w");
      free(tempStr);
    }
    if (outfile == NULL) {
      open_file_error(outfilename);
      return 1;
    }
    
    int intch;  // Character read from input file. must be an int to catch EOF.
    unsigned char ch; // Character stripped down to a byte.
    
    // Read each character from the file, checking for EOF.
    while ((intch = fgetc(infile)) != EOF) {
      ch = (unsigned char) intch; // Convert to one-byte char.
      fputc(ch, outfile); // Write out.
    }
    
    // All done--close the files
    fclose(infile);
    fclose(outfile);
    closedir(dir);
    
  }
  return 0; // Success!
}
예제 #3
0
파일: cptest2.c 프로젝트: Spkordell/cs2303
/** Copies one file to another using raw unformatted I/O, one byte at a time.
 * @param infilenames Name of input file
 * @param outfilename Names of output file
 * @param fileCount Number of input files to copy
 * @return 0 if successful, 1 if error.
 * @author Steven Kordell
*/
int copyfile2(char** infilenames, char* outfilename, int fileCount) {
  int infile;
  int outfile;
  DIR* dir;
  int i; //an iterator

  for (i = 0; i < fileCount; i++) {

    //open file for reading
    infile = open(infilenames[i],O_RDONLY);
    if (infile == -1) {
      open_file_error(infilenames[i]);
      return 1;
    }
    
    
    // Open or create file for writing
    dir = opendir(outfilename); //try to open the directory
    if (dir == NULL) {
      //the directoy doesn't exist, copy the file to a new name in the current directory
      outfile = open(outfilename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
    } else {
      //the directory does exist, copy the file with the same name to the directory
      char* tempStr = (char*) malloc(sizeof(char)*(strlen(outfilename)+3+strlen(basename(infilenames[i]))));
      strcat(tempStr,"./"); 
      strcat(tempStr,outfilename);
      strcat(tempStr,"/");
      strcat(tempStr,basename(infilenames[i]));
      outfile = open(tempStr, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IXUSR);
      free(tempStr);
    }
    if (outfile == -1) {
      open_file_error(outfilename);
      return 1;
    }
    
    unsigned char ch[1]; //the character read from infile to be written to outfile
    
    while(read(infile,ch,1)) { //read each character, checking for EOF
      write(outfile,ch,1); //Write the character to the output file
    }
    
    //close the files
    close(infile);
    close(outfile);
    closedir(dir);
  }
  return 0;
}
// -------------------------------------------------------------------------- //
//
void ScalarDistributionData::weighted_analysis(const Library & library,
                                               const std::vector<BasisContainer> & weights_table,
                                               const std::string & basename) const
{
    // Setup distributions.
    std::vector<double> distribution(distribution_.size(), 0.0);
    std::vector<double> weighted_distribution(distribution_.size(), 0.0);

    // Loop through the library set and extract the scalar value.
    for (size_t i = 0; i < weights_table.size(); ++i)
    {
        const int index     = weights_table[i].index;
        const double weight = weights_table[i].weight;
        const double value  = library.get_scalar_at(index, pos_);

        // Calculate which bin this value belongs to.
        const int bin = get_bin(value);

        // Increment the distribution at this bin.
        distribution[bin] += 1.0;
        weighted_distribution[bin] += 1.0 * weight;
    }

    // Normalize the distributions to 1.
    const double norm = vsum(distribution);
    distribution = distribution / norm;
    weighted_distribution = weighted_distribution / norm;

    // Print scale unweighted and weighted to file.
    std::string filename = basename + "." + "Distribution-" + name_;
    std::ofstream outfile(filename.c_str());

    // Check that the file is ok.
    if (outfile.bad())
    {
        open_file_error(filename, LOCATION);
    }

    outfile << "        SCALE                DISTRIBUTION          WEIGHTED             REFERENCE" << std::endl;

    // Loop over bins.
    for (size_t i = 0; i < distribution.size(); ++i)
    {
        // Pull out the values.
        const double scale  = scale_[i];
        const double target = target_[i];
        const double value  = distribution[i];
        const double weighted_value = weighted_distribution[i];

        char line[300];
        sprintf(line, "%20.10f %20.10f  %20.10f %20.10f\n", scale, value, weighted_value, target);
        outfile << std::string(line);
    }
    outfile.close();
}
예제 #5
0
파일: lexical.c 프로젝트: pein0119/compiler
int main(int argc, char *argv[])
{
    FILE *fin, *fout;

    if(argc == 1)
    {
        // 错误:无输入文件
        error_handle(file_exist_error);
        return 0;
    }
    //打开源程序文件
    fin = fopen(argv[1],"r");

    if(fin ==  NULL)
    {
        open_file_error();
        return 0;
    }

    //打开文件保存词法分析结果
    fout = fopen("temp.txt","w");

    if(fout == NULL)
    {
        create_file_error();
        return 0;
    }
    // 建立关键字的哈希表
    int i;
    for(i = 0; i < 38; ++i)
        createHash(word[i],HashTable);

    //从源程序文件中取字符进行处理
    char ch;
    unsigned char flag = 1;
    char str[100];
    char *buffer = str;

    buffer = fgets(str,100,fin);
    char *begin,*forward;
    int length;
    char *end;


    while(!feof(fin))
    {
        begin = forward = buffer;
        length = strlen(buffer);
        end = buffer + length;
        row = 1;                        // 记录当前的列号

        while(forward < end)
        {
            begin = forward;
            ch = *forward;
            if( check_ch(ch)==0 )
            {
                error_handle(illegal_ch_error);
                ++forward;
                ++row;                  // 移到下一列
                continue;
            }

            if((ch == TAB || ch == NEWLINE || ch == SPACE))
            {
                ++forward;
                ++row;
                begin = forward;
                continue;
            }
            if(isalpha(ch))
            {
                ++forward;
                ++row;
                ch = *forward;

                // if( check_ch(ch) ==0)
                // {
                //     error_handle(illegal_ch_error);
                //     --forward;
                //     copytoken(begin,forward);
                //     ++forward;
                //     print_word(token);
                //     continue;
                // }

                while(isalnum(ch)&&(++row, ++forward != end))
                {
                    ch = *forward;
                }

                --forward;
                --row;
                copytoken(begin,forward);
                ++forward;
                ++row;
                print_word(token);
            }
            else if(isdigit(ch))
            {
                ++forward;
                ++row;
                ch = *forward;
                // if(check_ch(ch)==0)
                // {
                //     error_handle(illegal_ch_error);
                // }
                while(isdigit(ch)&&(++row, ++forward != end))
                {
                    ch = *forward;
                    // if( check_ch(ch) == 0)
                    //     error_handle(illegal_ch_error);
                }

                --forward;
                --row;
                copytoken(begin,forward);
                ++forward;
                ++row;
                print_digit(token);
            }
            else
            {
                switch(ch)
                {
                case '*':
                    ++forward;
                    ++row;
                    ch = *forward;
                    if( check_ch(ch)==0 )
                    {
                        // error_handle(illegal_ch_error);
                        // ++forward;
                        printf("(MULTI,0)\n");
                        break;
                    }

                    if(ch == '*')
                        printf("(EXP,0)\n");
                    else
                    {
                        --forward;
                        --row;
                        printf("(MULTI,0)\n");
                    }
                    ++forward;
                    ++row;
                    break;
                case ':':
                    ++forward;
                    ++row;
                    ch = *forward;
                    if( check_ch(ch)==0 )
                    {
                        // error_handle(illegal_ch_error);
                        // ++forward;
                        printf("(COLON,0)\n");
                        break;
                    }

                    if(ch == '=')
                        printf("(ASSIGN,0)\n");
                    else
                    {
                        --forward;
                        --row;
                        printf("(COLON,0)\n");
                    }
                    ++forward;
                    ++row;
                    break;
                case '<':
                    ++forward;
                    ++row;
                    ch = *forward;

                    if( check_ch(ch)==0 )
                    {
                        // error_handle(illegal_ch_error);
                        // ++forward;
                        printf("(LT,0)\n");
                        break;
                    }


                    if(ch == '=')
                    {
                        printf("(LE,0)\n");
                    }
                    else if(ch == '>')
                    {
                        printf("(NE,0)\n");
                    }
                    else
                    {
                        --forward;
                        --row;
                        printf("(LT,0)\n");
                    }
                    ++forward;
                    ++row;
                    break;
                case '=':
                    printf("(EQ,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '>':
                    ++forward;
                    ++row;
                    ch = *forward;
                    if( check_ch(ch)==0 )
                    {
                        // error_handle(illegal_ch_error);
                        // ++forward;
                        printf("(GT,0)\n");
                        break;
                    }

                    if(ch == '=')
                        printf("(GE,0)\n\n");
                    else
                    {
                        --forward;
                        --row;
                         printf("(GT,0)\n");
                    }
                    ++forward;
                    ++row;
                    break;
                case '+':
                    printf("(PLUS,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '-':
                    printf("(MINUS,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '/':
                    printf("(REIV,0)\n");
                    ++forward;
                    ++row;
                    break;
                case ',':
                    printf("(COMMA,0)\n");
                    ++forward;
                    ++row;
                    break;
                case ';':
                    printf("(SEMIC,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '(':
                    printf("(LR_BRAC,0)\n");
                    int flag = 0; // 用于判断括号是否匹配
                    char* temp;
                    temp = forward;
                    while(++temp != end)
                    {
                        if(ch == '(')
                            ++flag;
                        else if(ch == ')')
                            --flag;
                        if(flag < 0)
                            break;

                        ch = *temp;
                    }
                    if(flag != 0)
                        error_handle(par_not_match);
                    ++forward;
                    ++row;
                    break;
                case ')':
                    printf("(RR_BRAC,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '[':
                    printf("(LS_BRAC,0)\n");
                    ++forward;
                    ++row;
                    break;
                case ']':
                    printf("(RS_BRAC,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '\'':
                    // printf("(Q_MARK,0)\n");
                    ++forward;
                    ++row;
                    ch = *forward;

                    while(ch != '\''&& (++row, ++forward != end))
                    {
                        ch = *forward;
                        if(!check_ch(ch))
                            error_handle(illegal_ch_error);
                        // ++forward;
                    }
                    // --forward;
                    if(ch == '\'')
                    {
                        copytoken(begin, forward); // 输出字符串
                        print_str(token);
                    }
                    else if(forward == end)
                    {
                        error_handle(quo_not_match); // 引号不匹配
                    }
                    ++forward;
                    ++row;
                    break;
                case '.':
                    printf("(F_STOP,0)\n");
                    ++forward;
                    ++row;
                    break;
                case '^':
                    printf("(CAP,0)\n");
                    ++forward;
                    ++row;
                    break;
                default:
                    // error_handle();
                    break;
                }
            }
        }
        // 再从文件中读入一行数据
        buffer = fgets(str,100,fin);
        ++line;
    }
    //关闭源程序和结果文件
    fclose(fin);
    fclose(fout);
    return 0;
}