Commit f019d35f authored by Seblu's avatar Seblu
Browse files

Ajout commentaire dans le code

parent bd4914be
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ public:
  void stop();
  void send(const string &data);
  void send(const char* buf, size_t len);
  string recvln();
private:
  void connect_();
  void disconnect_();
+39 −0
Original line number Diff line number Diff line
#include "error.hh"

/*!
** Constructor
**
** @param i error code
** @param s error message
*/
Error::Error(int i, const string s)
{
  val_ = i;
  msg_ << s;
}

/*!
** Constructor by copy
**
** @param e
*/
Error::Error(const Error &e)
{
  val_ = e.val_;
  msg_ << e.msg_.str();
}

/*!
** Print and error msg on std err
**
*/
void Error::print() const
{
  if (msg_.str() != "")
    std::cerr << msg_.str() << "." << std::endl;
}

/*!
** @return a copy of message description
*/
string Error::message() const
{
  return msg_.str();
}

/*!
** @return the error code
*/
int Error::code() const
{
  return val_;
}

/*!
** Set error code to @param v
*/
void Error::code(int v)
{
  val_ = v;
}

/*!
** Copy operator
*/
Error &Error::operator=(const Error &rhs)
{
  val_ = rhs.val_;
@@ -41,24 +68,36 @@ Error &Error::operator=(const Error &rhs)
  return *this;
}

/*!
** Print error message to @param o stream
*/
ostream &operator<<(ostream &o, const Error &e)
{
  o << e.msg_.str();
  return o;
}

/*!
** Concatenate @param val to errror message
*/
Error &operator<<(Error &e, int val)
{
  e.msg_ << val;
  return e;
}

/*!
** Concatenate @param s to errror message
*/
Error &operator<<(Error &e, const string &s)
{
  e.msg_ << s;
  return e;
}

/*!
** Concatenate @param s before error message
*/
Error &operator>>(Error &e, const string &s)
{
  string buf = s + e.msg_.str();
+20 −0
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@

History::History() : max_size_(100) {}

/*!
** Add @param s in history if it's non empty.
*/
void History::add(const string &s) {
  assert(strchr(s.c_str(), '\n') == 0);
  assert(max_size_ > 0);
@@ -17,6 +20,11 @@ void History::add(const string &s) {
  table_.push_front(s);
}

/*!
** Get @param off element in history
**
** @return string associed with @param off in history
*/
const string &History::get(size_t off) const {
  assert(max_size_ > 0);
  assert(off < table_.size());
@@ -32,6 +40,9 @@ const string &History::get(size_t off) const {
  return empty;
}

/*!
** Load history from @param filename
*/
void History::load(const string &filename) {
  ifstream fs;
  t_lines::const_iterator it;
@@ -52,6 +63,9 @@ void History::load(const string &filename) {
  fs.close();
}

/*!
** Save history to @param filename
*/
void History::save(const string &filename) const {
  ofstream fs;
  t_lines::const_reverse_iterator rit;
@@ -72,10 +86,16 @@ void History::save(const string &filename) const {

}

/*!
** @return History size
*/
size_t History::size() const {
  return table_.size();
}

/*!
** Set the max history size to @param s
*/
void History::max_size(size_t s) {
  assert(s > 0);
  max_size_ = s;
+14 −0
Original line number Diff line number Diff line
@@ -4,6 +4,9 @@
#include <getopt.h>
#include <stdlib.h>

/*!
** Constructor
*/
Options::Options() {
  port = 18136;
  history_size = 100;
@@ -11,6 +14,9 @@ Options::Options() {
  log_size = 500;
}

/*!
** Print usage on @param out. The program name is @param name.
*/
void Options::usage(const char *name, ostream &out) const {
  out << "usage: " << name
      << " [-h|--help] [-l|--login name] [-v|--verbose] server [port]" << std::endl;
@@ -18,6 +24,14 @@ void Options::usage(const char *name, ostream &out) const {
  out << "-l | --login name  : Define login to name." << std::endl;
}

/*!
** Parse argline arguments and fill options accordingly
**
** @param argc argc of your program
** @param argv argv of your program
**
** @return
*/
Options &Options::load(int argc, char *argv[]) {
  struct option lopt[] = {
    {"help", 0, 0, 'h'},
+89 −10
Original line number Diff line number Diff line
@@ -7,13 +7,28 @@
#include "error.hh"
#include "history.hh"

/*
*********************************************************************************
*************************************** PUBLIC **********************************
*********************************************************************************
*/

/*!
** Screen constructor
*/
Screen::Screen() {
  // msg init

  // cmd init
  *cmd_buffer_ = 0;
  cmd_buf_off_ = 0;
  cmd_win_off_ = 0;
  cmd_history_off_ = 0;
}

/*!
** Switch in ncurses mode
*/
void Screen::init() {
  // Init ncurses
  initscr();
@@ -29,6 +44,10 @@ void Screen::init() {
  refresh();
}

/*!
** Create windows clear the screen, destroy already existing windows,
** create windows with good size and draw it.
*/
void Screen::create_windows() {
  // Check screen size
  if (LINES < 6 || COLS < 3) {
@@ -36,8 +55,8 @@ void Screen::create_windows() {
    exit(ERR_SCREENSZ);
  }

  if ((size_t) LINES > O.history_size)
    O.history_size = LINES;
  if ((size_t) LINES * 2 > O.history_size)
    O.history_size = 2 * LINES;

  noecho();
  clear();
@@ -56,6 +75,10 @@ void Screen::create_windows() {
  cmd_draw();
}

/*!
** Swith into login window mode and
** manage the login/pass ask processus
*/
void Screen::login() {
  char *buf;
  int size;
@@ -93,12 +116,18 @@ void Screen::login() {
  delete [] buf;
}

/*!
** Switch in dialog window mode
*/
void Screen::dialog() {

  //nominal screen nom
  create_windows();
}

/*!
** Wait keyboard events and act accordingly
*/
void Screen::eventsloop() {
  while (1) {
    int key = getch();
@@ -195,6 +224,26 @@ void Screen::eventsloop() {
  }
}

/*!
** Print a message in msg window
**
** @param s message to print
*/
void Screen::msg_print(const string &s) {
  msg_add(s);
  msg_draw();
}

/*
*********************************************************************************
************************************** PRIVATE **********************************
*********************************************************************************
*/


/*!
** Draw the content of the cmd window
*/
void Screen::cmd_draw() {
  char print_buf[MAX_LINE_SIZE];

@@ -224,6 +273,9 @@ void Screen::cmd_draw() {
  wrefresh(cmd_);
}

/*!
** Draw the content of the msg window
*/
void Screen::msg_draw() {
  werase(msg_);
  box(msg_, 0, 0);
@@ -231,10 +283,10 @@ void Screen::msg_draw() {
}

/*!
** Add a char in a buffer
** Add char @param c in a @param string buffer at offset @param offset.
** @param buf_len is the size of the buffer
**
** @param c char to add
** @param offset where add in the buffer
** @return true if char was added
*/
bool Screen::add_char(char c, char *string, ssize_t offset, size_t buf_len) {
  assert(string);
@@ -255,6 +307,12 @@ bool Screen::add_char(char c, char *string, ssize_t offset, size_t buf_len) {
  return true;
}

/*!
** Del a char from @param string at offset @param offset.
** @param buf_len is the buffer size
**
** @return
*/
bool Screen::del_char(char *string, ssize_t offset, size_t buf_len) {
  assert(string);
  size_t len = strlen(string);
@@ -274,6 +332,9 @@ bool Screen::del_char(char *string, ssize_t offset, size_t buf_len) {
  return true;
}

/*!
** Add msg @param s to msg list
*/
void Screen::msg_add(const string &s) {
  assert(strchr(s.c_str(), '\n') == 0);
  if (s.empty())
@@ -283,6 +344,10 @@ void Screen::msg_add(const string &s) {
  msg_table_.push_front(s);
}

/*!
** Retrieve @param i th msg from msg list
** @return string corresponding to @param i in msg list
*/
const string &Screen::msg_get(size_t i) const {
  assert(i < msg_table_.size());
  t_lines::const_iterator it;
@@ -297,15 +362,18 @@ const string &Screen::msg_get(size_t i) const {
  return empty;
}

/*!
** Compute the size of msg list
**
** @return count of message in msg list
*/
size_t Screen::msg_size() const {
  return msg_table_.size();
}

void Screen::msg_print(const string &s) {
  msg_add(s);
  msg_draw();
}

/*!
** Load msg list from a file @param filename
*/
void Screen::msg_load(const string &filename) {
  ifstream fs;
  t_lines::const_iterator it;
@@ -326,6 +394,9 @@ void Screen::msg_load(const string &filename) {
  fs.close();
}

/*!
** Save msg list to a file @param filename
*/
void Screen::msg_save(const string &filename) const {
  ofstream fs;
  t_lines::const_reverse_iterator rit;
@@ -346,6 +417,14 @@ void Screen::msg_save(const string &filename) const {

}

/*!
** This is a sugar to print msg on msg window
**
** @param scr A Screen
** @param s string to print
**
** @return @param scr
*/
Screen &operator<< (Screen &scr, const string &s) {
  scr.msg_print(s);
  return scr;