home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OBJASM.ZIP / OUBUFF.C < prev    next >
C/C++ Source or Header  |  1990-10-08  |  7KB  |  213 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "o.h"
  4.  
  5. #define NOTHING 0
  6. #define ASCII   1
  7. #define SPECIAL 2
  8.  
  9. #define BUFFSIZE    0x1000+0x10
  10.  
  11. char *buff_beg;                     /* Pointer to first character in buffer */
  12. char *buff_end;                     /* Pointer to last character in buffer */
  13. char *buff_cur;                     /* Pointer to last getc()'d character */
  14.  
  15. char buff[BUFFSIZE] = { '\0' };     /* Array for actual buffer contents */
  16.  
  17. void empty_string( length )
  18.     int     length;
  19. {
  20.     int     prev_mode;
  21.     int     curr_mode;
  22.     int     out_count;
  23.     char    out_buff[OPSIZE+1];
  24.     int     this_char;
  25.     char    byte_buff[5];
  26.     int     byte_len;
  27.     char    *pre_text;
  28.     int     pre_len;
  29.  
  30.     prev_mode = NOTHING;
  31.     out_count = 0;
  32.     out_buff[0] = '\0';
  33.     buff_reseek();
  34.     while ( length ) {
  35.         this_char = buff_regetc();
  36.         if ( this_char < ' ' || this_char > '~' ) {
  37.             curr_mode = SPECIAL;
  38.             sprintf( byte_buff, "0%02Xh", this_char );
  39.             byte_len = 4;
  40.         } else {
  41.             curr_mode = ASCII;
  42.             if ( this_char == '\'' ) {
  43.                 strcpy( byte_buff, "''" );  /* Two apostrophes to indicate one */
  44.                 byte_len = 2;
  45.             } else {
  46.                 sprintf( byte_buff, "%c", this_char );
  47.                 byte_len = 1;
  48.             }
  49.         }
  50.         pre_text = "";                  /* Default to no separator */
  51.         pre_len = 0;
  52.         if ( prev_mode == ASCII && curr_mode == SPECIAL ) {
  53.             pre_text = "',";            /* Separator between ascii and hex */
  54.             pre_len = 2;
  55.         }
  56.         if ( prev_mode == SPECIAL && curr_mode == ASCII ) {
  57.             pre_text = ",'";            /* Separator between hex and ascii */
  58.             pre_len = 2;
  59.         }
  60.         if ( prev_mode == SPECIAL && curr_mode == SPECIAL ) {
  61.             pre_text = ",";             /* Separator between hex codes */
  62.             pre_len = 1;
  63.         }
  64.         if ( out_count + pre_len + byte_len >= OPSIZE ) {
  65.             /*
  66.             ** Output current line and proceed to next line
  67.             */
  68.             if ( prev_mode == ASCII ) {
  69.                 strcat( out_buff, "'" );
  70.             }
  71.             if ( pass == 3 ) {
  72.                 out_directive( "db" );
  73.                 out_operand( out_buff );
  74.                 out_endline();
  75.             }
  76.             out_count = 0;
  77.             out_buff[0] = '\0';
  78.             prev_mode = NOTHING;
  79.             pre_text = "";              /* Back to no seperator */
  80.             pre_len = 0;
  81.         }
  82.         if ( prev_mode == NOTHING && curr_mode == ASCII ) {
  83.             pre_text = "'";
  84.             pre_len = 1;
  85.         }
  86.         strcat( out_buff, pre_text );
  87.         strcat( out_buff, byte_buff );
  88.         out_count += pre_len + byte_len;
  89.         prev_mode = curr_mode;
  90.         if ( this_char == '\0' || this_char == '$' ) {
  91.             /*
  92.             ** Output current line (with this_char) and proceed to next line
  93.             */
  94.             if ( prev_mode == ASCII ) {
  95.                 strcat( out_buff, "'" );
  96.             }
  97.             if ( pass == 3 ) {
  98.                 out_directive( "db" );
  99.                 out_operand( out_buff );
  100.                 out_endline();
  101.             }
  102.             out_count = 0;
  103.             out_buff[0] = '\0';
  104.             prev_mode = NOTHING;
  105.             pre_text = "";              /* Back to no seperator */
  106.             pre_len = 0;
  107.         }
  108.         --length;
  109.     }
  110.     if ( prev_mode != NOTHING ) {
  111.         if ( prev_mode == ASCII ) {
  112.             strcat( out_buff, "'" );
  113.         }
  114.         if ( pass == 3 ) {
  115.             out_directive( "db" );
  116.             out_operand( out_buff );
  117.             out_endline();
  118.         }
  119.     }
  120. }
  121.  
  122. /*------------------------------------------------------------------------------
  123. ** buff_init() - Initialize the buffer variables for an empty buffer.
  124. **------------------------------------------------------------------------------
  125. */
  126. int buff_init( length )
  127.     int     length;
  128. {
  129.     if ( length > BUFFSIZE ) fmt_error("Record too large");
  130.     buff_beg = &buff[0];
  131.     buff_cur = buff_beg;
  132.     length = fread( buff_beg, sizeof(char), length, o_file );
  133.     buff_end = buff_beg + length;  /* Last character will be at buff[length-1] */
  134.     return( length );
  135. }
  136.  
  137. /*------------------------------------------------------------------------------
  138. ** buff_add() - Add to an initialized the buffer.
  139. **------------------------------------------------------------------------------
  140. */
  141. int buff_add( length )
  142.     int     length;
  143. {
  144.     if ( length + (buff_end-buff_beg) > BUFFSIZE ) 
  145.         fmt_error("Adding too much to internal buffer");
  146.     length = fread( buff_end, sizeof(char), length, o_file );
  147.     buff_end = buff_end + length;   /* Last character at buff[length-1] */
  148.     return( length );
  149. }
  150.  
  151. /* --------------------------------------------------------------------------
  152. ** buff_getc() - Get a character from the buffer, if the buffer is empty, the
  153. **              stream is read.  The buffer position pointer is adjusted
  154. **              foreward 1 byte.  If the buffer position pointer is equal to
  155. **              the buffer end pointer, then and EOF value is returned.
  156. **------------------------------------------------------------------------------
  157. */
  158. int buff_getc()
  159. {
  160.     unsigned char   ch;
  161.     int             result;
  162.  
  163.     if ( buff_cur == buff_end ) {                   /* At End of Buffer? */
  164.         result = EOF;
  165.     } else {
  166.         ch = *buff_cur++;
  167.         result = ch;
  168.     }
  169.     return( result );
  170. }
  171.  
  172. /*------------------------------------------------------------------------------
  173. ** buff_regetc() - Get a character which has already been getc'd.  The first
  174. **                character regotten will be the first character after any
  175. **                previous buff_regetc() or buff_empty().   If the count
  176. **                of the number of characters in the buffer is zero, an EOF
  177. **                value is returned.  Otherwise, The buffer beginning pointer
  178. **                is adjusted foreward 1 byte and the character is returned.
  179. **------------------------------------------------------------------------------
  180. */
  181. int buff_regetc()
  182. {
  183.     int     result;
  184.  
  185.     if ( buff_beg == buff_end ) {                          /* Buffer empty? */
  186.         result = EOF;
  187.     } else {
  188.         result = *buff_beg++;
  189.         buff_cur = buff_beg;
  190.     }
  191.     return( result );
  192. }
  193.  
  194. /*------------------------------------------------------------------------------
  195. ** buff_reseek() - Move the buffer position pointer back to the buffer
  196. **                 beginning pointer.
  197. **------------------------------------------------------------------------------
  198. */
  199. void buff_reseek()
  200. {
  201.     buff_cur = buff_beg;            /* Position buff_getc() to beginning */
  202. }
  203.  
  204. /*------------------------------------------------------------------------------
  205. ** buff_empty() - Moves the buffer beginning pointer to the buffer position 
  206. **                pointer.
  207. **------------------------------------------------------------------------------
  208. */
  209. void buff_empty()
  210. {
  211.     buff_beg = buff_cur;            /* Forget all characters before cur ptr */
  212. }
  213.