<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: print code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 05:21:24 GMT</pubDate>
    <description>DZone Snippets: print code</description>
    <item>
      <title>Pretty Print XML using Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4953</link>
      <description>This code makes the XML output look pretty.  I tried using the documentation from the REXML website to apply the method example doc.write($stdout,0), but I gave up, and instead wrote my own XML pretty-print method.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'rexml/document'&lt;br /&gt;include REXML&lt;br /&gt;&lt;br /&gt;  def pretty_print(parent_node, itab)&lt;br /&gt;    buffer = ''&lt;br /&gt;&lt;br /&gt;    parent_node.elements.each do |node|&lt;br /&gt;&lt;br /&gt;      buffer += ' ' * itab + "&lt;#{node.name}#{get_att_list(node)}"&lt;br /&gt;  &lt;br /&gt;      if node.to_a.length &gt; 0&lt;br /&gt;        buffer += "&gt;"&lt;br /&gt;        if node.text.nil?&lt;br /&gt;          buffer += "\n"&lt;br /&gt;          buffer += pretty_print(node,itab+2) &lt;br /&gt;          buffer += ' ' * itab + "&lt;/#{node.name}&gt;\n"&lt;br /&gt;        else&lt;br /&gt;          node_text = node.text.strip&lt;br /&gt;          if node_text != ''&lt;br /&gt;            buffer += node_text &lt;br /&gt;            buffer += "&lt;/#{node.name}&gt;\n"        &lt;br /&gt;          else&lt;br /&gt;            buffer += "\n" + pretty_print(node,itab+2) &lt;br /&gt;            buffer += ' ' * itab + "&lt;/#{node.name}&gt;\n"        &lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;      else&lt;br /&gt;        buffer += "/&gt;\n"&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;    end&lt;br /&gt;    buffer&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def get_att_list(node)&lt;br /&gt;    att_list = ''&lt;br /&gt;    node.attributes.each { |attribute, val| att_list += " #{attribute}='#{val}'" }&lt;br /&gt;    att_list&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def pretty_xml(doc)&lt;br /&gt;    buffer = ''&lt;br /&gt;    xml_declaration = doc.to_s.match('&lt;\?.*\?&gt;').to_s&lt;br /&gt;    buffer += "#{xml_declaration}\n" if not xml_declaration.nil?&lt;br /&gt;    xml_doctype = doc.to_s.match('&lt;\!.*\"&gt;').to_s&lt;br /&gt;    buffer += "#{xml_doctype}\n" if not xml_doctype.nil?&lt;br /&gt;    buffer += "&lt;#{doc.root.name}#{get_att_list(doc.root)}"&lt;br /&gt;    if doc.root.to_a.length &gt; 0&lt;br /&gt;      buffer +="&gt;\n#{pretty_print(doc.root,2)}&lt;/#{doc.root.name}&gt;"&lt;br /&gt;    else&lt;br /&gt;      buffer += "/&gt;\n"&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;xml_data = "&lt;hi id='124'&gt;&lt;yo&gt;&lt;a id='1'/&gt;&lt;b/&gt;&lt;c/&gt;&lt;/yo&gt;&lt;sushi&gt;&lt;love&gt;Ruby&lt;/love&gt;&lt;/sushi&gt;&lt;/hi&gt;"&lt;br /&gt;doc = Document.new(xml_data)&lt;br /&gt;pretty_xml(doc)&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;output&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;hi id='124'&gt;&lt;br /&gt;  &lt;yo&gt;&lt;br /&gt;    &lt;a id='1'/&gt;&lt;br /&gt;    &lt;b/&gt;&lt;br /&gt;    &lt;c/&gt;&lt;br /&gt;  &lt;/yo&gt;&lt;br /&gt;  &lt;sushi&gt;&lt;br /&gt;    &lt;love&gt;Ruby&lt;/love&gt;&lt;br /&gt;  &lt;/sushi&gt;&lt;br /&gt;&lt;/hi&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;*Update 7-Jan-08 1:51AM *&lt;br /&gt;Although I haven't tried running the following XSL code, this might actually have been a better solution to my problem. source: http://www.printk.net/~bds/indent.html&lt;br /&gt;&lt;br /&gt;*Pretty Print XML using XSLT*&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;&lt;br /&gt;  &lt;xsl:output method="xml" encoding="ISO-8859-1"/&gt;&lt;br /&gt;  &lt;xsl:param name="indent-increment" select="'   '"/&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;xsl:template name="newline"&gt;&lt;br /&gt;    &lt;xsl:text disable-output-escaping="yes"&gt;&lt;br /&gt;&lt;/xsl:text&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;xsl:template match="comment() | processing-instruction()"&gt;&lt;br /&gt;    &lt;xsl:param name="indent" select="''"/&gt;&lt;br /&gt;    &lt;xsl:call-template name="newline"/&gt;    &lt;br /&gt;    &lt;xsl:value-of select="$indent"/&gt;&lt;br /&gt;    &lt;xsl:copy /&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;xsl:template match="text()"&gt;&lt;br /&gt;    &lt;xsl:param name="indent" select="''"/&gt;&lt;br /&gt;    &lt;xsl:call-template name="newline"/&gt;    &lt;br /&gt;    &lt;xsl:value-of select="$indent"/&gt;&lt;br /&gt;    &lt;xsl:value-of select="normalize-space(.)"/&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;    &lt;br /&gt;  &lt;xsl:template match="text()[normalize-space(.)='']"/&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;xsl:template match="*"&gt;&lt;br /&gt;    &lt;xsl:param name="indent" select="''"/&gt;&lt;br /&gt;    &lt;xsl:call-template name="newline"/&gt;    &lt;br /&gt;    &lt;xsl:value-of select="$indent"/&gt;&lt;br /&gt;      &lt;xsl:choose&gt;&lt;br /&gt;       &lt;xsl:when test="count(child::*) &gt; 0"&gt;&lt;br /&gt;        &lt;xsl:copy&gt;&lt;br /&gt;         &lt;xsl:copy-of select="@*"/&gt;&lt;br /&gt;         &lt;xsl:apply-templates select="*|text()"&gt;&lt;br /&gt;           &lt;xsl:with-param name="indent" select="concat ($indent, $indent-increment)"/&gt;&lt;br /&gt;         &lt;/xsl:apply-templates&gt;&lt;br /&gt;         &lt;xsl:call-template name="newline"/&gt;&lt;br /&gt;         &lt;xsl:value-of select="$indent"/&gt;&lt;br /&gt;        &lt;/xsl:copy&gt;&lt;br /&gt;       &lt;/xsl:when&gt;       &lt;br /&gt;       &lt;xsl:otherwise&gt;&lt;br /&gt;        &lt;xsl:copy-of select="."/&gt;&lt;br /&gt;       &lt;/xsl:otherwise&gt;&lt;br /&gt;     &lt;/xsl:choose&gt;&lt;br /&gt;  &lt;/xsl:template&gt;    &lt;br /&gt;&lt;/xsl:stylesheet&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 07 Jan 2008 01:41:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4953</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Print Page Link</title>
      <link>http://snippets.dzone.com/posts/show/4728</link>
      <description>// Print Page (link) code&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;a href="#" onclick="window.print();return false;"&gt;print&lt;/a&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Nov 2007 01:35:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4728</guid>
      <author>Hanek (Michael Watt)</author>
    </item>
    <item>
      <title>Print a binary number in C</title>
      <link>http://snippets.dzone.com/posts/show/4716</link>
      <description>These are two functions that print the binary representation of an integer. The first simply prints it out, while the second only prints out the relevant digits (i.e. cuts the first x 0 digits) in groups of four.&lt;br /&gt;&lt;br /&gt;Explanation &lt;a href="http://compprog.wordpress.com/2007/10/29/bit-operations/"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* Print n as a binary number */&lt;br /&gt;void printbitssimple(int n) {&lt;br /&gt;	unsigned int i;&lt;br /&gt;	i = 1&lt;&lt;(sizeof(n) * 8 - 1);&lt;br /&gt;&lt;br /&gt;	while (i &gt; 0) {&lt;br /&gt;		if (n &amp; i)&lt;br /&gt;			printf("1");&lt;br /&gt;		else&lt;br /&gt;			printf("0");&lt;br /&gt;		i &gt;&gt;= 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Print n as a binary number */&lt;br /&gt;void printbits(int n) {&lt;br /&gt;	unsigned int i, step;&lt;br /&gt;&lt;br /&gt;	if (0 == n) { /* For simplicity's sake, I treat 0 as a special case*/&lt;br /&gt;		printf("0000");&lt;br /&gt;		return;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	i = 1&lt;&lt;(sizeof(n) * 8 - 1);&lt;br /&gt;&lt;br /&gt;	step = -1; /* Only print the relevant digits */&lt;br /&gt;	step &gt;&gt;= 4; /* In groups of 4 */&lt;br /&gt;	while (step &gt;= n) {&lt;br /&gt;		i &gt;&gt;= 4;&lt;br /&gt;		step &gt;&gt;= 4;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	/* At this point, i is the smallest power of two larger or equal to n */&lt;br /&gt;	while (i &gt; 0) {&lt;br /&gt;		if (n &amp; i)&lt;br /&gt;			printf("1");&lt;br /&gt;		else&lt;br /&gt;			printf("0");&lt;br /&gt;		i &gt;&gt;= 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; i &lt; 16; ++i) {&lt;br /&gt;		printf("%d = ", i);&lt;br /&gt;		printbitssimple(i);&lt;br /&gt;		printf("\n");&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 30 Oct 2007 11:53:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4716</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>[ASM]Write String only using BIOS</title>
      <link>http://snippets.dzone.com/posts/show/3129</link>
      <description>First, set starting address of text into DS:SI registers. Procedure writes text until null-character is met.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;;-----------------------------------------------------------------------------;&lt;br /&gt;;		WRITE_STRING(DS:SI Text)                                      ;&lt;br /&gt;;-----------------------------------------------------------------------------;&lt;br /&gt;;	Writes string from DS:SI until character #0 is met		      ;&lt;br /&gt;Write_String:		&lt;br /&gt;	mov ah, 0xE	&lt;br /&gt;	xor bh, bh	&lt;br /&gt;	mov bl, 0x7&lt;br /&gt;.nextchar	&lt;br /&gt;	lodsb		&lt;br /&gt;	or al,al&lt;br /&gt;	jz .return&lt;br /&gt;	int 10h	&lt;br /&gt;	jmp .nextchar&lt;br /&gt;.return		&lt;br /&gt;	ret	&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Dec 2006 02:02:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3129</guid>
      <author>darktemplar (Piotr)</author>
    </item>
    <item>
      <title>pretty tables for rails models</title>
      <link>http://snippets.dzone.com/posts/show/2176</link>
      <description>From http://www.rubyinside.com/columnized-text-datasets-in-rails-71.html:&lt;br /&gt;Inspired by: http://blog.caboo.se/articles/2006/06/10/pretty-tables-for-ruby-objects&lt;br /&gt;&lt;br /&gt;It gives a MySQL-command-line-client style textual view of data stored in your Rails database. The syntax worked like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Something.find(:all, :conditions =&gt; &#226;&#8364;&#732;whatever&#226;&#8364;&#732;).pretty_print&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;&lt;br /&gt;  protected&lt;br /&gt;&lt;br /&gt;    def columnized_row(fields, sized)&lt;br /&gt;      r = []&lt;br /&gt;      fields.each_with_index do |f, i|&lt;br /&gt;        r &lt;&lt; sprintf(&#226;&#8364;?%0-#{sized[i]}s&#226;&#8364;&#339;, f.to_s.gsub(/\n|\r/, &#226;&#8364;&#732;&#226;&#8364;&#8482;).slice(0, sized[i]))&lt;br /&gt;      end&lt;br /&gt;      r.join(&#226;&#8364;&#8482; | &#226;&#8364;&#732;)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  public&lt;br /&gt;&lt;br /&gt;  def columnized(options = {})&lt;br /&gt;    sized = {}&lt;br /&gt;    self.each do |row|&lt;br /&gt;      row.attributes.values.each_with_index do |value, i|&lt;br /&gt;        sized[i] = [sized[i].to_i, row.attributes.keys[i].length, value.to_s.length].max&lt;br /&gt;        sized[i] = [options[:max_width], sized[i].to_i].min if options[:max_width]&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    table = []&lt;br /&gt;    table &lt;&lt; header = columnized_row(self.first.attributes.keys, sized)&lt;br /&gt;    table &lt;&lt; header.gsub(/./, &#226;&#8364;&#732;-&#226;&#8364;&#732;)&lt;br /&gt;    self.each { |row| table &lt;&lt; columnized_row(row.attributes.values, sized) }&lt;br /&gt;    table.join(&#226;&#8364;?\n&#226;&#8364;&#339;)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class ActiveRecord::Base&lt;br /&gt;  def columnized(options = {})&lt;br /&gt;    [*self].columnized(options)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To use:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; puts Post.find(:all).columnized(:max_width =&gt; 10)&lt;br /&gt;updated_at | title      | private | url | thumb      | metadata | movie      | id  | views | content    | user_id | created_at&lt;br /&gt;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&#226;&#8364;&#8221;&lt;br /&gt;Wed May 31 | tetwer     | 0       |     |            |          |            | 909 | 0     | video:xyzz | 1       | Wed May 31&lt;br /&gt;Wed May 31 | bbbb       | 0       |     |            |          |            | 1   | 15    | // descrip | 1       | Tue May 23&lt;br /&gt;Wed May 31 | cxzcxzx    | 0       |     |            |          |            | 906 | 19    | // descrip | 1       | Tue May 23&lt;br /&gt;Wed May 31 | jklklkl;   | 0       |     |            |          |            | 907 | 35    | // descrip | 1       | Tue May 23&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;If you want to use it with your project, put the code into lib/columnized.rb, use require &#226;&#8364;&#732;columnized&#226;&#8364;&#8482;, and you&#226;&#8364;&#8482;re ready to roll. Unlike courtenay&#226;&#8364;&#8482;s version, mine only supports max_width, but I didn&#226;&#8364;&#8482;t consider changing the column separator too important.</description>
      <pubDate>Tue, 13 Jun 2006 01:56:27 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2176</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>print array in readable format</title>
      <link>http://snippets.dzone.com/posts/show/1555</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;print("&lt;pre&gt;".print_r($_POST,true)."&lt;/pre&gt;");&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 23 Feb 2006 02:51:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1555</guid>
      <author>sneakysausage (Chris Hodgson)</author>
    </item>
    <item>
      <title>Print to a file</title>
      <link>http://snippets.dzone.com/posts/show/952</link>
      <description>To print to console, it's easy&lt;br /&gt;&lt;code&gt;print 'hello', 'world'&lt;/code&gt;&lt;br /&gt;To print to an output file, it's similar&lt;br /&gt;&lt;code&gt;f = open('out.txt','w')&lt;br /&gt;print &gt;&gt;f, 'hello', 'world'&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Dec 2005 14:00:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/952</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
