<?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>Artful Code &#187; c</title>
	<atom:link href="http://www.artfulcode.net/tags/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Pointers, arrays, and string literals</title>
		<link>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/</link>
		<comments>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 12:52:56 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=698</guid>
		<description><![CDATA[A recently posted question on Stack Overflow highlighted a common misconception about the role of pointers and arrays held by many programmers learning C.]]></description>
			<content:encoded><![CDATA[<p>A recently posted <a href="http://stackoverflow.com/questions/1011455/is-it-possible-to-modify-a-string-of-char-in-c/">question on Stack Overflow</a> highlighted a common misconception about the role of pointers and arrays held by many programmers learning C.<br />
<span id="more-698"></span><br />
The confusion stems from a misunderstanding concerning the role of pointers and strings in C. A pointer is an address in memory. It often points to an index in an array, such as in the function <code>strtoupper</code> in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> strtoupper<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>str<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">// null ptr check, courtesy of Michael</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>str <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// destructively modify the contents at the current pointer location</span>
            <span style="color: #666666; font-style: italic;">// using the dereference operator to access the value at the current</span>
            <span style="color: #666666; font-style: italic;">// pointer address.</span>
            <span style="color: #339933;">*</span>str <span style="color: #339933;">=</span> toupper<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #339933;">++</span>str<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> my_str<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
    strtoupper<span style="color: #009900;">&#40;</span>my_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> my_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>my_str</code> is actually a pointer to a block of memory holding chars. This allows us to use address math to access indices of the array and modify them using the dereference operator. In fact, an array index such as <code>my_str[3]</code> is identical to the expression <code>*(my_str + 3)</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span> my_str<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">*</span>my_str <span style="color: #339933;">=</span> toupper<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>my_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>my_str <span style="color: #339933;">+</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> toupper<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>my_str <span style="color: #339933;">+</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> my_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints, &quot;Hello World&quot;</span></pre></div></div>

<p>However, if <code>my_str</code> is declared as a char pointer to the string literal &#8220;hello world&#8221; rather than a char array, these operations fail:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>my_str <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">*</span>my_str <span style="color: #339933;">=</span> toupper<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>my_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// fails</span>
<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>my_str <span style="color: #339933;">+</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> toupper<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>my_str <span style="color: #339933;">+</span> <span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// fails</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> my_str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s explore the difference between the two declarations.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>a <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">char</span> b<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;hello world&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the compiled program, it is likely that &#8220;hello world&#8221; is stored literally inside the executable. It is effectively an immutable, constant value. Pointing <code>char *a</code> to it provides the scope with read-only access to an immutable block of memory. Therefore, attempting to assign a value might cause other code that points to the same memory to behave erratically (read <a href="http://stackoverflow.com/questions/1011455/is-it-possible-to-modify-a-string-of-char-in-c/1011545#1011545">this response to the above post on Stack Overflow</a> for an excellent explanation of this behavior.)</p>
<p>The declaration of <code>char b[]</code> instead declares a locally allocated block of memory that is then filled with the chars, &#8220;hello world&#8221;. <code>b</code> is now a pointer to the first address of that array. The complete statement, combining the declaration and assignment, is shorthand. Dispensing with the array size (e.g., <code>char</code> instead of <code>char[12]</code>) is permitted as the compiler is able to ascertain its size from the string literal it was assigned.</p>
<p>In both cases the pointer is used to access array indices:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%c&quot;</span><span style="color: #339933;">,</span> toupper<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, only with <code>b</code> is the program able to modify the values in memory, since it is explicitly copied to a mutable location on the stack in its declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> b<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
    b<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> toupper<span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F&amp;title=Pointers%2C+arrays%2C+and+string+literals" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F&amp;title=Pointers%2C+arrays%2C+and+string+literals" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Pointers%2C+arrays%2C+and+string+literals&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F&amp;title=Pointers%2C+arrays%2C+and+string+literals" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F&amp;title=Pointers%2C+arrays%2C+and+string+literals" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F&amp;title=Pointers%2C+arrays%2C+and+string+literals" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Pointers%2C+arrays%2C+and+string+literals+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpointers-arrays-and-string-literals%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/pointers-arrays-and-string-literals/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using the newLISP FFI</title>
		<link>http://www.artfulcode.net/articles/using-the-newlisp-ffi/</link>
		<comments>http://www.artfulcode.net/articles/using-the-newlisp-ffi/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 15:44:03 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=469</guid>
		<description><![CDATA[One of newLisp&#8217;s out-of-the-box features is its foreign function interface. It is simple to use, requiring only a minimal knowledge of C and a willingness to read documentation. Importing foreign functions Loading a function from a shared library is done with the &#8216;import&#8217; function. Once imported, the function now exists as a normal function in [...]]]></description>
			<content:encoded><![CDATA[<p>One of newLisp&#8217;s out-of-the-box features is its foreign function interface. It is simple to use, requiring only a minimal knowledge of C and a willingness to read documentation.<span id="more-469"></span></p>
<h3>Importing foreign functions</h3>
<p>Loading a function from a shared library is done with the &#8216;import&#8217; function. Once imported, the function now exists as a normal function in the namespace in which is was imported.  From the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#import">newLisp documentation</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; import in Linux</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.so.6&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; import in Mac OS X</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.dylib&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; import in CYGWIN</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;cygwin1.dll&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>printf is now a function that may be called as any other. Because functions evaluate to themselves and import returns the function value, it is simple to bind the function to a different name:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> print-f <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.dylib&quot;</span> <span style="color: #3AA43E;">&quot;printf&quot;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<h3>Calling foreign functions</h3>
<p>Foreign functions are called in the same was any other function.  They can be applied, curried, or mapped.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>printf <span style="color: #3AA43E;">&quot;%g %s %d %c<span style="color: #546E99; font-weight: bold;">\n</span>&quot;</span> <span style="color: #675400;">1.23</span> <span style="color: #3AA43E;">&quot;hello&quot;</span> <span style="color: #675400;">999</span> <span style="color: #675400;">65</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #675400;">1.23</span> hello <span style="color: #675400;">999</span> A</pre></div></div>

<p>The values that the foreign function receives are pointers to the values that were passed to the function in newLisp. In most cases, this is not problematic, but it is something that must be kept in mind.</p>
<h3>Return values</h3>
<p>Often, a library function will not return a value directly. Often, functions return a pointer to a value or struct in memory. newLisp cannot guess at the size of the return value or its composition, so it is up to the programmer to know what kind of value is being returned.</p>
<p>For functions that return a pointer to a single value, the functions <a href="http://www.newlisp.org/downloads/newlisp_manual.html#get-int">get-int</a>, <a href="http://www.newlisp.org/downloads/newlisp_manual.html#get-char">get-char</a>, and <a href="http://www.newlisp.org/downloads/newlisp_manual.html#get-float">get-float</a> may be used to find the value to which the pointer points. It is important to check the return value of such a function, as passing an invalid value or null pointer to these functions may crash the interpreter.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>get-pointer-from-some-c-func<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> value <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-int</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Strings may be accessed using <a href="http://www.newlisp.org/downloads/newlisp_manual.html#and">get-string</a>, although functions that return string pointers often leave it to the consuming function to free the memory. To do so, the function &#8216;free&#8217; (which frees memory) must first be imported from libc:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">import</span> <span style="color: #3AA43E;">&quot;libc.dylib&quot;</span> <span style="color: #3AA43E;">&quot;free&quot;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>get-string-pointer<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> str <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-string</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; copy the c string to a newLisp string</span>
  <span style="color: #AF0500;">&#40;</span>free ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; free the string allocated by the foreign function</span></pre></div></div>

<h3>Deconstructing structs</h3>
<p>Functions in 3rd party libraries often return custom types that newLisp cannot introspect. In these cases, the programmer must tell newLisp how to unpack the values.</p>
<p>To that end, newLisp provides the <a href="http://www.newlisp.org/downloads/newlisp_manual.html#pack">pack</a> and <a href="http://www.newlisp.org/downloads/newlisp_manual.html#unpack">unpack</a> functions. These can be used to pack newLisp values into a structure and vice versa. pack and unpack use a specially formated string that corresponds to the members of the struct.  Take the following struct as an example:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> foo <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> int_value<span style="color: #339933;">;</span>
  <span style="color: #993333;">double</span> float_value<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It could be unpacked using the format, &#8220;lu lf&#8221; (see the <a href=" http://www.newlisp.org/downloads/newlisp_manual.html#pack">documentation for pack and unpack</a> for a full explanation of the format used). Unpacking with this format will return a list of the struct&#8217;s values.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unpack</span> <span style="color: #3AA43E;">&quot;lu lf&quot;</span> foo-instance<span style="color: #AF0500;">&#41;</span> <span style="color: #808080; font-style: italic;">; returns (42 84.5)</span></pre></div></div>

<p>A function that accepts such a struct can be passed the return value of the pack function:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span>foofunction <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">pack</span> <span style="color: #3AA43E;">&quot;lu lf&quot;</span> <span style="color: #675400;">42</span> <span style="color: #675400;">84.5</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>This makes it simple to unpack complex types into newLisp values.</p>
<h3>Putting it all together</h3>
<p>It is helpful to create a base FOOP class to work with compound types. Specific types can be prototyped from this class, changing only the pack format to match.</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> Pointer:<span style="color: #2028B8;">pack-</span><span style="color: #2028B8;">format</span> <span style="color: #2028B8;">nil</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Pointer:<span style="color: #2028B8;">Pointer</span> addr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">list</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">context</span><span style="color: #AF0500;">&#41;</span> addr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Pointer:<span style="color: #2028B8;">pointer</span> inst<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>inst <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Pointer:<span style="color: #2028B8;">member</span> inst n , unpacked<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> unpacked <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">unpack</span> Pointer:<span style="color: #2028B8;">pack-</span><span style="color: #2028B8;">format</span> <span style="color: #AF0500;">&#40;</span>:<span style="color: #2028B8;">pointer</span> inst<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span>unpacked n<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>The Pointer class gives us a few useful methods. :pointer returns the pointer address and :member returns the nth member of the struct.  So, the foo class would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">new</span> 'Pointer 'Foo<span style="color: #AF0500;">&#41;</span>
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> Foo:<span style="color: #2028B8;">pack-</span><span style="color: #2028B8;">format</span> <span style="color: #3AA43E;">&quot;lu lf&quot;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>It might also be helpful to add a couple of accessor functions:</p>

<div class="wp_syntax"><div class="code"><pre class="newlisp" style="font-family:monospace;"><span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Foo:<span style="color: #2028B8;">int-val</span> inst , ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>:<span style="color: #2028B8;">member</span> inst <span style="color: #675400;">0</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-int</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
&nbsp;
<span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">define</span> <span style="color: #AF0500;">&#40;</span>Foo:<span style="color: #2028B8;">float-val</span> inst , ptr<span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">setf</span> ptr <span style="color: #AF0500;">&#40;</span>:<span style="color: #2028B8;">member</span> inst <span style="color: #675400;">1</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span>
  <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">if-not</span> <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">zero?</span> ptr<span style="color: #AF0500;">&#41;</span>
    <span style="color: #AF0500;">&#40;</span><span style="color: #2028B8;">get-float</span> ptr<span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span><span style="color: #AF0500;">&#41;</span></pre></div></div>

<p>Functions from a foreign library that work on &#8216;foo&#8217; types can now be added directly to the Foo class as methods, and passed the return value of (:pointer my-foo). This makes working with foreign types reasonably trivial.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Using+the+newLISP+FFI&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F&amp;title=Using+the+newLISP+FFI" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Using+the+newLISP+FFI+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-the-newlisp-ffi%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/using-the-newlisp-ffi/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Making 64-bit MySQL work with newLISP</title>
		<link>http://www.artfulcode.net/articles/making-64-bit-mysql-work-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/making-64-bit-mysql-work-newlisp/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 17:49:47 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/making-64-bit-mysql-work-newlisp/</guid>
		<description><![CDATA[For the past few days have been spent trying to get the newLISP MySQL module to work with the 64-bit version of the libmysqlclient library. Here is what I did to get things working properly. Updating paths The file paths in the MySQL module only include the most common libmysqlclient paths. Update the files list [...]]]></description>
			<content:encoded><![CDATA[<p>For the past few days have been spent trying to get the newLISP MySQL module to work with the 64-bit version of the libmysqlclient library.  Here is what I did to get things working properly.<span id="more-16"></span></p>
<h4>Updating paths</h4>
<p>The file paths in the MySQL module only include the most common libmysqlclient paths.  Update the <code>files</code> list to contain <code>/usr/lib64</code> as well:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'files '<span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">&quot;/usr/lib64/libmysqlclient.so&quot;</span> <span style="color: #808080; font-style: italic;">; 64 bit Linux, UNIX</span>
    <span style="color: #ff0000;">&quot;/usr/lib/libmysqlclient.so&quot;</span> <span style="color: #808080; font-style: italic;">; Linux, UNIX</span>
    <span style="color: #ff0000;">&quot;/usr/local/mysql/lib/libmysqlclient.dylib&quot;</span> <span style="color: #808080; font-style: italic;">; MacOS X</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If you are not sure where you libmysqlclient library is, you can find it with this command (assuming it is somewhere in <code>/usr</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">find /usr -<span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;libmysqlclient.so&quot;</span></pre></div></div>

<h4>Updating alignments</h4>
<p>The offsets for most structure fields are set at the beginning of mysql5.lsp.  They need to be updated:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>constant 'NUM_ROWS_OFFSET <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> big-endian <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>constant 'NUM_FIELDS_OFFSET <span style="color: #cc66cc;">96</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>constant 'ERROR_OFFSET <span style="color: #cc66cc;">141</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>constant 'INSERT_ID_OFFSET <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> big-endian <span style="color: #cc66cc;">836</span> <span style="color: #cc66cc;">832</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>constant 'AFFECTED_ROWS_OFFSET <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> big-endian <span style="color: #cc66cc;">828</span> <span style="color: #cc66cc;">824</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is not necessarily the same on all systems.  In the newLISP source tree is a folder named util.  Compile sql.c and execute it to get the proper offsets.  Note that the numbers generated are only one of  the two used in this; you need to first determine the endianness of your system.  The module itself uses this newLISP form:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>pack <span style="color: #ff0000;">&quot;&gt;ld&quot;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>pack <span style="color: #ff0000;">&quot;ld&quot;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If that returns true, update the first number and offset the other number by -4.  Otherwise, update the second number and offset the first by 4 (except for <code>NUM_FIELDS_OFFSET</code> and <code>ERROR_OFFSET</code>).</p>
<p>The offsets for the MYSQL_FIELD structure fields are hard-coded in the functions <code>fetch-row</code> and <code>keep-type</code>.</p>
<p>In <code>keep-type</code>, update the line:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'data <span style="color: #66cc66;">&#40;</span>get-int <span style="color: #66cc66;">&#40;</span>int <span style="color: #66cc66;">&#40;</span>+ type_ptr <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">19</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&#8230;to:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'data <span style="color: #66cc66;">&#40;</span>get-int <span style="color: #66cc66;">&#40;</span>int <span style="color: #66cc66;">&#40;</span>+ type_ptr <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">10</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>That reflects that there are 9 8-byte fields (all char* or ulong) and 10 4-byte fields (all uint) in the MYSQL_FIELD struct.  That will keep the coerced types returned from queries straight.</p>
<p>Next, in <code>fetch-row</code>, the pointer to the field structure is found using:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'field_addr <span style="color: #66cc66;">&#40;</span>get-int <span style="color: #66cc66;">&#40;</span>int <span style="color: #66cc66;">&#40;</span>+ rdata <span style="color: #66cc66;">&#40;</span>* field <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Change the 4 to 8:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> 'field_addr <span style="color: #66cc66;">&#40;</span>get-int <span style="color: #66cc66;">&#40;</span>int <span style="color: #66cc66;">&#40;</span>+ rdata <span style="color: #66cc66;">&#40;</span>* field <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>&#8230;or you will only get half the fields&#8217; values :).</p>
<h4>Data types</h4>
<p>On the newLISP forum, Lutz pointed out that in <code>fetch-row</code>, the call to <code>get-int</code> must be changed to <code>get-long</code> for the 64-bit library.  After a quick look at the types returned in the MySQL C api docs, I also updated <code>num-rows</code>, <code>affected-rows</code> and <code>inserted-id</code>.</p>
<h4>Test it out</h4>
<p>Load the updated module in the newLISP shell and run:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>test-mysql<span style="color: #66cc66;">&#41;</span></pre></div></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F&amp;title=Making+64-bit+MySQL+work+with+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F&amp;title=Making+64-bit+MySQL+work+with+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Making+64-bit+MySQL+work+with+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F&amp;title=Making+64-bit+MySQL+work+with+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F&amp;title=Making+64-bit+MySQL+work+with+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F&amp;title=Making+64-bit+MySQL+work+with+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Making+64-bit+MySQL+work+with+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fmaking-64-bit-mysql-work-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/making-64-bit-mysql-work-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending Python with (almost) anything</title>
		<link>http://www.artfulcode.net/articles/extending-python-almost-anything/</link>
		<comments>http://www.artfulcode.net/articles/extending-python-almost-anything/#comments</comments>
		<pubDate>Mon, 24 Mar 2008 19:42:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[newlisp]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/extending-python-almost-anything/</guid>
		<description><![CDATA[Among the various methods of extending Python about which I have written, Python 2.5&#8242;s ctypes module is possibly the easiest. Libraries written in many languages beyond C, including ECL, may be compiled into a shared library or DLL. Many other languages, like Lua and newLISP, include simple DLLs of their own to embed their interpreters [...]]]></description>
			<content:encoded><![CDATA[<p>Among the <a href="http://www.artfulcode.net/articles/extending-python-pyrex/">various methods</a> of <a href="/articles/writing-python-modules-c-swig/">extending Python</a> about which I have written, Python 2.5&#8242;s <code>ctypes</code> module is possibly the easiest.<span id="more-28"></span></p>
<p>Libraries written in many languages beyond C, including <a href="http://ecls.sourceforge.net/">ECL</a>, may be compiled into a shared library or DLL.  Many other languages, like <a href="http://www.lua.org/">Lua</a> and <a href="http://www.newlisp.org">newLISP</a>, include simple DLLs of their own to embed their interpreters in an application.</p>
<p><code>ctypes</code> allows you to wrap a shared library in pure Python.  Here is a quick example to get you started.  newLISP exports a simple function, <code>newlispEvalStr</code>, which accepts a string of newLISP code and returns the result of the code&#8217;s evaluation as a string.  To compile newLISP as a shared library, use the <code>PLATFORM_lib</code> variant of your system&#8217;s <code>make</code> target (quick note: <code>make install</code> does not install the shared library; it must be manually copied to your path).  The helper function, <code>find_library</code> (located in <code>ctypes.util</code>), gives you a platform-independent way of finding the needed path string for a library.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> ctypes.<span style="color: black;">util</span> <span style="color: #ff7700;font-weight:bold;">import</span> find_library
&nbsp;
lib_path = find_library<span style="color: black;">&#40;</span><span style="color: #483d8b;">'newlisp'</span><span style="color: black;">&#41;</span>
newlisp = CDLL<span style="color: black;">&#40;</span>lib_path<span style="color: black;">&#41;</span></pre></div></div>

<p><code>CDLL</code> extends <code>LoadLibrary</code>, the primary utility class in <code>ctypes</code>.  In Windows, there are also <code>OleDLL</code> and <code>WinDll</code>.  All of these release the global interpreter lock when a foreign function is entered and reclaim it when the function call completes.  There is also a <code>PyDLL</code> class that does not release the GIL; its main purpose is the provide direct access to the Python C API from within your Python application.</p>
<p><code>newlispEvalStr</code> is accessed as a method of the CDLL instance.  As with any low level interface, it is important to know the library you are using.  Accessing invalid function names can lead to unexpected results or system instability.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">nl_eval = newlisp.<span style="color: black;">newlispEvalStr</span></pre></div></div>

<p><code>nl_eval</code> is now an instance of the class <code>_FuncPtr</code>.  To be safe, we want to make sure that only strings get passed to the function and to access the returned data as a string.<br />
<code>ctypes</code> provides the convenience properties <code>argtypes</code> and <code>restype</code> for this purpose.<br />
<code>argtypes</code> is set to lists of argument types (using the c-mapped types <a href="http://docs.python.org/lib/node453.html">here</a>).<br />
<code>restype</code> is a single c type:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">nl_eval.<span style="color: black;">argtypes</span> = <span style="color: black;">&#91;</span>c_char_p<span style="color: black;">&#93;</span>
nl_eval.<span style="color: black;">restype</span> = c_char_p</pre></div></div>

<p>We are now ready to evaluate newLISP code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">nl_eval<span style="color: black;">&#40;</span><span style="color: #483d8b;">'(+ 2 2)'</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># =&amp;gt; '4'</span>
nl_eval<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;(apply + '(4 3 2 5 4))&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># =&amp;gt; '18'</span></pre></div></div>

<p>A more general function can be defined to coerce the desired result type after <code>nl_eval</code> returns:<br />
&lt;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> newlisp_eval<span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span>, return_type=<span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> return_type<span style="color: black;">&#40;</span>nl_eval<span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
newlisp_eval<span style="color: black;">&#40;</span><span style="color: #483d8b;">'(+ 2 2)'</span>, <span style="color: #008000;">int</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># =&amp;gt; 4</span></pre></div></div>

<p>What&#8217;s even better is that because the GIL is released and the library (in this instance) is not directly modifying any Python data, we can make effective use of threading.</p>
<p>Finally, here is a simple module putting all this together:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> ctypes <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> ctypes.<span style="color: black;">util</span> <span style="color: #ff7700;font-weight:bold;">import</span> find_library
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> LibraryNotFound<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> newLISP<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        found = find_library<span style="color: black;">&#40;</span><span style="color: #483d8b;">'newlisp'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> found <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> LibraryNotFound<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">lib</span> = CDLL<span style="color: black;">&#40;</span>found<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>._eval = <span style="color: #008000;">self</span>.<span style="color: black;">lib</span>.<span style="color: black;">newlispEvalStr</span>
            <span style="color: #008000;">self</span>._eval.<span style="color: black;">argtypes</span> = <span style="color: black;">&#91;</span>c_char_p<span style="color: black;">&#93;</span>
            <span style="color: #008000;">self</span>._eval.<span style="color: black;">restype</span> = c_char_p
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #dc143c;">code</span>, return_type=<span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">code</span> = c_char_p<span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span>
        result = <span style="color: #008000;">self</span>._eval<span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> result:
            result = result.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> return_type<span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span></pre></div></div>

<p>Links:</p>
<ul>
<li> <a href="http://docs.python.org/lib/module-ctypes.html">Python ctypes documentation</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F&amp;title=Extending+Python+with+%28almost%29+anything" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F&amp;title=Extending+Python+with+%28almost%29+anything" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Extending+Python+with+%28almost%29+anything&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F&amp;title=Extending+Python+with+%28almost%29+anything" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F&amp;title=Extending+Python+with+%28almost%29+anything" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F&amp;title=Extending+Python+with+%28almost%29+anything" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Extending+Python+with+%28almost%29+anything+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-almost-anything%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/extending-python-almost-anything/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Extending Python with Pyrex</title>
		<link>http://www.artfulcode.net/articles/extending-python-pyrex/</link>
		<comments>http://www.artfulcode.net/articles/extending-python-pyrex/#comments</comments>
		<pubDate>Tue, 23 Oct 2007 18:20:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/extending-python-pyrex/</guid>
		<description><![CDATA[In a previous entry, I showed how to extend Python without directly using the Python C using SWIG. SWIG parses an interface file written by the programmer and then outputs a wrapper module. A more elegant, more pythonic solution might be a parser which allows a C module to be written directly in Python. Pyrex [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="/articles/writing-python-modules-c-swig/">previous entry</a>, I showed how to extend Python without directly using the Python C using SWIG. <a href="http://www.swig.org">SWIG</a> parses an interface file written by the programmer and then outputs a wrapper module.  A more elegant, more pythonic solution might be a parser which allows a C module to be written directly in Python. <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/">Pyrex</a> lets you write your extension module in a Python-like syntax that compiles to C.  The C module can then by compiled using distutils (or gcc, et al) to a binary Python module.<span id="more-44"></span></p>
<p>Pyrex has several advantages over brute force solutions like SWIG.  Pyrex makes the creation of new built-in data types a walk in the park.  There are no cryptic functions for dealing with structs or unions.  Perhaps most significantly, though, is the fact that modules written for Pyrex are written in a syntax very close to Python.  In fact, according to the Pyrex website, &#8220;the fundamental nature of Pyrex can be summed up as follows: Pyrex is Python with C data types.&#8221;</p>
<p>Python is simple, elegant, and productive.  That is why Pythonistas use Python instead of C.  The one place where C is required is when Python lacks a particular functionality that would be too expensive if implemented in pure Python.  Pyrex solves this by allowing the implementation to be written in Python, but then compiled to C (which can then be compiled to machine code.)</p>
<p>In the previous article on SWIG and Python, I wrote a simple C extension that counted the occurrences of each ASCII character in a string.  The reason for this is that iteration over Python&#8217;s strings is slow.  So let&#8217;s create a new data type using Pyrex, the CharIterator.  Our type will be constructed from a Python string and be maintained internally as a character array.  The type will implement the Python iterator and return the ASCII code for the current index&#8217;s character.  Here is our simple Pyrex implementation, which we will call mymod.pyx:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">cdef <span style="color: #ff7700;font-weight:bold;">class</span> CharIterator:
    cdef char <span style="color: #66cc66;">*</span>value
    cdef <span style="color: #008000;">int</span> index
    cdef <span style="color: #008000;">int</span> last_char
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, char <span style="color: #66cc66;">*</span>value<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">value</span> = value
        <span style="color: #008000;">self</span>.<span style="color: black;">index</span> = -<span style="color: #ff4500;">1</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">last_char</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> __next__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">index</span> <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span>= <span style="color: #008000;">self</span>.<span style="color: black;">last_char</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">StopIteration</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">self</span>.<span style="color: black;">index</span> + <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">value</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">index</span><span style="color: black;">&#93;</span></pre></div></div>

<p>CharIterator maintains state in self.index, which is one below the current 0-base index.  The reason for this is so that the character being returned from __next__ does not need to be stored in a temporary variable while index is incremented.  The other possibility would be to return self.value[self.index-1], but that would be inelegant and inefficient (although inc/dec operations have minimal overhead).  More information about the specifics of Pyrex syntax (such as cdef and when to use static type declarations) may be found in the <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/LanguageOverview.html">Pyrex documentation</a>.</p>
<p>Now, compile the module.  Write a simple setup.py:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup
<span style="color: #ff7700;font-weight:bold;">from</span> Pyrex.<span style="color: black;">Distutils</span>.<span style="color: black;">extension</span> <span style="color: #ff7700;font-weight:bold;">import</span> Extension
<span style="color: #ff7700;font-weight:bold;">from</span> Pyrex.<span style="color: black;">Distutils</span> <span style="color: #ff7700;font-weight:bold;">import</span> build_ext
&nbsp;
setup<span style="color: black;">&#40;</span>
  name = <span style="color: #483d8b;">'mymod'</span>,
  ext_modules=<span style="color: black;">&#91;</span>Extension<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mymod&quot;</span>, <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;mymod.pyx&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>,
  cmdclass = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'build_ext'</span>: build_ext<span style="color: black;">&#125;</span>
<span style="color: black;">&#41;</span></pre></div></div>

<p>&#8230;and run &#8220;python setup.py build&#8221;, then &#8220;python setup.py install&#8221; to install to the Python site-packages directory.  You can also skip the last command and manually copy mymod.o from the build/lib.path_based_on_os_and_version/ directory.  Now test the module:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> mymod <span style="color: #ff7700;font-weight:bold;">import</span> CharIterator
&nbsp;
text = <span style="color: #483d8b;">&quot;Hello world.&quot;</span>
chars = CharIterator<span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> chars:
    <span style="color: #ff7700;font-weight:bold;">print</span> c, <span style="color: #483d8b;">&quot;=&quot;</span>, <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Output:</span>
<span style="color: #808080; font-style: italic;"># 72 = H</span>
<span style="color: #808080; font-style: italic;"># 101 = e</span>
<span style="color: #808080; font-style: italic;"># 108 = l</span>
<span style="color: #808080; font-style: italic;"># 108 = l</span>
<span style="color: #808080; font-style: italic;"># 111 = o</span>
<span style="color: #808080; font-style: italic;"># 32 =</span>
<span style="color: #808080; font-style: italic;"># 119 = w</span>
<span style="color: #808080; font-style: italic;"># 111 = o</span>
<span style="color: #808080; font-style: italic;"># 114 = r</span>
<span style="color: #808080; font-style: italic;"># 108 = l</span>
<span style="color: #808080; font-style: italic;"># 100 = d</span>
<span style="color: #808080; font-style: italic;"># 46 = .</span></pre></div></div>

<p>A little testing shows that our CharIterator is a little under twice as fast (roughly 40% on my machine) than iterating over a string array, counting occurrences of each character.  The code used for the test was:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">time</span> <span style="color: #ff7700;font-weight:bold;">import</span> clock
<span style="color: #ff7700;font-weight:bold;">from</span> mymod <span style="color: #ff7700;font-weight:bold;">import</span> CharIterator
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> string_of_file<span style="color: black;">&#40;</span>file_name<span style="color: black;">&#41;</span>:
    file_handle = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>file_name<span style="color: black;">&#41;</span>
    file_text = file_handle.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    file_handle.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> file_text
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> string_of_int<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">32</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">chr</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> i == <span style="color: #ff4500;">9</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; Tab&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> i == <span style="color: #ff4500;">10</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; Newline&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> i == <span style="color: #ff4500;">13</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; Carriage return&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> i == <span style="color: #ff4500;">32</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; Space&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot; Not counted&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> count_chars_pyrex<span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>:
    chars = CharIterator<span style="color: black;">&#40;</span>file_text<span style="color: black;">&#41;</span>
    ascii = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    counts = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> ascii<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> chars:
        counts<span style="color: black;">&#91;</span>c<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> convert_list<span style="color: black;">&#40;</span>counts<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> count_chars_native<span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span>:
    ascii = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    counts =  <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> ascii<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> text:
        i = <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>c<span style="color: black;">&#41;</span>
        counts<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> convert_list<span style="color: black;">&#40;</span>counts<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> convert_list<span style="color: black;">&#40;</span>lst<span style="color: black;">&#41;</span>:
    ascii = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>
    counts = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> ascii:
        char = string_of_int<span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> char <span style="color: #ff7700;font-weight:bold;">in</span> counts.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            counts<span style="color: black;">&#91;</span>char<span style="color: black;">&#93;</span> += lst<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            counts<span style="color: black;">&#91;</span>char<span style="color: black;">&#93;</span> = lst<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> counts
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> print_counts<span style="color: black;">&#40;</span>counts<span style="color: black;">&#41;</span>:
    chars = counts.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    chars.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> chars:
        v = counts<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> v <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">0</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;%s: %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>k.<span style="color: black;">lstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, v<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    file_name = <span style="color: #483d8b;">&quot;/Users/jober/Desktop/Projects/war_and_peace.txt&quot;</span>
    file_text = string_of_file<span style="color: black;">&#40;</span>file_name<span style="color: black;">&#41;</span>
&nbsp;
    start = clock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    native_counts = count_chars_native<span style="color: black;">&#40;</span>file_text<span style="color: black;">&#41;</span>
    end = clock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    native_time = end-start
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Native implementation ran in %f seconds.&quot;</span> <span style="color: #66cc66;">%</span> native_time
&nbsp;
    start = clock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    pyrex_counts = count_chars_pyrex<span style="color: black;">&#40;</span>file_text<span style="color: black;">&#41;</span>
    end = clock<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    pyrex_time = end-start
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Pyrex implementation ran in %f seconds.&quot;</span> <span style="color: #66cc66;">%</span> pyrex_time
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;----------&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Subject text is %d characters long.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>file_text<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> pyrex_time <span style="color: #66cc66;">&amp;</span>lt<span style="color: #66cc66;">;</span> native_time:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Pyrex implementation was faster by %f seconds.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>native_time - pyrex_time<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Native implementation was faster by %f seconds.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>pyrex_time - native_time<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;----------&quot;</span>
    print_counts<span style="color: black;">&#40;</span>pyrex_counts<span style="color: black;">&#41;</span></pre></div></div>

<p>More information about Pyrex:</p>
<ul>
<li> <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/LanguageOverview.html">Pyrex homepage</a></li>
<li> <a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/LanguageOverview.html">Documentation</a></li>
<li> <span style="text-decoration: line-through;">Tutorial</span> (http://ldots.org/pyrex-guide/ is apparently gone for good)</li>
<li> <a href="http://www.freenet.org.nz/python/embeddingpyrex/">Writing an executable with Pyrex</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F&amp;title=Extending+Python+with+Pyrex" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F&amp;title=Extending+Python+with+Pyrex" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Extending+Python+with+Pyrex&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F&amp;title=Extending+Python+with+Pyrex" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F&amp;title=Extending+Python+with+Pyrex" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F&amp;title=Extending+Python+with+Pyrex" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Extending+Python+with+Pyrex+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fextending-python-pyrex%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/extending-python-pyrex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing Python modules in C with SWIG</title>
		<link>http://www.artfulcode.net/articles/writing-python-modules-c-swig/</link>
		<comments>http://www.artfulcode.net/articles/writing-python-modules-c-swig/#comments</comments>
		<pubDate>Tue, 14 Aug 2007 14:53:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/writing-python-modules-c-swig/</guid>
		<description><![CDATA[The Python C API, while very well put together, is still C and therefore requires a lot of time an effort to understand, let alone use. But every now and then, a programmer comes across something that is either impossible in Python or so prohibitively expensive performance-wise that it is worth dusting off the old [...]]]></description>
			<content:encoded><![CDATA[<p>The Python C API, while very well put together, is still C and therefore requires a lot of time an effort to understand, let alone use.  But every now and then, a programmer comes across something that is either impossible in Python or so prohibitively expensive performance-wise that it is worth dusting off the old C textbook.<span id="more-49"></span></p>
<p>I recently began a seemingly simple project that involves counting the number of each character in a potentially large string.  The program only needs to consider ascii characters, with a few significant exceptions- smart quotes and ticks in Word documents must be counted as ascii single and double quotes, respectively.  Happily, Python&#8217;s re is UTF-aware and so can make these substitutions for us.  However, iteration in Python, especially over a potentially large string, is expensive, and using regular expressions for this sort of thing is not to be thought of.  Regular expressions are expensive to begin with, especially if we are performing 128 of them (one for each ascii character that could potentially appear in a string), each over the entire string.  The best performance will be had by iterating over each character in the string and incrementing a dictionary/list/array/whatever containing counts of each character.</p>
<p>I tried this in pure Python.  The performance was average.  I used the <a href="http://www.gutenberg.net/">The Project Gutenberg</a> free online copy of <span style="text-decoration: underline;">War and Peace</span>.  On a quad-core G5 PowerMac, it took several seconds just to iterate through the string, let alone access a dictionary to store counts.  Using a Python iterator did not increase performance significantly.</p>
<p>So I wrote a simple C module with a function that accepts a string pointer, iterates through each character, incrementing an array (using the int cast of the character as the character&#8217;s index in the array).  It was as fast as can be expected from C, processing the entire file in under one tenth of a second.  Here is the code (without the test function that iterated over the file itself- that will not be necessary from Python).  Included is a helper function, count_of_char, which is only there to make access to the TALLY struct more simple from Python (tally.c):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include </span>
<span style="color: #339933;">#include </span>
<span style="color: #339933;">#include </span>
<span style="color: #339933;">#define CHARS 128</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>text<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> counts<span style="color: #009900;">&#91;</span>CHARS<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> TALLY<span style="color: #339933;">;</span>
&nbsp;
TALLY init<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> count_chars<span style="color: #009900;">&#40;</span>TALLY <span style="color: #339933;">*</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">int</span> count_of_char<span style="color: #009900;">&#40;</span>TALLY <span style="color: #339933;">*</span>t<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
TALLY init<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>str<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    TALLY t<span style="color: #339933;">;</span>
    t.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> str<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> CHARS<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        t.<span style="color: #202020;">counts</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    count_chars<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> t<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> count_chars<span style="color: #009900;">&#40;</span>TALLY <span style="color: #339933;">*</span>t<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> strlen<span style="color: #009900;">&#40;</span>t<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        t<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>counts<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> t<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>text<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> count_of_char<span style="color: #009900;">&#40;</span>TALLY <span style="color: #339933;">*</span>t<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> c<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> t<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>counts<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> c<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, how to make this accessible to Python?  It took me a matter of an hour or so to write the C library.  Writing the Python API should not take longer than the library itself, so I decided to use <a href="http://www.swig.org/">SWIG</a> to generate the necessary code.  SWIG uses an interface file rather than parsing the actual C library.  Here is the simple one I used for the library above (tally.i):</p>
<pre><code>%module tally

%{
    #define CHARS 128

    typedef struct
    {
        char *text;
        int counts[CHARS];
    } TALLY;

    extern TALLY init(char *str);
    extern int count_of_char(TALLY *t, char c);
    extern TALLY init_from_file(char *file_path);
%}

typedef struct
{
    char *text;
    int counts[CHARS];
} TALLY;

extern TALLY init(char *str);
extern int count_of_char(TALLY *t, char c);
extern TALLY init_from_file(char *file_path);
</code></pre>
<p>That was straight out of the example in the SWIG docs for Python.  It doesn&#8217;t take much.  Often, an interface file is not even required.  I then ran <code>swig -python tally.i</code>.  This created a few files (tally_wrapper.c and tally.py).  Now, since we are using Python and have access to distutils, we can write a simple setup.py file to do the work of compiling and linking for us:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">distutils</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup, Extension
&nbsp;
setup<span style="color: black;">&#40;</span>name = <span style="color: #483d8b;">&quot;Tally&quot;</span>,
      version = <span style="color: #483d8b;">&quot;1.0&quot;</span>,
      ext_modules = <span style="color: black;">&#91;</span>Extension<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;_tally&quot;</span>, <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;tally.i&quot;</span>,<span style="color: #483d8b;">&quot;tally.c&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Note the naming conventions here.  The extension library must end up named _&lt;name&amp;rt;.so, with the leading underscore, for SWIG.  Now, we can just run <code>python setup.py build</code>, and then <code>python setup.py install</code> to compile and install our module.  From there, we can use a simple Python CGI (or FastCGI or mod_python app, depending on what your host supports) that reads in the string from a web form and uses our new library, tally, to count the characters.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">from operator import itemgetter
import tally
&nbsp;
def tally_string<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">,</span> <span style="color: #b1b100;">min</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> sort<span style="color: #66cc66;">=</span>False<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span>
    <span style="color: #555;">t</span> <span style="color: #66cc66;">=</span> tally<span style="color: #66cc66;">.</span>init<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span>
    chars <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
    for i in xrange<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">127</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span>
        <span style="color: #555;">x</span> <span style="color: #66cc66;">=</span> tally<span style="color: #66cc66;">.</span>count_of_char<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">&#41;</span>
        <span style="color: #b1b100;">if</span> x <span style="color: #66cc66;">&amp;</span>gt<span style="color: #808080; font-style: italic;">;= min:</span>
            chars<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> x
    <span style="color: #b1b100;">if</span> sort<span style="color: #66cc66;">:</span>
        <span style="color: #555;">chars</span> <span style="color: #66cc66;">=</span> sorted<span style="color: #66cc66;">&#40;</span>chars<span style="color: #66cc66;">.</span>items<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> key<span style="color: #66cc66;">=</span>itemgetter<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #b1b100;">reverse</span><span style="color: #66cc66;">=</span>True<span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">return</span> chars</pre></div></div>

<p>Note the use of itemgetter to sort the dictionary we created by value.  The reason that I decided to create the Python storage from Python rather than C was that I was trying to avoid using the Python API in my library at all to maintain its usefulness in other applications.  Now I don&#8217;t have to change the source code at all to use this library in another app.  Additionally, I had never used SWIG before and did not want to overcomplicate matters by messing with lower-level stuff, especially since the big performance problem (the string iteration) had been solved, and I now had cycles to spare.</p>
<p>To see the code in action, <span style="text-decoration: line-through;">here is a working version</span>.  This is a work in progress, obviously, but it demonstrates the speed gain.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F&amp;title=Writing+Python+modules+in+C+with+SWIG" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F&amp;title=Writing+Python+modules+in+C+with+SWIG" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Writing+Python+modules+in+C+with+SWIG&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F&amp;title=Writing+Python+modules+in+C+with+SWIG" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F&amp;title=Writing+Python+modules+in+C+with+SWIG" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F&amp;title=Writing+Python+modules+in+C+with+SWIG" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Writing+Python+modules+in+C+with+SWIG+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwriting-python-modules-c-swig%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/writing-python-modules-c-swig/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using C libraries in newLISP</title>
		<link>http://www.artfulcode.net/articles/using-c-libraries-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/using-c-libraries-newlisp/#comments</comments>
		<pubDate>Sat, 14 Jul 2007 15:43:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/using-c-libraries-newlisp/</guid>
		<description><![CDATA[The past couple of articles have been tutorials on how to use CFFI to access functions from C libraries in Lisp. Out of completeness, I thought I&#8217;d write a short tutorial demonstrating how much easier it is to do the same thing in newLisp. As newLisp is an entirely interpreted language, this built in functionality [...]]]></description>
			<content:encoded><![CDATA[<p>The past couple of articles have been tutorials on how to use CFFI to access functions from C libraries in Lisp.  Out of completeness, I thought I&#8217;d write a short tutorial demonstrating how much easier it is to do the same thing in newLisp.  As newLisp is an entirely interpreted language, this built in functionality allows very easy extension of the language using much more low level and efficient libraries.<span id="more-51"></span></p>
<p>newLisp uses the &#8216;import function to define a function from a foreign library.  The syntax is (import file-path func-name).  For example, if we wished to use libmysqlclient, as in the previous tutorials,</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> libmysqlclient <span style="color: #ff0000;">&quot;/path/to/libmysqlclient.15.dylib&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>import libmysqlclient <span style="color: #ff0000;">&quot;mysql_init&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>import libmysqlclient <span style="color: #ff0000;">&quot;mysql_close&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now, both mysql_init and mysql_close have been wrapped for us and can be used as regular newLisp functions.  From the <a title="&quot;rel=&quot;self" href="http://dev.mysql.com/doc/refman/5.0/en/c.html">MySQL C API docs</a>, we know that mysql_init takes a pointer as a parameter and returns a pointer to a MYSQL type struct.  In newLisp, calling the function with a NULL pointer and assigning the new MYSQL pointer is as easy as,</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> *mysql* <span style="color: #66cc66;">&#40;</span>mysql_init <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>That&#8217;s it.  The symbols <em>mysql</em> now returns a foreign address.  Since we don&#8217;t ever need to access it, we can simply pass <em>mysql</em> to our other functions, such as mysql_close, directly:<br />
<</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>mysql_close *mysql*<span style="color: #66cc66;">&#41;</span>
The MySQL docs describe mysql_real_connect as<span style="color: #66cc66;">:</span>
<span style="color: #66cc66;">&lt;</span>pre lang<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #66cc66;">&gt;</span>MYSQL *mysql_real_connect<span style="color: #66cc66;">&#40;</span>MYSQL *mysql<span style="color: #66cc66;">,</span> const char *host<span style="color: #66cc66;">,</span>
    const char *user<span style="color: #66cc66;">,</span> const char *passwd<span style="color: #66cc66;">,</span> const char *db<span style="color: #66cc66;">,</span> unsigned int port<span style="color: #66cc66;">,</span>
    const char *unix_socket<span style="color: #66cc66;">,</span> unsigned long client_flag<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>That&#8217;s a long definition, but it accepts NULL for the last four arguments.  So, in newLisp:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>mysql_real_connect *mysql* <span style="color: #ff0000;">&quot;someuser&quot;</span> <span style="color: #ff0000;">&quot;secret&quot;</span> <span style="color: #ff0000;">&quot;mydatabase&quot;</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Easy!  This will reassign <em>mysql</em> if successful or return NULL if it fails (it is a destructive function).  To test for this, we can define our own simple wrapper for convenience:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define <span style="color: #66cc66;">&#40;</span>connect host user pass <span style="color: #66cc66;">&#40;</span>db <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>port <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>socket <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span>zero? <span style="color: #66cc66;">&#40;</span>mysql_real_connect *mysql* host user pass db
        port socket <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This accepts our parameters and defaults db, port, and socket to C NULL.  It then calls the function and verifies evaluates to true or nil, depending on whether or not the function returned 0, which is the C NULL.</p>
<p><span style="text-decoration: line-through;">Next, we will discuss how to run a query and access results stored in a struct.</span> <strong>Note:</strong> I never wrote the second part to this article.  Sorry.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F&amp;title=Using+C+libraries+in+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F&amp;title=Using+C+libraries+in+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Using+C+libraries+in+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F&amp;title=Using+C+libraries+in+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F&amp;title=Using+C+libraries+in+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F&amp;title=Using+C+libraries+in+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Using+C+libraries+in+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fusing-c-libraries-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/using-c-libraries-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapping a C library in lisp (part 2)</title>
		<link>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/</link>
		<comments>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/#comments</comments>
		<pubDate>Wed, 04 Jul 2007 00:53:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/</guid>
		<description><![CDATA[Once the library has been defined and loaded into the Lisp image, work can begin on defining C routines in Lisp. Before that can be done, however, a note for those who do not know C. Functions in C have a declared type, as do all of their arguments. When reading the docs for a [...]]]></description>
			<content:encoded><![CDATA[<p>Once the library has been defined and loaded into the Lisp image, work can begin on defining C routines in Lisp.<span id="more-52"></span></p>
<p>Before that can be done, however, a note for those who do not know C.  Functions in C have a declared type, as do all of their arguments.  When reading the docs for a particular library, you will want to know description conventions for C routines.  Let&#8217;s use the example of mysql_init() to begin with.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">MYSQL <span style="color: #339933;">*</span>mysql_init<span style="color: #009900;">&#40;</span>MYSQL <span style="color: #339933;">*</span>mysql<span style="color: #009900;">&#41;</span></pre></div></div>

<p>The first part, <code>MYSQL *mysql_init</code>, defines the function&#8217;s return type and name.  MYSQL is a type created by the library that stores a struct.  This is actually completely irrelevant at this point, because of the asterisk.  The asterisk means that the function is actually allocating a pointer.  Although we can pass an already existing MYSQL object pointer (that&#8217;s the second part), generally we will just use this function to create the internal struct needed to run the program.  We will need to store the pointer that is returned.</p>
<p>The second part, <code>MYSQL *mysql</code>, states this- namely, that a pointer (named mysql, and we know it&#8217;s a pointer because it&#8217;s labelled with an asterisk- *mysql) will be returned.  We store it because this will be passed to most of the other routines in the library.</p>
<p>Technically, we do not really need to define the routine in Lisp.  The function, foreign-funcall, allows us to call the foreign function directly.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>foreign-<span style="color: #b1b100;">funcall</span> <span style="color: #ff0000;">&quot;mysql_init&quot;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span> <span style="color: #66cc66;">&#40;</span>null-pointer<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In this, we apply mysql_init to a null pointer (an empty pointer argument), created using the CFFI function (null-pointer).  The last symbol states we will get a pointer back.</p>
<p>In order to wrap this as a pretty Lisp function, we do:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defcfun <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mysql_init&quot;</span> libmysql-init<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span>
  <span style="color: #66cc66;">&#40;</span>mysql <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This points the Lisp symbol libmysql-init to &#8220;mysql_init&#8221; in the C library.  The next argument, :pointer, states that this returns a pointer.  Next, we can have an &amp;rest list of (parameter-name :type), to define the parameters to pass to the function, in this case, (mysql :pointer).  Available types are listed in the <a href="http://common-lisp.net/project/cffi/manual/cffi-manual.html#Built_002dIn-Types">CFFI documentation</a>.</p>
<p>However, we don&#8217;t want to have to run (null-pointer) every time we use this function.  We also need to store the return value for future use.  So let&#8217;s wrap it in another function to make it more expressive:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mysql-init <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> *mysql* <span style="color: #66cc66;">&#40;</span>libmysql-init <span style="color: #66cc66;">&#40;</span>null-pointer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> *mysql* <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setq</span> *mysql* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Now, we can simply run (mysql-init) and we will have the symbol *mysql* pointing to the returned pointer or nil in the event of a failure.</p>
<p>If we do have a failure, we can use the following definitions to get at the error text:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defcfun <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mysql_error&quot;</span> libmysql-<span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">string</span>
    <span style="color: #66cc66;">&#40;</span>mysql <span style="color: #66cc66;">:</span><span style="color: #555;">pointer</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> mysql-<span style="color: #b1b100;">error</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>libmysql-<span style="color: #b1b100;">error</span> *mysql*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Of course, we will want to add error handling and some other niceties, but having these declared makes writing and testing those completed functions that much easier.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Wrapping+a+C+library+in+lisp+%28part+2%29&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+2%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Wrapping+a+C+library+in+lisp+%28part+2%29+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-2%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wrapping a C library in lisp (part 1)</title>
		<link>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/</link>
		<comments>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/#comments</comments>
		<pubDate>Mon, 02 Jul 2007 19:06:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/</guid>
		<description><![CDATA[One of the more significant difficulties I face when developing Lisp applications is the apparent lack of generally compatable database libraries for the various lisp dialects. CLSQL is a nice solution, but it does not get along well on OSX, my development OS, or with CLISP, which is the primary lisp dialect I have available [...]]]></description>
			<content:encoded><![CDATA[<p>One of the more significant difficulties I face when developing Lisp applications is the apparent lack of generally compatable database libraries for the various lisp dialects. <a href="http://clsql.b9.com/">CLSQL</a> is a nice solution, but it does not get along well on OSX, my development OS, or with CLISP, which is the primary lisp dialect I have available on my <a href="http://www.nearlyfreespeech.com">host</a>.<span id="more-53"></span></p>
<p>My database of choice (and the one available to me on my host) is MySQL. Unfortunately, this is the piece of CLSQL that I can&#8217;t seem to get running on OSX.  In every dialect I&#8217;ve tried (sbcl, cmucl, clisp, openmcl, ad infinitum), CFFI/UFFI cannot find the libmysqlclient library- even when it has been pushed onto the library search path&#8230; but that is a rant for a different blog (or Usenet).</p>
<p><a href="http://common-lisp.net/project/cffi/">CFFI</a> is generally available via ASDF and plays well with GNU CLISP.  It has a fairly simple, intuitive syntax for making C routines available to lisp (as a side note, I have not encountered anything as easy as newLisp&#8217;s C library importer, although this is made easier in that newLisp is implemented in C).</p>
<p>Using CFFI, we can fairly easily create mappings for the most important <a href="http://dev.mysql.com/doc/refman/5.0/en/c.html">MySQL C api</a> functions.  This entry will walk through the process of importing the library; future entries will go one to defining a few of the most basic routines for our lisp image.</p>
<p>Let&#8217;s begin by defining our mysql wrapper package.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defpackage <span style="color: #66cc66;">:</span><span style="color: #555;">mysql-api</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">use</span> <span style="color: #66cc66;">:</span><span style="color: #555;">common-lisp</span> <span style="color: #66cc66;">:</span><span style="color: #555;">cffi</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>in-package <span style="color: #66cc66;">:</span><span style="color: #555;">mysql-api</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>To load the library itself, we use the function, define-foreign-library, in the CFFI package.  The first argument to define-foreign-library is the symbol we wish to point to the library.  Next, we create list similar in syntax to cond, where we define the names of our libraries based on, for example, the host OS.  Since I happen to know that CFFI does find the library path even when explicitly specified, we can conveniently specify a list of full paths to try for each OS.  The full list of platform symbols available <a href="http://common-lisp.net/project/cffi/manual/cffi-manual.html#Platform_002dspecific-features">here</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define-foreign-library libmysqlclient
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">darwin</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #b1b100;">or</span> <span style="color: #ff0000;">&quot;/usr/local/mysql/lib/libmysqlclient.dylib&quot;</span>
                <span style="color: #ff0000;">&quot;/sw/lib/libmysqlclient.dylib&quot;</span>
                <span style="color: #ff0000;">&quot;/usr/local/lib/libmysqlclient.dylib&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>t <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">default</span> <span style="color: #ff0000;">&quot;libmysqlclient&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The last line is a catch-all, and will format the filename based on the OS (i.e., libmysqlclient.so for unix, .dll for Windows).  Although I know my library is located in /usr/local/mysql/lib, I have included various other commonly used paths for MySQL libraries in OSX.</p>
<p>Then, to load the library:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use-foreign-library libmysqlclient<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This will return a pointer to a foreign address (for the C-challenged, a pointer is a variable to points to a value&#8217;s address in memory; it is used to access a value by reference, rather than by value) or, in the event that it did not work, it will throw up an error.  Generally, the error will be caused by CFFI not being able to find the library. If this is the case, verify that your account has access to the file and that the file is included in one of the paths above.</p>
<p>Next, we will begin defining our C routines in lisp.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Wrapping+a+C+library+in+lisp+%28part+1%29&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F&amp;title=Wrapping+a+C+library+in+lisp+%28part+1%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Wrapping+a+C+library+in+lisp+%28part+1%29+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fwrapping-c-library-lisp-part-1%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/wrapping-c-library-lisp-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

