<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Information Technology Management &#38; Development &#187; erlang</title>
	<atom:link href="http://blogs.yellowfish.biz/category/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.yellowfish.biz</link>
	<description>IT Consulting, Infrastructure, Software &#38; Voip Phone Systems</description>
	<lastBuildDate>Tue, 27 Jul 2010 20:04:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSV Parsing with Erlang</title>
		<link>http://blogs.yellowfish.biz/2009/csv-parsing-with-erlang/</link>
		<comments>http://blogs.yellowfish.biz/2009/csv-parsing-with-erlang/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 14:00:47 +0000</pubDate>
		<dc:creator>Praveen Ray</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blogs.yellowfish.biz/?p=320</guid>
		<description><![CDATA[CVS Parsing code with Erlang using OTP gen_fsm]]></description>
			<content:encoded><![CDATA[<p>Since <a href="http://www.erlang.org/doc/">OTP</a> doesn&#39;t provide any CSV parsing capabilities, I decided to write my own based upon <a href="http://www.erlang.org/doc/man/gen_fsm.html">gen_fsm</a>. Following the short explanation given in Perl&#39;s <a href="http://search.cpan.org/~hmbrand/Text-CSV_XS-0.69/CSV_XS.pm#SPECIFICATION">Text::CSV_XS</a> module, I implemented a simple state machine. Erlang&#39;s excellent binary processing capabilities and built in gen_fsm behavior make the code compact, and surprisingly easy to implement. You can download the code from <a href="http://www.yellowfish.biz/code/parse_csv.erl">here</a>. A short explanation follows.</p>
<p>The state machine has only following handful of states:</p>
<h4>start_field</h4>
<p>Start reading a CSV field &#8211; it might be double quoted and might have special chars such as \r, \n, comma and double quote. Goto read_field or read_quoted_field, depending upon if a double quote started this field.</p>
<h4>read_field</h4>
<p>Once a field has started, we switch to this state and read binary bytes until and end of field condition is detected. End of Field is marked by either a comma or a newline.&nbsp;</p>
<h4>read_quoted_field</h4>
<p>We&#39;re inside a double quoted field; read everything until another double quote is encountered. A double quote might be end of this field or an embedded double quote marked with two consecutive double quotes. Switch to&nbsp;escaped_double_quote if a double quote is encountered.</p>
<h4>escaped_double_quote</h4>
<p>We come inside this field upon encountering a double quote inside read_quoted_field state. If another double quote is seen, it&#39;s an escaped double quote, else, it&#39;s nothing special. Both these cases go back to read_quoted_field.</p>
<h3>Usage</h3>
<p>Following public methods are exported:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">parse_csv<span style="color: #009900;">&#40;</span>File_path<span style="color: #009900;">&#41;</span>
parse_csv<span style="color: #009900;">&#40;</span>Binary_blob<span style="color: #009900;">&#41;</span>
parse_csv<span style="color: #009900;">&#40;</span>File_path<span style="color: #339933;">,</span> Options<span style="color: #009900;">&#41;</span>
parse_csv<span style="color: #009900;">&#40;</span>Binary_blob<span style="color: #339933;">,</span> Options<span style="color: #009900;">&#41;</span></pre></div></div>

<p>where Options is:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>callback_fn<span style="color: #339933;">,</span> Fun<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>callback_state<span style="color: #339933;">,</span> term<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span></pre></div></div>

<p>Fun is a function/2 and gets called with a List of Fields and callback state.</p>
<p>With no Options passed, the return is a list of list of Fields. With callback_fn passed, the Callback is called at the end of each line with a list of Fields.</p>
<h3>Examples:</h3>
<p><br/></p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">parse_csv<span style="color: #339933;">:</span>parse_csv<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/tmp/data.csv&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span></pre></div></div>

<p>Returns:</p>
<p>&nbsp;</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">&quot;Date&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;Source&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;Destination&quot;</span><span style="color: #339933;">&gt;&gt;,</span>
  <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">&quot;Seconds&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;CallerID&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;Disposition&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;Cost&quot;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
 <span style="color: #009900;">&#91;</span><span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">&quot;2009-09-18 09:44:54&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;5097213333&quot;</span><span style="color: #339933;">&gt;&gt;,</span>
  <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">&quot;18667778888&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;66&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;5098761323&quot;</span><span style="color: #339933;">&gt;&gt;,&lt;&lt;</span><span style="color: #ff0000;">&quot;ANSWERED&quot;</span><span style="color: #339933;">&gt;&gt;,</span>
  <span style="color: #339933;">&lt;&lt;</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">F <span style="color: #339933;">=</span> fun<span style="color: #009900;">&#40;</span>Fields<span style="color: #339933;">,</span> State<span style="color: #009900;">&#41;</span> <span style="color: #339933;">-&gt;</span> <span style="color: #006600;">io</span><span style="color: #339933;">:</span><span style="color: #000066;">format</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;~p~n&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span>Fields<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> State <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span> end<span style="color: #339933;">.</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">parse_csv<span style="color: #339933;">:</span>parse_csv<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/tmp/data.csv&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>callback_fn<span style="color: #339933;">,</span> F<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>callback_state<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span></pre></div></div>

<div>It calls F repeatedly. First with second parameter set to 0, then 1, then2 and so on. Note that your fun must return a modified state which becomes second parameter to callback_fn for the next line.</div>
<div>&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://blogs.yellowfish.biz/2009/csv-parsing-with-erlang/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Yaws and File Upload</title>
		<link>http://blogs.yellowfish.biz/2009/yaws-and-file-upload/</link>
		<comments>http://blogs.yellowfish.biz/2009/yaws-and-file-upload/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 01:27:05 +0000</pubDate>
		<dc:creator>Praveen Ray</dc:creator>
				<category><![CDATA[Computer Languages]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Web Frameworks]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[yaws]]></category>

		<guid isPermaLink="false">http://blogs.yellowfish.biz/?p=204</guid>
		<description><![CDATA[Read multipart form data using yaws_api]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://yaws.hyber.org/code.yaws?file=/upload.yaws">example</a> at yaws web site to read the file upload is a good starting point but it&#39;s too simplistic. I extended the example so it&#39;s useful in the real world. (Update: Thanks to Steve Vinoski, this module(yaws_multipart) is now part of the yaws <a href="http://github.com/klacke/yaws">git</a> tree).</p>
<ol>
<li>It reads all parameters &#8211; files uploaded and other simple parameters .</li>
<li>It takes a few options to help file uploads. Specifically:
<ol>
<li>{max_file_size, MaxBytes} : If file exceeds MaxBytes bytes, return an error</li>
<li>no_temp_file: read the uploded file in memory without any temp files</li>
<li>{temp_file,FullFilePath}: Specify full path for the temp file. If not given, a unique file name is generated</li>
<li>{temp_dir, TempDir} : Specify a directory to store uploaded temp file. By default &#39;/tmp&#39; is used.</li>
</ol>
</li>
</ol>
<p>Using it is simple. Just call <strong>read_multipart_form</strong> from your &#39;<strong>out</strong>&#39; function and it&#39;ll return a tuple with first element either &#39;get_more&#39;, &#39;done&#39; or &#39;error&#39;. The &#39;get_more&#39; implies more data needs to be read and you must call read_multipart_form again. &#39;done&#39; implies it&#39;s done reading all parameters and you&#39;re free to proceed. The &#39;done&#39; tuple also returns a &#39;dict&#39; full of params. This dict can be queried for parameters by name. For file upload parameters it returns one of the following lists:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>filename<span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;name of the uploaded file as entered on the form&quot;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
  <span style="color: #109ab8;">&#123;</span>value<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Contents_of_the_file_all_in_memory</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span>
<span style="color: #45b3e6;">OR</span>
<span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>filename<span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;name of the uploaded file as entered on the form&quot;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
  <span style="color: #109ab8;">&#123;</span>temp_file<span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;full pathname of the temp file&quot;</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span></pre></div></div>

<p>In the second case, it&#39;s your responsibility to remove the temp file. <strong>Usage example:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>my_yaws_controller<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>out<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">out</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Arg</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
     <span style="color: #45b3e6;">Options</span> <span style="color: #014ea4;">=</span> <span style="color: #109ab8;">&#91;</span>no_temp_file<span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span>
     <span style="color: #186895;">case</span> yaws_multipart:<span style="color: #ff3c00;">read_multipart_form</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Arg</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Options</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span>
             <span style="color: #109ab8;">&#123;</span>done<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span> 
                   <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Params : ~p&quot;</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
                   <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#123;</span>filename<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">File_name</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#123;</span>value<span style="color: #6bb810;">,</span><span style="color: #45b3e6;">File_content</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#93;</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">dict</span>:<span style="color: #ff3c00;">find</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;my_file&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
                  <span style="color: #45b3e6;">Another_param</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">dict</span>:<span style="color: #ff3c00;">find</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;another_param&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Params</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
                  <span style="color: #666666; font-style: italic;">% do something with File_name, File_content and Another_param</span>
              <span style="color: #109ab8;">&#123;</span>error<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
                   <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Error reading multipart form: ~s&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Reason</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
              <span style="color: #45b3e6;">Other</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #45b3e6;">Other</span>
      <span style="color: #186895;">end</span>
<span style="color: #6bb810;">.</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blogs.yellowfish.biz/2009/yaws-and-file-upload/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
