// with file void Cal::getLines(const char *name_file) { std::ifstream fd (name_file); std::string line; if (fd.is_open()) { while (std::getline (fd,line)) { if (line == ";;" || line == "^D") break; this->_lines->push_back(line); } if (this->_lines->size() == 0) { std::stringstream ss; ss << "Error: instructions not found."; throw ft_exception(ss.str()); } fd.close(); scanLines(); exeLines(); } else { std::stringstream ss; ss << "Error: open " << name_file << "."; throw ft_exception(ss.str()); } }
void Cal::print(void) { if (this->_stack.size() == 0) { std::stringstream ss; ss << "LINE " << Cal::_count_line << ": " << "Error: print 0 argument in stack."; throw ft_exception(ss.str()); } if (this->_stack.top()->getType() == INT8) { char c = static_cast<char>(std::atoi(this->_stack.top()->toString().data())); if (isascii(c)) std::cout << c << std::endl; else { std::stringstream ss; ss << "LINE " << Cal::_count_line << ": " << "Error: print not ascii char."; throw ft_exception(ss.str()); } } else { std::stringstream ss; ss << "LINE " << Cal::_count_line << ": " << "Error: print not char."; throw ft_exception(ss.str()); } }
char *ft_itoa(int number) { int number2; int negative; size_t size_str; char *str_number; size_str = 0; negative = 0; if (number == 0 || number == -2147483648) return (ft_exception(number)); if (number < 0 && ++negative) number = -number; number2 = number; while (number) { number = number / 10; size_str++; } str_number = malloc(sizeof(char) * (size_str + 1)); if (!str_number) return (0); ft_itoa2(number2, &str_number, negative, 0); return (ft_strrev(str_number)); }
char *ft_itoa(int number) { int positive; size_t i; char *str_number; init_str(&str_number, &positive, &i); if (number == 0 || number == -2147483648) return (ft_exception(number)); if (number < 0) { number = -number; positive = 0; } while (number) { str_number[i] = (number) % 10 + '0'; number = number / 10; i++; } if (!positive) { str_number[i] = '-'; i++; } str_number[i] = 0; return (strrev(str_number)); }
void Cal::pop(void) { if (this->_stack.size() == 0) { std::stringstream ss; ss << "LINE " << Cal::_count_line << ": " << "Error: Pop on empty stack."; throw ft_exception(ss.str()); } delete(this->_stack.top()); this->_stack.pop(); }
void Cal::assert(IOperand const *o) { if (o->toString() != this->_stack.top()->toString() || o->getType() != this->_stack.top()->getType()) { delete(o); std::stringstream ss; ss << "LINE " << Cal::_count_line << ": " << "Error: assert."; throw ft_exception(ss.str()); } else delete(o); }
void Cal::scanLines() { std::vector<std::string>::iterator begin = this->_lines->begin(); std::vector<std::string>::iterator end = this->_lines->end(); bool error = false; for(; begin != end; ++begin) { Cal::_count_line++; if (testCmd(*begin) == -1) error = true; } if (error) throw ft_exception("Syntax Error."); }
void Cal::mod(void) { if (this->_stack.size() < 2) { std::stringstream ss; ss << "LINE " << Cal::_count_line << ": " << "Error: mod not 2 argument in stack."; throw ft_exception(ss.str()); } IOperand *tmp1 = this->_stack.top(); this->_stack.pop(); IOperand *tmp2 = this->_stack.top(); this->_stack.pop(); this->push(*tmp2 % *tmp1); delete(tmp1); delete(tmp2); }
// without file void Cal::getLines() { std::string line; while (std::getline (std::cin,line)) { if (line == ";;" || line == "^D") break; this->_lines->push_back(line); } if (this->_lines->size() == 0) { std::stringstream ss; ss << "Error: instructions not found."; throw ft_exception(ss.str()); } scanLines(); exeLines(); }
void Cal::exeLines(void) { std::vector<std::string>::iterator begin = this->_lines->begin(); std::vector<std::string>::iterator end = this->_lines->end(); Cal::_count_line = 0; for(; begin != end; ++begin) { Cal::_count_line++; exeCmd(*begin); if (Cal::_count_line == 0) break ; } if (Cal::_count_line > 0) { std::stringstream ss; ss << "LINE " << Cal::_count_line + 1 << ": " << "Error: exit not found."; throw ft_exception(ss.str()); } }