⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comments.c

📁 代码格式化工具。 其实就是linux下indent的windows版本。
💻 C
📖 第 1 页 / 共 2 页
字号:
	  switch (*buf_ptr)
	    {
	    case ' ':
	    case TAB:
	      /* If formatting, and previous break marker is
	       * nonexistant, or before text on line, reset
	       * it to here. 
	       */
	      if (format && (line_break_ptr < text_on_line))
		{
		  line_break_ptr = e_com;
		}

	      if (format)
		{
		  /* Don't write two spaces after another, unless
		   * the first space is preceeded by a dot. */
		  if ((e_com == s_com) || (e_com[-1] != ' ') ||
		      (e_com - 1 == s_com) || (e_com[-2] == '.'))
		    {
		      *e_com++ = ' ';
		      column++;
		    }
		}
	      else if (*buf_ptr == ' ')
		{
		  *e_com++ = ' ';
		  column++;
		}
	      else
		{
		  /* Convert the tab to the appropriate number of spaces,
		     based on the column we found the comment in, not
		     the one we're printing in. */
		  int tab_width =
		    (settings.tabsize -
		     ((column + found_column - start_column -
		       1) % settings.tabsize));
		  column += tab_width;
		  while (tab_width--)
		    {
		      *e_com++ = ' ';
		    }
		}
	      break;

	    case EOL:
	      /* We may be at the end of a C++ comment */
	      if (comment_type == cplus_comment)
		{
		cplus_exit:
		  parser_state_tos->tos--;
		  parser_state_tos->com_col =
		    (two_contiguous_comments ? 1 : start_column);
		  parser_state_tos->box_com = boxed_comment;
		  *e_com = 0;
		  return;
		}

	      if (format)
		{
		  /* Newline and null are the two characters which
		     end an input line, so check here if we need to
		     get the next line. */
		  if (*buf_ptr == EOL)
		    {
		      ++line_no;
		    }

		  buf_ptr++;
		  if (buf_ptr >= buf_end)
		    {
		      fill_buffer ();
		    }

		  /* If there are any spaces between the text and this
		     newline character, remove them. */
		  if ((e_com > line_break_ptr) &&
		      (text_on_line < line_break_ptr))
		    {
		      e_com = line_break_ptr;
		    }

		  /* If this is "\n\n", or "\n<whitespace>\n",
		   * it's a paragraph break. */

		  skip_buffered_space ();	/* adjusts buf_ptr */

		  if ((*buf_ptr == EOL) || !text_on_line)
		    {
		      paragraph_break = 1;
		      goto end_line;
		    }

		  /* Also need to eat the preamble. */
		  if (!boxed_comment
		      && (current_column () == found_column + 1)
		      && (buf_ptr[0] == '*') && (buf_ptr[1] != '/'))
		    {
		      if (++buf_ptr >= buf_end)
			{
			  fill_buffer ();
			}

		      if ((*buf_ptr == ' ') && (++buf_ptr >= buf_end))
			{
			  fill_buffer ();
			}
		    }

		  /* This is a single newline.  Transform it (and any
		   * following whitespace) into a single blank. */

		  if (e_com[-1] != ' ')
		    {
		      line_break_ptr = e_com;
		      *e_com++ = ' ';
		      column++;
		    }

		  continue;
		}

	      /* We are printing this line "as is", so output it
	         and continue on to the next line. */
	      goto end_line;

	    case '*':
	      /* Check if we've reached the end of the comment. */
	      if (comment_type == comment)
		{
		  if (*(buf_ptr + 1) == '/')
		    {
		      /* If it's not a boxed comment, put some whitespace
		       * before the ending delimiter.  Otherwise, simply
		       * insert the delimiter. */

		      if (!boxed_comment)
			{
			  if (text_on_line)
			    {
			      if (blankline_delims && !suppress_cdb)
				{
				  *e_com = '\0';
				  dump_line (true, paren_targ);
				  *e_com++ = ' ';
				}
			      else
				{
				  /* Insert space before closing delim */
				  if ((*(e_com - 1) != ' ')
				      && (*(e_com - 1) != TAB))
				    {
				      *e_com++ = ' ';
				    }
				}
			    }
			  else if ((s_com == e_com) || (*s_com != '/'))
			    {
			      /* If no text on line, then line is completely empty
			       * or starts with preamble, or is beginning of
			       * comment and starts with beginning delimiter. */

			      e_com = s_com;
			      *e_com++ = ' ';
			    }
			  else
			    {
			      /* This is case of first comment line.  Test
			       * with:
			       * if (first_comment_line != com_lines)
			       * abort (); */

			      if ((*(e_com - 1) != ' ')
				  && (*(e_com - 1) != TAB))
				{
				  *e_com++ = ' ';
				}
			    }
			}

		      /* Now insert the ending delimiter */
		      *e_com++ = '*';
		      *e_com++ = '/';
		      *e_com = '\0';

		      /* Skip any whitespace following the comment.  If
		       * there is only whitespace after it, print the line.
		       *
		       * NOTE:  We're not printing the line: TRY IT! */

		      buf_ptr += 2;

		      buf_ptr = skip_horiz_space (buf_ptr);

		      if (buf_ptr >= buf_end)
			{
			  fill_buffer ();
			}

		      parser_state_tos->tos--;
		      parser_state_tos->com_col =
			(two_contiguous_comments ? 1 : start_column);
		      parser_state_tos->box_com = boxed_comment;
		      return;
		    }

		  /* If this star is on the second line of the
		   * comment in the same column as the star of the
		   * beginning delimiter, then consider it
		   * a boxed comment. */

		  if ((first_comment_line == com_lines - 1) &&
		      (e_com == s_com + line_preamble_length))
		    {
		      /* Account for change in line_preamble_length: */
		      column -= line_preamble_length - 1;
		      line_preamble = " ";
		      line_preamble_length = 1;
		      boxed_comment = 1;
		      format = 0;
		      blankline_delims = 0;
		      *s_com = ' ';
		      *(s_com + 1) = '*';
		      text_on_line = e_com = s_com + 2;
		      column++;
		      break;
		    }
		}

	      /* If it was not the end of the comment, drop through
	       * and insert the star on the line. */

	    default:
	      /* Some textual character. */
	      text_on_line = e_com;
	      *e_com++ = *buf_ptr;
	      column++;
	      break;
	    }


	  /* If we are formatting, check that we haven't exceeded the
	     line length.  If we haven't set line_break_ptr, keep going. */
	  if (format && (column > right_margin) && line_break_ptr)
	    {
	      if (line_break_ptr < e_com - 1)
		{
		  /* Here if we are really "breaking" the line:  the line
		   * break is before some text we've seen. */

		  *line_break_ptr = '\0';
		  save_ptr = line_break_ptr + 1;
		  save_length = e_com - save_ptr;
		  e_com = line_break_ptr;

		  /* If we had to go past `right_margin' to print stuff out,
		   * extend `right_margin' out to this point. */
		  if ((column - save_length) > right_margin)
		    {
		      right_margin = column - save_length;
		    }
		}
	      else
		{
		  /* The line break is after the last text;  we're really
		   * truncating the line. */
		  if (comment_type == cplus_comment)
		    {
		      buf_ptr = skip_horiz_space (buf_ptr);

		      buf_ptr--;
		      if (*buf_ptr == EOL)
			{
			  goto cplus_exit;
			}
		    }
		  else
		    {
		      while ((*buf_ptr == TAB) ||
			     (*buf_ptr == ' ') || (*buf_ptr == EOL))
			{
			  if (*buf_ptr == EOL)
			    {
			      ++line_no;
			    }

			  buf_ptr++;

			  if (buf_ptr >= buf_end)
			    {
			      fill_buffer ();
			    }
			}

		      buf_ptr--;
		    }

		  *e_com = EOS;
		}
	      goto end_line;
	    }

	  if (*buf_ptr == EOL)
	    {
	      ++line_no;
	    }

	  buf_ptr++;

	  if (buf_ptr == buf_end)
	    {
	      fill_buffer ();
	    }
	}


    end_line:
      /* Compress pure whitespace lines into newlines. */
      if (!text_on_line && !visible_preamble
	  && !(first_comment_line == com_lines))
	{
	  e_com = s_com;
	}

      *e_com = EOS;
      dump_line (true, paren_targ);

      /* We're in the middle of a C-comment, don't add blank lines! */

      prefix_blankline_requested = 0;

      /* If formatting (paragraph_break is only used for formatted
       * comments) and user wants blank lines merged, kill all white
       * space after the "\n\n" indicating a paragraph break. */

      if (paragraph_break)
	{
	  if (merge_blank_comment_lines)
	    {
	      while ((*buf_ptr == EOL) ||
		     (*buf_ptr == ' ') || (*buf_ptr == TAB))
		{
		  if (*buf_ptr == EOL)
		    {
		      ++line_no;
		    }

		  if (++buf_ptr >= buf_end)
		    {
		      fill_buffer ();
		    }
		}
	    }

	  paragraph_break = 0;
	}
      else
	{
	  /* If it was a paragraph break (`if' clause), we scanned ahead
	   * one character.  So, here in the `else' clause, advance buf_ptr. */

	  if (*buf_ptr == EOL)
	    {
	      ++line_no;
	    }

	  buf_ptr++;

	  if (buf_ptr >= buf_end)
	    {
	      fill_buffer ();
	    }
	}

    begin_line:
      if (had_eof)
	{
	  break;
	}

      /* Indent the line properly.  If it's a boxed comment, align with
       * the '*' in the beginning slash-star and start inserting there.
       * Otherwise, insert blanks for alignment, or a star if the
       * user specified -sc.
       */

      if (line_preamble)
	{
	  (void) memcpy (e_com, line_preamble, line_preamble_length);
	  e_com += line_preamble_length;
	  column = start_column + line_preamble_length;
	}
      else
	{
	  column = start_column;
	}

      line_break_ptr = 0;

      /* If we have broken the line before the end for formatting,
       * copy the text after the break onto the beginning of this
       * new comment line. */

      if (save_ptr)
	{
	  while (((*save_ptr == ' ') || (*save_ptr == TAB)) && save_length)
	    {
	      save_ptr++;
	      save_length--;
	    }

	  (void) memcpy (e_com, save_ptr, save_length);
	  text_on_line = e_com;
	  e_com += save_length;

	  /* We only break if formatting, in which cases there
	   * are no tabs, only spaces. */

	  column += save_length;
	  save_ptr = 0;
	  save_length = 0;
	}
      else
	{
	  skip_buffered_space ();	/* adjusts buf_ptr */

	  text_on_line = 0;
	}
    }

  parser_state_tos->tos--;
  parser_state_tos->com_col = (two_contiguous_comments ? 1 : start_column);
  parser_state_tos->box_com = boxed_comment;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -