473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

waitbar function error

25
Hello,
I found this waitbar functioning pgm in a forum which does the same work as a matlab waitbar. However this pgm has an error and i cudnot figure it out. Can anyone pls correct it.
Thanks,
Prads
Expand|Select|Wrap|Line Numbers
  1. /**************************************************************************
  2.  *  Waitbar function -- displays progress of lengthy calculations
  3.  *  -------------------------------------------------------------
  4.  *  Copyright (c) 2002  Quentin Spencer <qspencer@ieee.org>
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19.  *  02110-1301 USA
  20.  *
  21.  *
  22.  *************************************************************************/
  23.  
  24. #include <octave/oct.h>
  25. #if defined (HAVE_TERM_H)
  26. #  include <term.h>
  27. #elif defined (HAVE_TERMCAP_H)
  28. #  include <termcap.h>
  29. #endif
  30.  
  31. #define BUF_SIZE    256
  32. #define MAX_LEN        240
  33. #define DEFAULT_LEN    50
  34. #define    BAR_CHAR    '#'
  35. #define SPACING        3
  36.  
  37. static bool no_terminal=false;
  38.  
  39. DEFUN_DLD(waitbar, args, nargout,
  40. "waitbar(...);\n\
  41.  WAITBAR displays a text-based wait bar. This function\n\
  42.  is similar to the Matlab waitbar command, but it is\n\
  43.  a text, rather than graphical function.\n\n\
  44.  A typical usage of WAITBAR in a lengthy computation\n\
  45.  (inside a FOR loop, for example) is as follows:\n\n\
  46.  for i=1:1000\n\
  47.      ## computation\n\
  48.      waitbar(i/1000);\n\
  49.  end\n\n\
  50.  WAITBAR(X,TITLE), where 0 <= X <= 1, sets the position of\n\
  51.  the waitbar to the fractional length X. Values of X exactly\n\
  52.  equal to 0 or 1 clear the waitbar. The optional second\n\
  53.  argument TITLE sets the waitbar caption to TITLE.\n\n\
  54.  If Octave is running in a smart terminal, the width is\n\
  55.  automatically detected, and the title is displayed in the\n\
  56.  waitbar (and truncated if it is too long). Otherwise, the\n\
  57.  title is not displayed and the width is initialized to a\n\
  58.  default of 50 characters, or it can be set to N characters\n\
  59.  with WAITBAR(0,N). If no terminal is detected (such as when\n\
  60.  Octave is run in batch mode and output is redirected), no\n\
  61.  output is generated.\n\n\
  62.  For compatibility with the Matlab version of this function\n\
  63.  (which is graphical rather than text-based), additional\n\
  64.  arguments are ignored, but there are no guarantees of perfect\n\
  65.  compatibility.") 
  66. {
  67.   static char print_buf[BUF_SIZE];
  68.   static int n_chars_old;
  69.   static int pct_int_old;
  70.   static int length;
  71. #if defined(USE_TERM)
  72.   static char term_buffer[1024];
  73.   static char *begin_rv, *end_rv;
  74.   static int brvlen, ervlen, pct_pos;
  75.   static bool smart_term;
  76.   static bool newtitle = false;
  77.   static charMatrix title;
  78.   int j;
  79. #endif
  80.   static char *term;
  81.   static bool init;
  82.  
  83.   double pct;
  84.   int i;
  85.  
  86.   octave_value_list retval;
  87.   retval(0) = Matrix(0,0);
  88.   int nargin = args.length();
  89.   if (nargin < 1) {
  90.     print_usage ();
  91.     return retval;
  92.   }
  93.  
  94.   if(no_terminal)
  95.     return retval;
  96.  
  97.   pct    = args(0).double_value();
  98.   if(pct>1.0)    pct    = 1.0;        // to prevent overflow
  99.  
  100. #if defined(USE_TERM)
  101.   if(nargin==2 && args(1).is_string())
  102.     {
  103.       newtitle = true;
  104.       title = args(1).string_value();
  105.     }
  106.   if(nargin==3)
  107.     {
  108.       newtitle = true;
  109.       title = args(2).string_value();
  110.     }
  111. #endif
  112.  
  113.   if(pct==0.0 || pct==1.0)
  114.     {
  115.       init = true;
  116.       term = getenv("TERM");
  117.       if(!term)
  118.     {
  119.       no_terminal    = true;
  120.       return retval;
  121.     }
  122. #if defined (USE_TERM)
  123.       i = tgetnum("co");
  124.       smart_term = i ? true : false;
  125.       if(nargin==1 || args(1).is_string())
  126.     length = smart_term ? i-1 : DEFAULT_LEN;
  127. #else
  128.       if(nargin==1)
  129.         length = DEFAULT_LEN;
  130. #endif
  131.       else
  132.     if(nargin==2 && !(args(1).is_string()))
  133.     {
  134.       length    = args(1).int_value();
  135.       if(length>MAX_LEN)    length    = MAX_LEN;
  136.       if(length<=0)        length    = DEFAULT_LEN;
  137.     }
  138. #if defined (USE_TERM)
  139.       pct_pos = length/2-2;
  140.       if(smart_term)
  141.     {
  142.       // get terminal strings ("rv"="reverse video")
  143.       char* buf_ptr    = term_buffer;
  144.       begin_rv    = tgetstr("so", &buf_ptr);
  145.       end_rv    = tgetstr("se", &buf_ptr);
  146.       brvlen = 0;    buf_ptr = begin_rv;
  147.       while(buf_ptr[++brvlen]);
  148.       ervlen = 0;    buf_ptr = end_rv;
  149.       while(buf_ptr[++ervlen]);
  150.  
  151.       // initialize print buffer
  152.       for(i=0; i<BUF_SIZE; ++i)
  153.         print_buf[i]    = ' ';
  154.       print_buf[length+brvlen+ervlen+1] = '\r';
  155.       print_buf[length+brvlen+ervlen+2] = '\0';
  156.       for(i=0; i<brvlen; ++i)
  157.         print_buf[i]    = begin_rv[i];
  158.       for(i=0; i<ervlen; ++i)
  159.         print_buf[i+brvlen]    = end_rv[i];
  160.       fputs(print_buf,stdout);
  161.       if(title.length())
  162.         newtitle    = true;
  163.     }
  164.       else
  165.     {
  166. #endif
  167.       for(i=0; i<BUF_SIZE; ++i)
  168.         print_buf[i]    = ' ';
  169.       print_buf[length+8]    = '\r';
  170.       print_buf[length+9]    = '\0';
  171.       fputs(print_buf,stdout);
  172.       print_buf[0]        = '[';
  173.       print_buf[length+1]    = ']';
  174. #if defined (USE_TERM)
  175.     }
  176. #endif
  177.       n_chars_old    = 0;
  178.       fflush(stdout);
  179.       return retval;
  180.     }
  181.   else
  182.     {
  183.       // calculate position
  184.       int n_chars=(int)(pct*length+0.5);
  185.       int pct_int=(int)(pct*100.0+0.5);
  186.  
  187.       // check to see if we got this far without initialization
  188.       if(init==false)
  189.     {
  190.       Fwaitbar(octave_value(0.0),0);
  191.       fputs(print_buf,stdout);
  192.       fflush(stdout);
  193.     }
  194.  
  195.       // check to see of output needs to be updated
  196. #if !defined (USE_TERM)
  197.       if(n_chars!=n_chars_old || pct_int!=pct_int_old)
  198.     {
  199. #else
  200.       if(n_chars!=n_chars_old || pct_int!=pct_int_old || newtitle)
  201.     {
  202.       if(smart_term)
  203.         {
  204.           static char pct_str[5];
  205.           sprintf(pct_str,"%3i%%",pct_int);
  206.  
  207.           // Insert the title
  208.           if(newtitle)
  209.         {
  210.           pct_pos    = length-SPACING*2;
  211.           for(i=SPACING+brvlen; i<pct_pos+brvlen-SPACING;  ++i)
  212.             print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = ' ';
  213.           for(i=SPACING+brvlen, j=0; j<title.length(); ++i, ++j)
  214.             if(i<pct_pos+brvlen-SPACING)
  215.               print_buf[ i>=n_chars_old ? i+ervlen : i ] = title(0,j);
  216.           newtitle    = false;
  217.         }
  218.  
  219.           // Insert the percentage string
  220.           for(i=pct_pos+brvlen, j=0; j<4; ++i, ++j)
  221.         print_buf[ i>=n_chars_old+brvlen ? i+ervlen : i ] = pct_str[j];
  222.  
  223.           // Move print_buf characters
  224.           if(n_chars_old<n_chars)
  225.         for(i=n_chars_old+brvlen; i<n_chars+brvlen; ++i)
  226.           print_buf[i]    = print_buf[i+ervlen];
  227.           else
  228.         for(i=n_chars_old+brvlen-1; i>=n_chars+brvlen; --i)
  229.           print_buf[i+ervlen]    = print_buf[i];
  230.  
  231.           // Insert end of reverse video
  232.           for(i=n_chars+brvlen, j=0; j<ervlen; ++i, ++j)
  233.         print_buf[i]    = end_rv[j];
  234.         }
  235.       else
  236.         {
  237. #endif
  238.           if(n_chars>=n_chars_old)
  239.         for(int i=n_chars_old+1; i<=n_chars; ++i)
  240.           print_buf[i]    = BAR_CHAR;
  241.           else
  242.         for(int i=n_chars+1; i<=n_chars_old; ++i)
  243.           print_buf[i]    = ' ';
  244.           sprintf(&(print_buf[length+3])," %3i%%\r",pct_int);
  245. #if defined (USE_TERM)
  246.         }
  247. #endif
  248.       fputs(print_buf,stdout);
  249.       fflush(stdout);
  250.       n_chars_old    = n_chars;
  251.       pct_int_old    = pct_int;
  252.     }
  253.     }
  254.   return retval;
  255. }
Nov 2 '07 #1
1 1574
Banfa
9,065 Expert Mod 8TB
However this pgm has an error and i cudnot figure it out.
This doesn't appear to be a program, I see no main.

Also if you want help then rather than stating that the code has an error I suggest that you list the errors and the expected/observed behaviour.
Nov 2 '07 #2

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
11
by: John Collyer | last post by:
Hi, In assembly language you can use a lookup table to call functions. 1. Lookup function address in table 2. Call the function Like: CALL FUNCTION
2
by: Chuck Martin | last post by:
I am having a most frustrating problem that references, web searches, and other resources are no help so far in solving. Basically, I'm trying to design a pop-up window to be called with a funciton...
4
by: atv | last post by:
Whatis the proper way to handle errors from function calls? For example, i normally have a main function, with calls to mine or c functions. Should i check for errors in the functions called...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
4
by: prads | last post by:
Hello, this waitbar function (similar to the one in matlab) in C++ pops an error. Pls correct the error for me. Thanks, prads <code> #include <octave/oct.h> #if defined (HAVE_TERM_H) # ...
3
by: suganya | last post by:
Hi Some professionals already has developed the project using menu. In my company, they have given me task to clear the error in that. It is a script file named as "menubarAPI4.js" which is kept...
9
by: MrDeej | last post by:
Hello guys! We have an SQL server which sometimes makes timeouts and connection errors. And we have an function witch writes and updates data in 2 tables on this server. When the SQL server error...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.