From bug-octave-request at bevo dot che dot wisc dot edu Fri Jan 22 04:49:18 1999 Subject: Octave script question From: "John W. Eaton" To: Andy Adler Cc: help-octave at bevo dot che dot wisc dot edu, bug-octave@bevo.che.wisc.edu Date: Fri, 22 Jan 1999 04:49:36 -0600 (CST) On 21-Jan-1999, Andy Adler wrote: | I'm writing a script to convert a file from jpg to | another utility, using octave. | | here's the code | ______________________________ | #!/usr/local/bin/octave -qf | fn= argv(1,:); | img= jpgread(fn); [m,n]= size(img); | fwrite(stdout, [floor(m/256); rem(m,256); | floor(n/256); rem(n,256) ], 'uchar' ); | fwrite(stdout, img(:) ,'uchar'); | ________________________________ | | jpgread is a function that I wrote. | | If I write to a file instead of stdout, then it works | fine, but if I call | | sh>./scriptname jpgfile.jpg > jnkfile | | then I only get 3 characters in jnkfile. | m=320;n=240; to the first 3 characters should be 1 64 0, | but instead they're 1 64 10. | | If seems that some character (0) is acting as an EOF for | octave. Is that right? | | Does the pager need to be disabled? No, it is already disabled. I think the problem is that Octave's buffer for the pager doesn't correctly handle data that contains NUL characters. Please try the following patch (relative to 2.0.13). Thanks, jwe Fri Jan 22 04:41:48 1999 John W. Eaton * pager.cc (more_than_a_screenful): Accept length as second arg. Handle long lines properly, assuming the terminal wraps long lines. (octave_pager_buf::do_sync): Accept length of data as second arg. Use write instead of << to put characters on output stream. (octave_pager_buf::sync): Don't assume data ends at first NUL. (octave_diary_buf::sync): Ditto. *** src/pager.cc~ Mon May 11 00:43:00 1998 --- src/pager.cc Fri Jan 22 04:39:47 1999 *************** *** 116,128 **** } static void ! do_sync (const char *msg, bool bypass_pager) { ! if (msg && *msg) { if (bypass_pager) { ! cout << msg; cout.flush (); } else --- 116,128 ---- } static void ! do_sync (const char *msg, int len, bool bypass_pager) { ! if (msg && len > 0) { if (bypass_pager) { ! cout.write (msg, len); cout.flush (); } else *************** *** 152,158 **** { if (octave_pager_pid > 0 && external_pager->good ()) { ! *external_pager << msg; // These checks are needed if a signal handler // invoked since the last set of checks attempts --- 152,158 ---- { if (octave_pager_pid > 0 && external_pager->good ()) { ! external_pager->write (msg, len); // These checks are needed if a signal handler // invoked since the last set of checks attempts *************** *** 178,209 **** } else { ! cout << msg; cout.flush (); } } } } static bool ! more_than_a_screenful (const char *s) { if (s) { int available_rows = terminal_rows () - 2; int count = 0; ! char c; ! while ((c = *s++) != '\0') ! if (c == '\n') ! { ! count++; ! ! if (count > available_rows) ! return true; ! } } return false; --- 178,218 ---- } else { ! cout.write (msg, len); cout.flush (); } } } } + // Assume our terminal wraps long lines. + static bool ! more_than_a_screenful (const char *s, int len) { if (s) { int available_rows = terminal_rows () - 2; + int cols = terminal_columns (); + int count = 0; ! int chars_this_line = 0; ! ! for (int i = 0; i < len; i++) ! { ! if (*s++ == '\n') ! { ! count += chars_this_line / cols + 1; ! chars_this_line = 0; ! } ! else ! chars_this_line++; ! } ! if (count > available_rows) ! return true; } return false; *************** *** 217,238 **** || (Vpage_screen_output && Vpage_output_immediately) || ! Vpage_screen_output) { - sputc ('\0'); - char *buf = eback (); bool bypass_pager = (! interactive || ! Vpage_screen_output || (really_flush_to_pager && Vpage_screen_output && ! Vpage_output_immediately ! && ! more_than_a_screenful (buf))); ! seekoff (0, ios::beg); ! do_sync (buf, bypass_pager); ! octave_diary << buf; } return 0; --- 226,250 ---- || (Vpage_screen_output && Vpage_output_immediately) || ! Vpage_screen_output) { char *buf = eback (); + int len = pptr () - buf; + bool bypass_pager = (! interactive || ! Vpage_screen_output || (really_flush_to_pager && Vpage_screen_output && ! Vpage_output_immediately ! && ! more_than_a_screenful (buf, len))); ! if (len > 0) ! { ! do_sync (buf, len, bypass_pager); ! seekoff (0, ios::beg); ! octave_diary.write (buf, len); ! } } return 0; *************** *** 241,250 **** int octave_diary_buf::sync (void) { - sputc ('\0'); - if (write_to_diary_file && external_diary_file) ! external_diary_file << eback (); seekoff (0, ios::beg); --- 253,265 ---- int octave_diary_buf::sync (void) { if (write_to_diary_file && external_diary_file) ! { ! int len = pptr () - eback (); ! ! if (len > 0) ! external_diary_file.write (eback (), len); ! } seekoff (0, ios::beg);