Example #1
0
// 3(X2(B)Y)
//  ^
//   get -> X
// A-Z -> expr
// 2(A-Z) -> expr(A-Z, A-Z)
// ) return
void decompress(ifstream& input, queue<char>& expr) {
  while (input) {
    char c;
    input.get(c);
    if (')' == c) {
      return;

    } else if (c == '\n') {
      expr.push(c);

    } else if ('A' <= c && c <= 'Z') {
      expr.push(c);

    } else if ('0' <= c && c <= '9') {
      int repeat = c - '0';
      while (true) {
        input.get(c);
        if (c == '(') {
          break;
        }
        repeat *= 10;
        repeat += c - '0';
      }

      queue<char> internalRepeat;
      decompress(input, internalRepeat);

      while (repeat--) {
        queue<char> temp = internalRepeat;
        while (!temp.empty()) {
          char repeatChar;
          temp.pop(repeatChar);
          expr.push(repeatChar);
        }
      }
    }
  }
}
Example #2
0
/**
   Utility function to read in a raw 4-byte integer.
   This assumes the host byteorder is LSB first.
*/
static unsigned int rawLong( ifstream &stream ) {
    unsigned int val = 1;
  
    // Get a pointer into val.
    char *p = (char *)&val;

    // Test the byte order (stupid to do this every time)
    if ( *p == 1 ) {
        // Read each of the bytes of val.
        stream.get( p[ 0 ] );
        stream.get( p[ 1 ] );
        stream.get( p[ 2 ] );
        stream.get( p[ 3 ] );
    } else {
        stream.get( p[ 3 ] );
        stream.get( p[ 2 ] );
        stream.get( p[ 1 ] );
        stream.get( p[ 0 ] );
    }
  
    // Return the result.
    return val;
}
Token Lexer::Get() {
	if (ifs.good()) {
		char c = ifs.get();
		// it's a valid token
		Token t = Token();
		t.value = c;
		return t;
	}
	else {
		Token t = Token();
                t.empty = true;
                return t;
	}
}
 void start_thread(int num) {
   cerr << "starting worker " << num << endl;
   char buffer[mem_each*1024];
   //cerr << "stream good: " << input.good() << endl;
   input.get(buffer, mem_each*1024, EOF);
   //cerr << "Buffer: " << string(buffer) << endl;
   maps[num] = unordered_map<string, int>();
   //cerr << "Map clean: " << maps[num].empty() << endl;
   workers[num] = thread(wc, string(buffer), ref(maps[num]));
   if (input.eof()) {
     done = true;
     cerr << "End of file" << endl;
   }
 }
Example #5
0
//Function that makes a map from all potential seeds of length specified by order to a vector of all characters that follow the after the seed in the text.
void makeMap(ifstream& sourceText, int order, Map<string, Vector<char> >& possibleNextChars){
    char ch;
    string seed;
    
    //Get the first letters of number equal to order from the text.
    for(int i = 0; i < order; i++){ 
        ch = sourceText.get();
        seed += ch;
    }
    
    seed = toLowerCase(seed);
    
    //Get every other possible seed in text and map them to vector of chars that follow them.
    while(true){
        int next = sourceText.get();
        if(next == EOF) break;
        ch = next;
        seed = toLowerCase(seed);
        possibleNextChars[seed] += ch;
        seed = seed.substr(1,seed.length()-1) + ch;
    }

}
Example #6
0
//chete 32bitovo pololijelno chislo 
unsigned int read32bit_integer(ifstream& reader){

		char bytes[4];
		for(int i = 0; i<4; i++){
				bytes[i] = reader.get();
		}
		unsigned int result = 0;
		for(int i = 0; i<4; i++){
				result = (result << 8) | ((unsigned char) bytes[i]);
		}

		//cout<<result<<endl;
		return result;
}
Example #7
0
string read_quoted_str(ifstream &fin)
{
  string r;
  int c;
  for(;;)
    {
      c=fin.get();
      if(c==EOF)
	assert(0);
      if(c=='"')
	break;
    }
  for(;;)
    {
      c=fin.get();
      if(c=='"')
	break;
      if(c==EOF)
	assert(0);
      r+=c;
    }
  return r;
}
Example #8
0
int UserList::load( ifstream& fp)
{
  int		type;
  int		end_found = 0;
  char		dummy[40];

  for (;;)
  {
    fp >> type;

    switch( type) {
      case user_eData_User: break;
      case user_eData_UserName:
        fp.get();
        fp.getline( name, sizeof(name));
        break;
      case user_eData_UserPassword:
        fp.get();
        fp.getline( password, sizeof(password));
        break;
      case user_eData_UserPrivilege: fp >> priv; priv = idecrypt( priv); break;
      case user_eData_UserId: fp >> id; break;
      case user_eData_UserFullName:
        fp.get();
        fp.getline( fullname, sizeof(fullname));
        break;
      case user_eData_UserDescription:
        fp.get();
        fp.getline( description, sizeof(description));
        break;
      case user_eData_UserEmail:
        fp.get();
        fp.getline( email, sizeof(email));
        break;
      case user_eData_UserPhone:
        fp.get();
        fp.getline( phone, sizeof(phone));
        break;
      case user_eData_UserSms:
        fp.get();
        fp.getline( sms, sizeof(sms));
        break;
      case user_eData_End: end_found = 1; break;
      default:
        cout << "User:open syntax error" << endl;
        fp.getline( dummy, sizeof(dummy));
   }
   if ( end_found)
     break;
  }
  return 1;
}
Example #9
0
//
// Name: readLineForDebug()
// Discription: This will read in an entire line and save it.  It will then move
//              The stream back to the start of the line.  Used for debugging
//              instructions.
// Parameters: ifstream &stream - The stream to read from.
// Returns: void
//
void Instruction::readLineForDebug(ifstream &stream) {
    string s;
    while (!stream.eof()) {
        if (stream.peek() == endOfInstruction || stream.peek() == EOF)
            break;
        char c = stream.get();
        if (c == '\n')
            break;
        lineOfCode += c;
    }
    stream.seekg(((int)stream.tellg())-lineOfCode.size());
    lineOfCode = typeToString(type) + lineOfCode;
    cerr << lineNumber << " " << lineOfCode << endl;
    lineOfCode = "Smada Instruction: " + lineOfCode;
}
int readLineFromFile(vector<int>& dataLineVec, u_int& dataVecLen){
	if(inpFileStream.eof()) return 0;
	char c = inpFileStream.get();
	int no;
	dataVecLen = 0;
	dataLineVec.clear();
	while( c != '\n' && !inpFileStream.eof()){
		if( c == ' ')
		c = inpFileStream.get();
		no = 0;
		bool empty = true;
		while(c>='0' && c<='9'){
			empty = false;
			no *= 10;
			no += (c - '0');
			c = inpFileStream.get();
		}
		if(!empty){
			dataLineVec.push_back(no);
			++dataVecLen;
		}
	}
	return 1;
}
Example #11
0
// Literals (char, string)
// Make sure that <,>, etc are escaped inside literals!
void handle_literal(char delimiter, ifstream& inFile, ofstream& outFile) {
	
	outFile << "<font color=\"gray\">";	
	outFile.put(delimiter);
	
	char ch;
	
	while(inFile.get(ch)) {
		
		if(ch == delimiter) {
			outFile.put(ch);
			break;
		}
		else if(ch == '\\'){
			outFile.put(ch);		// TEST
			inFile.get(ch) && outFile.put(ch);
		}
		else
			outputChar(ch, outFile);
	}
	
	outFile << "</font>";
	
}
void invert_image(ifstream &infile, ofstream &outfile, BITMAPFILEHEADER &file_header, BITMAPINFOHEADER &info_header)
{
	RGBQUAD	pixel = { 0, 0, 0};
	long	size;	//Size of the file
	
	//Getting the size of the stream
	infile.seekg(0, infile.end);
	size = infile.tellg();							
	
	//Outputting the file headers													
	outfile.write(reinterpret_cast<char*>(&file_header), sizeof(file_header));
	outfile.write(reinterpret_cast<char*>(&info_header), sizeof(info_header));
		
			
	infile.seekg(54, infile.beg);		//Moving stream to location 54 to avoid file header values

	for (int i = 54; i < size; i++)		//Loop through all of the pixel data
	{
		pixel.rgbBlue	  = infile.get();	//Read in pixel value
		pixel.rgbGreen	  = infile.get();
		pixel.rgbRed	  = infile.get();
		

		pixel.rgbBlue	  = 255 - pixel.rgbBlue;	//Inverts the colour
		pixel.rgbGreen	  = 255 - pixel.rgbGreen;
		pixel.rgbRed	  = 255 - pixel.rgbRed;
		

		outfile.write(reinterpret_cast<char*>(&pixel), 3);
	}

	/*Closing the io streams and ending the program*/
	infile.close();
	outfile.close();
	cout << "\nThe operation is completed\nThe file has been closed...\n";
}
Example #13
0
void space_remover(ifstream& in_stream, ofstream& out_stream)
{
	char current_char;
	bool shouldContinue = true;

	do {
		in_stream.get(current_char);

		if (current_char == ' ') {
			out_stream.put(current_char);

			do {
				in_stream.get(current_char);
			} while (current_char == ' ');

			out_stream.put(current_char);

		} else if (!in_stream.eof()) {
			out_stream.put(current_char);
		} else {
			shouldContinue = false;
		}
	} while (shouldContinue == true);
}
Example #14
0
int SystemList::load( ifstream& fp)
{
  int		type;
  int		end_found = 0;
  char		dummy[40];

  for (;;)
  {
    fp >> type;

    switch( type) {
      case user_eData_SystemName:
        fp.get();
        fp.getline( name, sizeof(name));
        break;
      case user_eData_SystemLevel: fp >> level; break;
      case user_eData_SystemAttributes: fp >> attributes; break;
      case user_eData_SystemId: fp >> id; break;
      case user_eData_SystemDescription:
        fp.get();
        fp.getline( description, sizeof(description));
        break;
      case user_eData_User:
        load_user( fp); break;
      case user_eData_System:
        load_system( fp); break;
      case user_eData_End: end_found = 1; break;
      default:
        cout << "System:open syntax error" << endl;
        fp.getline( dummy, sizeof(dummy));
   }
   if ( end_found)
     break;
  }
  return 1;
}
Example #15
0
/**
 * \brief Read single single symbol into the end of a string buffer.
 * \param fin The input file stream.
 * \param str The string buffer.
 */
void readsinglesymbol(ifstream& fin, string& str)
{
  char currentchar;
  fin.get(currentchar);
  if (fin.eof()) {
    return;
  }
  if (currentchar == '\"') {
    // read a string literal
    do {
      str += currentchar;
      fin.get(currentchar);
    } while (currentchar != '\"');
    str += currentchar;
  } else {
    do {
      str += currentchar;
      fin.get(currentchar);
    } while ((false == iswhitespace(currentchar)) 
	     && ('(' != currentchar) 
	     && (false == fin.eof()));
    fin.putback(currentchar);  
  }
}
Example #16
0
bool LeerCabecera (ifstream& f, int& filas, int& columnas)
{
   int maxvalor;

   while (SaltarSeparadores(f)=='#')
   f.ignore(10000,'\n');

   f >> columnas >> filas >> maxvalor;
   
   if (/*str &&*/ f && filas>0 && filas <5000 && columnas >0 && columnas<5000) {
      f.get(); // Saltamos separador
      return true;
   }
   else return false;
}
Example #17
0
char Scanner::NextChar()
{
	char c;

	sourceFile.get(c);
	if (c == '\n')
	{
		listFile.width(6);
		listFile << ++lineNumber << "  " << lineBuffer << endl;
		lineBuffer = "";
	}
	else
		lineBuffer += c;
	return c;
}
Example #18
0
int ParameterList::readNext(ifstream in,char * tmp){
	int tmp_lenght;
	//remove all leading spaces
	while(in.peek()==32) in.get();
	//check if end of line
	if((in.peek()==10 || in.peek()==13)) 
		return-1;

	in>>tmp;
	
	if(in.eof()) return -1;
	tmp_lenght=strlen(tmp);
	// look for comment sign
	if((int)strcspn(tmp,"#")<tmp_lenght){
		//found comment: read till the end and go to next line
		while(!(in.peek()==10 || in.peek()==13)) in.get();
		//remove end of line
		while((in.peek()==10 || in.peek()==13)) in.get();
		return -1;
	}
	return 1;


}
// herbert: fixed TestComment
void SplineGeometry2d :: TestComment ( ifstream & infile )
{
    bool comment = true;
    char ch;
    while ( comment == true && !infile.eof() ) {
        infile.get(ch);
        if ( ch == '#' ) { // skip comments
            while (  ch != '\n' && !infile.eof() ) {
                infile.get(ch);
            }
        }
        else if ( ch == '\n' )  { // skip empty lines
            ;
        }
        else if ( isspace(ch) ) { // skip whitespaces
            ;
        }
        else { // end of comment
            infile.putback(ch);
            comment = false;
        }
    }
    return;
}
Example #20
0
void get_line(ifstream &fs, vector<char> &buf)
{
    buf.clear();
    buf.reserve(4096);
    while(!fs.eof()) {
        char ch = fs.get();
        if(ch == '\n' || ch == '\377')
	    break;
        if(ch == '\r') 
	    continue;
        buf.push_back(ch);
    }
    buf.push_back('\0');
    return;
}
Example #21
0
//Read the data of a student (name and poll) from list_students
//and stores it in list, using the poll as key.
void read_student(ifstream& list_students, map<Poll, vector<string> >& list) {
  char current_name[SIZE];
  Poll current_poll;
  
  list_students.get(current_name, SIZE, ',');
  list_students.ignore(1); //Skip the ','
  
  current_poll = read_poll(list_students);
  
  //Set flags so that good() works correctly
  list_students.peek();
  
  //Add this name to the list of students whose answer is this poll
  list[current_poll].push_back(string(current_name));
}
Example #22
0
void GrowAxisArc::open( ifstream& fp)
{
  int		type;
  int 		end_found = 0;
  char		dummy[40];
  int           tmp;

  for (;;)
  {
    if ( !fp.good()) {
      fp.clear();
      fp.getline( dummy, sizeof(dummy));
      printf( "** Read error GrowAxisArc: \"%d %s\"\n", type, dummy);      
    }

    fp >> type;
    switch( type) {
      case glow_eSave_GrowAxisArc: break;
      case glow_eSave_GrowAxisArc_max_value: fp >> max_value; break;
      case glow_eSave_GrowAxisArc_min_value: fp >> min_value; break;
      case glow_eSave_GrowAxisArc_arc_part: 
        GrowArc::open( fp);
        break;
      case glow_eSave_GrowAxisArc_lines: fp >> lines; break;
      case glow_eSave_GrowAxisArc_linelength: fp >> linelength; break;
      case glow_eSave_GrowAxisArc_longquotient: fp >> longquotient; break;
      case glow_eSave_GrowAxisArc_valuequotient: fp >> valuequotient; break;
      case glow_eSave_GrowAxisArc_format:
        fp.get();
        fp.getline( format, sizeof(format));
        break;
      case glow_eSave_GrowAxisArc_text_size: fp >> text_size; break;
      case glow_eSave_GrowAxisArc_text_drawtype: fp >> tmp; text_drawtype = (glow_eDrawType)tmp; break;
      case glow_eSave_GrowAxisArc_text_color_drawtype: fp >> tmp; text_color_drawtype = (glow_eDrawType)tmp; break;
      case glow_eSave_GrowAxisArc_userdata_cb:
	if ( ctx->userdata_open_callback)
	  (ctx->userdata_open_callback)(&fp, this, glow_eUserdataCbType_Node);
	break;
      case glow_eSave_End: end_found = 1; break;
      default:
        cout << "GrowAxisArc:open syntax error" << endl;
        fp.getline( dummy, sizeof(dummy));
    }
    if ( end_found)
      break;
  }
  configure();
}
Example #23
0
// this is already inside the loop E'--> +TE'|-TE'| e
int Exp2(int input){
	
	int result = input;
	char a;
	if (!fin.eof() ){
		fin.get(a);
	//if( (a = fin.get()) != fin.eof()){ // just another way i could have written this. works for my notes.
		if (a == '+')
			result = Exp2(result + Term() );
		else if(a == '-')
			result = Exp2(result - Term() );
		else if(a == ')')
			fin.putback(a);
	}
	return result;
}
void writeOutput(ifstream &infile, ofstream &outfile, int shift)
{
	char ch;
	int asciiCode = 0;
	
	while (infile.peek() != EOF) { //Until it is the end of the file...
		infile.get(ch); //Get the next character
		if (ch >= 'A' && ch <= 'z') //If the character is in the alphabet...
		{
			asciiCode = static_cast<int>(ch); //Change it to the ASCII number
			asciiCode += shift; //Do the shift
			ch = static_cast<char>(asciiCode); //Change it to the shifted letter
		}
		outfile << ch; //Print to the outfile
	}
}
Example #25
0
bool HuffmanEncoder::encodeFile(ifstream &fileIn, BitWriter &fileOut) const
{
	if(!fileIn.is_open() || !fileOut.is_open())
	{
		return false;
	}
	writeHeader(fileOut);
	while(!fileIn.eof())
	{
		char c;
		fileIn.get(c);
		int ind = (unsigned char) c;
		fileOut.Add(mapping[ind], mapBits[ind]); //encoding, num bits
	}
	return true;
}
int Term2(int inp)
{
  int result = inp;
  char a;
  testfile.get(a);
  if(a != EOF)
  {
    if (a == '*')
      result = Term2(result * Fact());
    else if (a == '/')
      result = Term2(result / Fact());
    else if (a == '+' || a == '-')
      testfile.putback(a);
  }
  return result;
}
Example #27
0
static Big hash(ifstream &fp)
{ /* compute hash function */
    char ch,s[20];
    Big h;
    sha sh;
    shs_init(&sh);
    forever 
    { /* read in bytes from message file */
        fp.get(ch);
        if (fp.eof()) break;
        shs_process(&sh,ch);
    }
    shs_hash(&sh,s);
    h=from_binary(20,s);
    return h;
}
Example #28
0
char ConfFile::next_char(ifstream& fs)
{
  char ch;
  bool ready=false;
  while (!ready){
    ready=true;
    if (fs.get(ch)){
      if ((ch==0x0a)||(ch==0x0d)){
        if (eol_char==0) eol_char=ch;
        if (ch==eol_char) ++line_num; else ready=false;
      }
    }
    else ch=0;
  }
  return ch;
}
Example #29
0
void getch()
{
    if(!infile.eof()){
        infile.get(ch);
        if(ch == '\n'){
            line__++;
            pos__ = 0;
        }
        else{
            pos__++;
        }
    }
    else{
        exit(1);                //文件读完,结束
    }
}
Example #30
0
/** DEPRECATED: slow */
bool field_value_t::load_value_from_file(ifstream & is,
                                         const char delim)
{
    assert (_pfield_desc);

    char* string = new char [10*_pfield_desc->fieldmaxsize()];
    is.get(string, 10*_pfield_desc->fieldmaxsize(), delim);
    if (strlen(string) == 0) {
        delete [] string;
        return false;
    }

    if (strcmp(string, "(null)") == 0) {
        assert(_pfield_desc->allow_null());
        _null_flag = true;
        delete [] string;
        return true;
    }

    _null_flag = false;

    switch (_pfield_desc->type()) {
    case SQL_BIT:       _value._bit = atoi(string); break;
    case SQL_SMALLINT:  _value._smallint = atoi(string); break;
    case SQL_CHAR:      _value._char = atoi(string); break;
    case SQL_INT:       _value._int = atoi(string); break;
    case SQL_FLOAT:     _value._float = atof(string); break;
    case SQL_LONG:     _value._float = atol(string); break;
    case SQL_TIME:      break;
    case SQL_VARCHAR:   {
        if (string[0] == '\"') string[strlen(string)-1] = '\0';
        set_var_string_value(string+1, strlen(string)-1);
        break;
    }
    case SQL_FIXCHAR:  {
        if (string[0] == '\"') string[strlen(string)-1] = '\0';
        set_fixed_string_value(string+1, strlen(string)-1);
        break;
    } 
    case SQL_NUMERIC:
    case SQL_SNUMERIC:
        set_fixed_string_value(string, strlen(string));
        break;
    }
    delete [] string;
    return true;
}