By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,514 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

waitbar function error. Pls help

P: 25
Hello,
this waitbar function (similar to the one in matlab) in C++ pops an error. Pls correct the error for me.
Thanks,
prads
Expand|Select|Wrap|Line Numbers
  1. <code>
  2.  
  3. #include <octave/oct.h>
  4. #if defined (HAVE_TERM_H)
  5. #  include <term.h>
  6. #elif defined (HAVE_TERMCAP_H)
  7. #  include <termcap.h>
  8. #endif
  9.  
  10. #define BUF_SIZE    256
  11. #define MAX_LEN        240
  12. #define DEFAULT_LEN    50
  13. #define    BAR_CHAR    '#'
  14. #define SPACING        3
  15.  
  16. static bool no_terminal=false;
  17.  
  18. DEFUN_DLD(waitbar, args, nargout,
  19. "waitbar(...);\n\
  20.  WAITBAR displays a text-based wait bar. This function\n\
  21.  is similar to the Matlab waitbar command, but it is\n\
  22.  a text, rather than graphical function.\n\n\
  23.  A typical usage of WAITBAR in a lengthy computation\n\
  24.  (inside a FOR loop, for example) is as follows:\n\n\
  25.  for i=1:1000\n\
  26.      ## computation\n\
  27.      waitbar(i/1000);\n\
  28.  end\n\n\
  29.  WAITBAR(X,TITLE), where 0 <= X <= 1, sets the position of\n\
  30.  the waitbar to the fractional length X. Values of X exactly\n\
  31.  equal to 0 or 1 clear the waitbar. The optional second\n\
  32.  argument TITLE sets the waitbar caption to TITLE.\n\n\
  33.  If Octave is running in a smart terminal, the width is\n\
  34.  automatically detected, and the title is displayed in the\n\
  35.  waitbar (and truncated if it is too long). Otherwise, the\n\
  36.  title is not displayed and the width is initialized to a\n\
  37.  default of 50 characters, or it can be set to N characters\n\
  38.  with WAITBAR(0,N). If no terminal is detected (such as when\n\
  39.  Octave is run in batch mode and output is redirected), no\n\
  40.  output is generated.\n\n\
  41.  For compatibility with the Matlab version of this function\n\
  42.  (which is graphical rather than text-based), additional\n\
  43.  arguments are ignored, but there are no guarantees of perfect\n\
  44.  compatibility.") 
  45. {
  46.   static char print_buf[BUF_SIZE];
  47.   static int n_chars_old;
  48.   static int pct_int_old;
  49.   static int length;
  50. #if defined(USE_TERM)
  51.   static char term_buffer[1024];
  52.   static char *begin_rv, *end_rv;
  53.   static int brvlen, ervlen, pct_pos;
  54.   static bool smart_term;
  55.   static bool newtitle = false;
  56.   static charMatrix title;
  57.   int j;
  58. #endif
  59.   static char *term;
  60.   static bool init;
  61.  
  62.   double pct;
  63.   int i;
  64.  
  65.   octave_value_list retval;
  66.   retval(0) = Matrix(0,0);
  67.   int nargin = args.length();
  68.   if (nargin < 1) {
  69.     print_usage ();
  70.     return retval;
  71.   }
  72.  
  73.   if(no_terminal)
  74.     return retval;
  75.  
  76.   pct    = args(0).double_value();
  77.   if(pct>1.0)    pct    = 1.0;        // to prevent overflow
  78.  
  79. #if defined(USE_TERM)
  80.   if(nargin==2 && args(1).is_string())
  81.     {
  82.       newtitle = true;
  83.       title = args(1).string_value();
  84.     }
  85.   if(nargin==3)
  86.     {
  87.       newtitle = true;
  88.       title = args(2).string_value();
  89.     }
  90. #endif
  91.  
  92.   if(pct==0.0 || pct==1.0)
  93.     {
  94.       init = true;
  95.       term = getenv("TERM");
  96.       if(!term)
  97.     {
  98.       no_terminal    = true;
  99.       return retval;
  100.     }
  101. #if defined (USE_TERM)
  102.       i = tgetnum("co");
  103.       smart_term = i ? true : false;
  104.       if(nargin==1 || args(1).is_string())
  105.     length = smart_term ? i-1 : DEFAULT_LEN;
  106. #else
  107.       if(nargin==1)
  108.         length = DEFAULT_LEN;
  109. #endif
  110.       else
  111.     if(nargin==2 && !(args(1).is_string()))
  112.     {
  113.       length    = args(1).int_value();
  114.       if(length>MAX_LEN)    length    = MAX_LEN;
  115.       if(length<=0)        length    = DEFAULT_LEN;
  116.     }
  117. #if defined (USE_TERM)
  118.       pct_pos = length/2-2;
  119.       if(smart_term)
  120.     {
  121.       // get terminal strings ("rv"="reverse video")
  122.       char* buf_ptr    = term_buffer;
  123.       begin_rv    = tgetstr("so", &buf_ptr);
  124.       end_rv    = tgetstr("se", &buf_ptr);
  125.       brvlen = 0;    buf_ptr = begin_rv;
  126.       while(buf_ptr[++brvlen]);
  127.       ervlen = 0;    buf_ptr = end_rv;
  128.       while(buf_ptr[++ervlen]);
  129.  
  130.       // initialize print buffer
  131.       for(i=0; i<BUF_SIZE; ++i)
  132.         print_buf[i]    = ' ';
  133.       print_buf[length+brvlen+ervlen+1] = '\r';
  134.       print_buf[length+brvlen+ervlen+2] = '\0';
  135.       for(i=0; i<brvlen; ++i)
  136.         print_buf[i]    = begin_rv[i];
  137.       for(i=0; i<ervlen; ++i)
  138.         print_buf[i+brvlen]    = end_rv[i];
  139.       fputs(print_buf,stdout);
  140.       if(title.length())
  141.         newtitle    = true;
  142.     }
  143.       else
  144.     {
  145. #endif
  146.       for(i=0; i<BUF_SIZE; ++i)
  147.         print_buf[i]    = ' ';
  148.       print_buf[length+8]    = '\r';
  149.       print_buf[length+9]    = '\0';
  150.       fputs(print_buf,stdout);
  151.       print_buf[0]        = '[';
  152.       print_buf[length+1]    = ']';
  153. #if defined (USE_TERM)
  154.     }
  155. #endif
  156.       n_chars_old    = 0;
  157.       fflush(stdout);
  158.       return retval;
  159.     }
  160.   else
  161.     {
  162.       // calculate position
  163.       int n_chars=(int)(pct*length+0.5);
  164.       int pct_int=(int)(pct*100.0+0.5);
  165.  
  166.       // check to see if we got this far without initialization
  167.       if(init==false)
  168.     {
  169.       Fwaitbar(octave_value(0.0),0);
  170.       fputs(print_buf,stdout);
  171.       fflush(stdout);
  172.     }
  173.  
  174.       // check to see of output needs to be updated
  175. #if !defined (USE_TERM)
  176.       if(n_chars!=n_chars_old || pct_int!=pct_int_old)
  177.     {
  178. #else
  179.       if(n_chars!=n_chars_old || pct_int!=pct_int_old || newtitle)
  180.     {
  181.       if(smart_term)
  182.         {
  183.           static char pct_str[5];
  184.           sprintf(pct_str,"%3i%%",pct_int);
  185.  
  186.           // Insert the title
  187.           if(newtitle)
  188.         {
  189.           pct_pos    = length-SPACING*2;
  190.           for(i=SPACING+brvlen; i<pct_pos+brvlen-SPACING;  ++i)
  191.             print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = ' ';
  192.           for(i=SPACING+brvlen, j=0; j<title.length(); ++i, ++j)
  193.             if(i<pct_pos+brvlen-SPACING)
  194.               print_buf[ i>=n_chars_old ? i+ervlen : i ] = title(0,j);
  195.           newtitle    = false;
  196.         }
  197.  
  198.           // Insert the percentage string
  199.           for(i=pct_pos+brvlen, j=0; j<4; ++i, ++j)
  200.         print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = pct_str[j];
  201.  
  202.           // Move print_buf characters
  203.           if(n_chars_old<n_chars)
  204.         for(i=n_chars_old+brvlen; i<n_chars+brvlen; ++i)
  205.           print_buf[i]    = print_buf[i+ervlen];
  206.           else
  207.         for(i=n_chars_old+brvlen-1; i>=n_chars+brvlen; --i)
  208.           print_buf[i+ervlen]    = print_buf[i];
  209.  
  210.           // Insert end of reverse video
  211.           for(i=n_chars+brvlen, j=0; j<ervlen; ++i, ++j)
  212.         print_buf[i]    = end_rv[j];
  213.         }
  214.       else
  215.         {
  216. #endif
  217.           if(n_chars>=n_chars_old)
  218.         for(int i=n_chars_old+1; i<=n_chars; ++i)
  219.           print_buf[i]    = BAR_CHAR;
  220.           else
  221.         for(int i=n_chars+1; i<=n_chars_old; ++i)
  222.           print_buf[i]    = ' ';
  223.           sprintf(&(print_buf[length+3])," %3i%%\r",pct_int);
  224. #if defined (USE_TERM)
  225.         }
  226. #endif
  227.       fputs(print_buf,stdout);
  228.       fflush(stdout);
  229.       n_chars_old    = n_chars;
  230.       pct_int_old    = pct_int;
  231.     }
  232.     }
  233.   return retval;
  234. }
  235.  
Nov 3 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
What's the error? Compile time or run time?

Also, please use code tage when posting.

I have added them for you this time.
Nov 4 '07 #2

P: 25
It shows the foll errors.....

octave/oct.h: No such file or directory.
39 expected constructor, destructor, or type conversion before '(' token
39 expected `,' or `;' before '(' token

Thanks,
Prads
Nov 4 '07 #3

P: 25
I am sorry........line 39 in my code corresponds to 18 in the above code. i.e the error is in DEFUN_DLD()
Thanks,
prads
Nov 4 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
DEFUN_DLD() is a function?

Then what does the function prototype look like?

I'm having trouble reading the code between line 19 and 44.
Nov 5 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.