<?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>coding &#187; programação</title>
	<atom:link href="http://www.coding.com.br/category/programacao/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coding.com.br</link>
	<description>have you coded today?</description>
	<lastBuildDate>Fri, 23 Jul 2010 02:37:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Meu editor de texto: Vim</title>
		<link>http://www.coding.com.br/programacao/meu-editor-de-texto-vim/</link>
		<comments>http://www.coding.com.br/programacao/meu-editor-de-texto-vim/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 05:42:11 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=909</guid>
		<description><![CDATA[O vim é a um bom tempo meu principal editor para a programação.  Além de utilizá-lo para todas as operações &#8220;básicas&#8221; de um editor de texto, vou apresentar rapidamente algumas coisas que fazem ele tão útil para mim.
1) Integração com  o ctags Ctrl+] e Ctrl+t
Muito útil para projetos com vários arquivos onde é preciso navegar pela [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">O <a href="http://www.vim.org" target="_blank">vim</a> é a um bom tempo meu principal editor para a programação.  Além de utilizá-lo para todas as operações &#8220;básicas&#8221; de um editor de texto, vou apresentar rapidamente algumas coisas que fazem ele tão útil para mim.</p>
<p><strong>1) Integração com  o ctags </strong>Ctrl+] e Ctrl+t</p>
<p style="padding-left: 30px; text-align: justify;">Muito útil para projetos com vários arquivos onde é preciso navegar pela definições. Primeiro é preciso gerar a lista de <em>tags</em> do diretório com  o código-fonte.</p>
<pre style="padding-left: 30px;">ctags -R .</pre>
<p style="padding-left: 30px;">Se uma definição se encontra em mais de um lugar, posiciono o cursor no nome da função/definição e entro com o comando <em>:ts</em> ao invés do <em>Ctrl+]</em></p>
<p><strong>2) Tabs </strong>(:tabe) <strong>e split </strong>(:split ou :vsplit)</p>
<p style="padding-left: 30px;">Utilizo para abrir vários arquivos em abas. Para movimentar entre ele utilizo &#8220;&lt;n&gt;gt&#8221; ou simplemente &#8220;<em>gt</em>&#8220;. Já o [v]split divide a tela, para alterar o foco uso o comando <em>Ctrl+w w</em></p>
<p style="padding-left: 30px;"><a href="http://www.coding.com.br/wp-content/uploads/2010/04/vim-tabs.png"><img class="aligncenter size-full wp-image-914" src="http://www.coding.com.br/wp-content/uploads/2010/04/vim-tabs.png" alt="" width="293" height="132" /></a></p>
<p><strong>3) Shell (:sh)</strong></p>
<p style="padding-left: 30px;">Acessar a <em>shell</em> do sistema sem fechar o editor, retorno digitando <em>exit</em></p>
<p><strong>4) Integração com o make </strong>(:make) <strong>e grep</strong> (:grep)</p>
<p><strong>5) Tabs remotas</strong></p>
<p style="padding-left: 30px; text-align: justify;">Este recurso só é ativado se o vim foi compilado com o parâmetro <em>clientserver</em> (no caso do Gentoo é a opção <em>vim-with-x</em>). Permite que eu abra uma única instância do vim e a medida que for abrindo os arquivos eles são visualizados nessa instância. Veja como é simples:</p>
<p style="padding-left: 30px;">Cria-se um servidor:</p>
<pre style="padding-left: 30px;">vim --servername coding</pre>
<p style="padding-left: 30px;">Verifica-se os servidores disponíveis:</p>
<pre style="padding-left: 30px;">vim --serverlist
CODING</pre>
<p style="padding-left: 30px;">Aproveitando, um <em>alia</em>s para facilitar a vida:</p>
<pre style="padding-left: 30px;">alias v="vim --servername CODING --remote-tab"</pre>
<p style="padding-left: 30px;">Depois é editar o arquivo normalmente:</p>
<pre style="padding-left: 30px;">v bla.c</pre>
<p><strong>6) Lista de definições</strong></p>
<p style="padding-left: 30px;">Uma pequena personalização no arquivo ~/.vimrc e um <em>plug-in </em>chamado <a href="http://vim-taglist.sourceforge.net/" target="_blank">taglist</a></p>
<pre style="padding-left: 30px;">let Tlist_Ctags_Cmd="/usr/bin/ctags"
let Tlist_WinWidth = 50
map &lt;F5&gt; :TlistToogle&lt;cr&gt;</pre>
<p style="padding-left: 30px;">Os comandos são auto-explicativos. O &lt;F5&gt; alterna a visibilidade da lista.</p>
<p><a href="http://www.coding.com.br/wp-content/uploads/2010/04/vim-tlist.png"><br />
<img class="aligncenter size-full wp-image-915" src="http://www.coding.com.br/wp-content/uploads/2010/04/vim-tlist.png" alt="" width="535" height="430" /></a></p>
<p style="text-align: justify;">Lembre-se: toda documentação do vim está acessível no próprio editor, basta <em>:help comando</em>. É importante ler a documentação para aprender. Minha intenção não é apresentar todos os modos de utilização, o objetivo deste <em>post</em> é apenas mostrar algumas possibilidades de uso.</p>
<p>:wq</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/meu-editor-de-texto-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ack</title>
		<link>http://www.coding.com.br/programacao/ack/</link>
		<comments>http://www.coding.com.br/programacao/ack/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 23:17:18 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=642</guid>
		<description><![CDATA[Junto com o vim/ctags o ack é ferramenta que mais utilizo ultimamente. É um jeito rápido e eficiente para fazer buscas em arquivos texto. Para quem usa o grep -r vai achar útil.
http://betterthangrep.com/
]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Junto com o vim/ctags o <strong>ack</strong> é ferramenta que mais utilizo ultimamente. É um jeito rápido e eficiente para fazer buscas em arquivos texto. Para quem usa o <em>grep -r </em>vai achar útil.</p>
<p style="text-align: center;"><a href="http://betterthangrep.com/" target="_blank">http://betterthangrep.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/ack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Download photos from Flickr!</title>
		<link>http://www.coding.com.br/programacao/download-photos-from-flickr/</link>
		<comments>http://www.coding.com.br/programacao/download-photos-from-flickr/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 01:35:22 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[yql]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=783</guid>
		<description><![CDATA[I think that one problem regarding creating a script to download photos form Flickr is getting copyrighted material. Suppose that you want all photos from Itajubá city. You can use Flickr API or an YQL request, as follows:
select * from flickr.photos.search where text="itajubá"
My first try to get license information was showing the pertinent part of [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I think that one problem regarding creating a <em>script</em> to download photos form Flickr is getting copyrighted material. Suppose that you want all photos from Itajubá city. You can use Flickr API or an YQL request, as follows:</p>
<pre>select * from flickr.photos.search where text="itajubá"</pre>
<p style="text-align: justify;">My first try to get license information was showing the pertinent part of JSON output. You could see that there isn&#8217;t a field telling that, but if you look at documentation there&#8217;s a &#8220;license&#8221; parameter.</p>
<blockquote><p>&#8220;farm&#8221;: &#8220;5&#8243;,<br />
&#8220;id&#8221;: &#8220;4458853758&#8243;,<br />
&#8220;isfamily&#8221;: &#8220;0&#8243;,<br />
&#8220;isfriend&#8221;: &#8220;0&#8243;,<br />
&#8220;ispublic&#8221;: &#8220;1&#8243;,<br />
&#8220;owner&#8221;: &#8220;76062736@N00&#8243;,<br />
&#8220;secret&#8221;: &#8220;9edcd7aea8&#8243;,<br />
&#8220;server&#8221;: &#8220;4003&#8243;,<br />
&#8220;title&#8221;: &#8220;Clube&#8221;</p></blockquote>
<p>Flickr! has seven options to licensing your photo (again I didn&#8217;t found in documentation, I tried each one)</p>
<ul>
<li>None (All rights reserved) [ license="0" ]</li>
<li>Attribution-NonCommercial-ShareAlike Creative Commons [ license="1" ]</li>
<li>Attribution-NonCommercial Creative Commons [ license="2" ]</li>
<li>Attribution-NonCommercial-NoDerivs Creative Commons [ license="3" ]</li>
<li>Attribution Creative Commons [ license="4" ]</li>
<li>Attribution-ShareAlike Creative Commons [ license="5" ]</li>
<li>Attribution-NoDerivs Creative Commons [ license="6" ]</li>
</ul>
<p>My friend was looking to a way to download all photos from Yahoo! Open Hack Brazil so I decided to create a quick way to get theses photos (respecting copyrighted material). My <a href="http://developer.yahoo.com/yql/console/?q=select%20*%20from%20flickr.photos.search%20where%20text%3D%22Cat%22%20limit%200#h=select%20*%20from%20flickr.photos.search%280%2C2000%29%20where%20tags%3D%22brhackday%22%20and%20license%3D%221%2C2%2C3%2C4%2C5%2C6%22">query</a> looks like:</p>
<pre>select * from flickr.photos.search(0,2000) where tags="brhackday" and license="1,2,3,4,5,6"</pre>
<p style="text-align: justify;">After tests I put the URL from YQL virtual console in my Python code (that parses JSON) and generate a Shell Script to download (using curl/wget)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> simplejson
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span> 
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;download.sh&quot;</span>,<span style="color: #483d8b;">&quot;w+&quot;</span><span style="color: black;">&#41;</span>
f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;#!/bin/sh<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
i = <span style="color: #ff4500;">0</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
&nbsp;
	url_base=<span style="color: #483d8b;">&quot;http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20flickr.photos.search(&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> 
+ <span style="color: #483d8b;">&quot;%2C&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span> +<span style="color: #483d8b;">&quot;)%20where%20tags%3D%22brhackday%22%20and%20license%3D%221%2C2%2C3%2C4%2C5%2C6%22
&amp;format=json&quot;</span> 	
&nbsp;
	rest = simplejson.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url_base<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'query'</span><span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> rest<span style="color: black;">&#91;</span><span style="color: #483d8b;">'count'</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'0'</span>:
		f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		exit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
	photos = rest<span style="color: black;">&#91;</span><span style="color: #483d8b;">'results'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'photo'</span><span style="color: black;">&#93;</span> 
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> photo <span style="color: #ff7700;font-weight:bold;">in</span> photos:
		<span style="color: #008000;">id</span> = photo<span style="color: black;">&#91;</span><span style="color: #483d8b;">'id'</span><span style="color: black;">&#93;</span>
		owner = photo<span style="color: black;">&#91;</span><span style="color: #483d8b;">'owner'</span><span style="color: black;">&#93;</span>
		d = <span style="color: #483d8b;">&quot;curl -s http://www.flickr.com/photos/%s/%s/sizes/o 
| egrep <span style="color: #000099; font-weight: bold;">\&quot;</span>&lt;p&gt;&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span> 
| sed 's/&lt;p&gt;//g ; s/&lt;br <span style="color: #000099; font-weight: bold;">\/</span>&gt;//g ; s/&lt;<span style="color: #000099; font-weight: bold;">\/</span>p&gt;//g ; s/&lt;img src=<span style="color: #000099; font-weight: bold;">\&quot;</span>//g ; s/<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\/</span>&gt;//g' 
| xargs wget ;&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>owner, <span style="color: #008000;">id</span><span style="color: black;">&#41;</span>
		f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>d+<span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span>
		i += <span style="color: #ff4500;">100</span>
&nbsp;
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p style="text-align: justify;">Probably If you cut &amp; paste the code they will not work due some indentation issue, so I published in gist <a href="http://gist.github.com/346413">here</a>.</p>
<p>The usage I simple:</p>
<pre>$ python flickr-d.py
$ sh download.sh</pre>
<p>This was a extremely simple workaround but I expect you enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/download-photos-from-flickr/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple C macro for debugging</title>
		<link>http://www.coding.com.br/programacao/simple-c-macro-for-debugging/</link>
		<comments>http://www.coding.com.br/programacao/simple-c-macro-for-debugging/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 03:30:18 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[linguagem c]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=687</guid>
		<description><![CDATA[A little trick if you use printf to debug information in your code and don&#8217;t like to comment/uncomment.

#include &#60;stdio.h&#62;
&#160;
#define dprintf if (debug) printf
&#160;
const char debug = 1; /* or 0 if you want disable debug */ 
&#160;
int main&#40;int argc, char *argv&#91;&#93; &#41; &#123;
     dprintf &#40;&#34;debug message&#34;&#41;;
     return [...]]]></description>
			<content:encoded><![CDATA[<p>A little trick if you use <em>printf</em> to debug information in your code and don&#8217;t like to comment/uncomment.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define dprintf if (debug) printf</span>
&nbsp;
<span style="color: #993333;">const</span> <span style="color: #993333;">char</span> debug <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* or 0 if you want disable debug */</span> 
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     dprintf <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;debug message&quot;</span><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 align="justify">
Remember that using <em>printf</em> is just one way to debug your code and an excess can impair the efficiency to analyze the situation or catch bugs. If you need start your program, specifying anything that might affect its behavior; make your program stop on specified conditions; examine what has happened, when your program has stopped or change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another; use programs such as <a href="http://www.gnu.org/software/gdb/">GDB</a> or <a href="http://valgrind.org/">Valgrind</a> (especially to memory management issues)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/simple-c-macro-for-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Uploads de fotos no Flickr! usando API e Python</title>
		<link>http://www.coding.com.br/programacao/uploads-de-fotos-no-flickr-usando-api-e-python/</link>
		<comments>http://www.coding.com.br/programacao/uploads-de-fotos-no-flickr-usando-api-e-python/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 04:10:23 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[flickr]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=663</guid>
		<description><![CDATA[A API (Interface de Programação de Aplicativo) do Flickr! é bem documetada e rapidamente você pode fazer bastante coisa. Se você desejar usar a linguagem Python como método de acesso, em linhas gerais você precisa.

Criar uma chave na API do Flickr!
Download do binding para acesso a API (flickrapi)

Nas distribuições Linux, um dos jeitos de instalar [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">A API (Interface de Programação de Aplicativo) do <a href="www.flickr.com" target="_blank">Flickr!</a> é bem <a href="http://www.flickr.com/services/api/" target="_blank">documetada</a> e rapidamente você pode fazer bastante coisa. Se você desejar usar a linguagem Python como método de acesso, em linhas gerais você precisa.</p>
<ol>
<li><a href="http://www.flickr.com/services/apps/create/" target="_blank">Criar</a> uma chave na API do Flickr!</li>
<li>Download do <em>binding</em> para acesso a API (<a href="http://pypi.python.org/pypi/flickrapi" target="_blank">flickrapi</a>)</li>
</ol>
<p>Nas distribuições Linux, um dos jeitos de instalar é usar o easy_install</p>
<pre>easy_install flickrapi</pre>
<p align="justify">
Um modo eficaz para fazer o <em>upload</em> das fotos no serviço seria um <em>script</em> que varre e submete todas as imagens, por exemplo, de uma pasta definida. O exemplo abaixo recebe como parâmetro um diretório e busca por todos os arquivos com extensão .jpg. A função <em>status</em> é apenas para mostrar o andamento do <em>upload</em> e é executada como uma chamada <em>callback</em> no método flickr.upload(). Nos meus testes, precisei pegar o número definido na variável <em>token</em>, antes, executando na interface de linha de comando do Python os seguintes passos:
</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> api_key = <span style="color: #483d8b;">&quot;&lt;API&gt;&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> secret = <span style="color: #483d8b;">&quot;&lt;CHAVE SECRETA&gt;&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> username = <span style="color: #483d8b;">&quot;&lt;USER&gt;&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> flickr = flickrapi.<span style="color: black;">FlickrAPI</span><span style="color: black;">&#40;</span>api_key,secret,username<span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">token</span>, frob<span style="color: black;">&#41;</span> = flickr.<span style="color: black;">get_token_part_one</span><span style="color: black;">&#40;</span>perms=<span style="color: #483d8b;">&quot;write&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">token</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">print</span> frob</pre></div></div>

<p>Na hora o <em>browser</em> padrão irá abrir e pedir para você confirmar o uso do aplicativo.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #808080; font-style: italic;">#/bin/python </span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">glob</span> 
<span style="color: #ff7700;font-weight:bold;">import</span> flickrapi
&nbsp;
api_key = <span style="color: #483d8b;">&quot;&lt;API&gt;&quot;</span>
secret = <span style="color: #483d8b;">&quot;&lt;CHAVE SECRETA&gt;&quot;</span>
username = <span style="color: #483d8b;">&quot;&lt;USER&gt;&quot;</span>
&nbsp;
<span style="color: #dc143c;">token</span>=<span style="color: #483d8b;">&quot;&lt;TOKEN&gt;&quot;</span>
frob=<span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> status<span style="color: black;">&#40;</span>progress, done<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> done:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Finished ;-)&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;At %s%%&quot;</span> <span style="color: #66cc66;">%</span> progress
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> upload<span style="color: black;">&#40;</span>photo<span style="color: black;">&#41;</span>:
	flickr.<span style="color: black;">upload</span><span style="color: black;">&#40;</span>photo, callback=status<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
&nbsp;
        flickr = flickrapi.<span style="color: black;">FlickrAPI</span><span style="color: black;">&#40;</span>api_key,secret,username<span style="color: black;">&#41;</span>
        flickr.<span style="color: black;">get_token_part_two</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">token</span>,frob<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        photos = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;*.jpg&quot;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">for</span> photo <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">glob</span>.<span style="color: #dc143c;">glob</span><span style="color: black;">&#40;</span>photos<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Uploading: &quot;</span>,photo
		upload<span style="color: black;">&#40;</span>photo<span style="color: black;">&#41;</span></pre></div></div>

<p>Um exemplo de uso:</p>
<pre>
python upload.py /Fotos
</pre>
<p>Utilize sua criatividade para extender essa idéia e criar aplicativos que ensinem e facilite sua vida. <img src='http://www.coding.com.br/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/uploads-de-fotos-no-flickr-usando-api-e-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter @hashunifei</title>
		<link>http://www.coding.com.br/programacao/twitter-hashunifei/</link>
		<comments>http://www.coding.com.br/programacao/twitter-hashunifei/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 04:37:12 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[unifei]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=570</guid>
		<description><![CDATA[
Sempre tive curiosidade de fazer um agregador para o Twitter, ou seja, criar um usuário que fizesse o retwitt de todos os termos que aparecem sobre determinada palavra. Para fazer um teste resolvi criar um usuário chamado @hashunifei que irá agregar o que pessoal escrever sobre a UNIFEI (Universidade Federal de Itajubá) incluindo o nome antigo [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">
Sempre tive curiosidade de fazer um agregador para o Twitter, ou seja, criar um usuário que fizesse o <em>retwitt</em> de todos os termos que aparecem sobre determinada palavra. Para fazer um teste resolvi criar um usuário chamado <a href="http://www.twitter.com/hashunifei">@hashunifei</a> que irá agregar o que pessoal escrever sobre a <a href="http://www.unifei.edu.br">UNIFEI</a> (Universidade Federal de Itajubá) incluindo o nome antigo <img src='http://www.coding.com.br/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Seguindo a filosofia <em>code less, create more</em> decidi fazer um <em>script</em> em Python que acessa a <a href="http://apiwiki.twitter.com/">API</a> do Twitter. Depois só configurar algum agendador de tarefas (ex.: <a href="http://en.wikipedia.org/wiki/Cron">cron</a>) para executar o programa de tempos em tempos. Para evitar que a cada busca os mesmos <em>twitts</em> sejam publicados há um arquivo chamado <em>.hashunifei</em> que grava o número de identificação (id) das mensagens publicadas.
</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #808080; font-style: italic;"># Tiago Maluta &lt;maluta@unifei.edu.br&gt; </span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> simplejson, <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> twitter 
&nbsp;
list_id = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
api = twitter.<span style="color: black;">Api</span><span style="color: black;">&#40;</span>username=<span style="color: #483d8b;">'USUARIO'</span>, password=<span style="color: #483d8b;">'SENHA'</span><span style="color: black;">&#41;</span>
&nbsp;
SEARCH_BASE = <span style="color: #483d8b;">'http://search.twitter.com/'</span>
url = SEARCH_BASE + <span style="color: #483d8b;">&quot;search.json?q=UNIFEI+OR+efei&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> publish<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>,text<span style="color: black;">&#41;</span>:
	twitt = <span style="color: #483d8b;">&quot;RT @&quot;</span>+<span style="color: #dc143c;">user</span>+<span style="color: #483d8b;">&quot; &quot;</span>+text
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>twitt<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">140</span>:
		twitt = twitt<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">140</span><span style="color: black;">&#93;</span>
	api.<span style="color: black;">PostUpdates</span><span style="color: black;">&#40;</span>twitt<span style="color: black;">&#41;</span>
&nbsp;
result = simplejson.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'results'</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.hashunifei&quot;</span>,<span style="color: #483d8b;">&quot;r+&quot;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f:
    data = f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    list_id = data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;,&quot;</span><span style="color: black;">&#41;</span> 
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.hashunifei&quot;</span>,<span style="color: #483d8b;">&quot;a&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> twitt <span style="color: #ff7700;font-weight:bold;">in</span> result:
&nbsp;
	text = twitt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'text'</span><span style="color: black;">&#93;</span>
	<span style="color: #dc143c;">user</span> = twitt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'from_user'</span><span style="color: black;">&#93;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">user</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;hashunifei&quot;</span>:
		<span style="color: #008000;">id</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>twitt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'id'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">id</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> list_id:	
			list_id.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#41;</span>
			f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>+<span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span>
			publish<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>,text<span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Se quiser testar o código-fonte (lembre-se de mudar o usuário e senha e a palavra da busca)</p>
<blockquote><p>
$ touch .hashunifei<br />
$ wget <a href="http://github.com/maluta/junk/raw/master/hashunifei.py">http://github.com/maluta/junk/raw/master/hashunifei.py</a>
</p></blockquote>
<p>Limitações: A mensagem é truncada em 140 caracteres (limite do twitter). </p>
<p>Eu sinceramente não sei se é assim que o pessoal faz, foi a primeira idéia que tive&#8230;, se o pessoal que entende de <em>web</em> quiser colaborar seria ótimo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/twitter-hashunifei/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Byte Order</title>
		<link>http://www.coding.com.br/programacao/byte-order/</link>
		<comments>http://www.coding.com.br/programacao/byte-order/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 22:03:58 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[arm]]></category>
		<category><![CDATA[intel]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=162</guid>
		<description><![CDATA[In the book about Linux Kernel Programming the author Robert Love demonstrated a trick to check your hardware endianness.

int x = 1;
if &#40;*&#40;char *&#41;&#38;x == 1&#41;
/* little endian */
else
/* big endian */

Using GCC you can use -mbig-endian or -mlittle-endian to generate appropriate endianess. Remember to check man pages section on your architecture (i.e: i386 and [...]]]></description>
			<content:encoded><![CDATA[<p>In the book about <em>Linux Kernel Programming</em> the author<em> Robert Love</em> demonstrated a trick to check your hardware <em>endianness</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> x <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>x <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #808080; font-style: italic;">/* little endian */</span>
<span style="color: #b1b100;">else</span>
<span style="color: #808080; font-style: italic;">/* big endian */</span></pre></div></div>

<p style="text-align: justify;">Using <a href="http://gcc.gnu.org">GCC</a> you can use <strong>-mbig-endian </strong>or <strong>-mlittle-endian</strong> to generate appropriate endianess. Remember to check man pages section on your architecture (i.e: i386 and x86-64 <span style="text-decoration: underline;">don&#8217;t</span> implement this option whilst IA-64 and ARM yes)</p>
<p style="text-align: justify;">One interesting point regards on byte swapping. Suppose that you have a file that starts with 0x<span style="color: #ff0000;">aabb</span><span style="color: #000080;">ccdd</span> referring to little endian and 0x<span style="color: #000080;">ddcc</span><span style="color: #ff0000;">bbaa</span> referring to big endian (note that I took these values and order arbitrarily). In order to use one kind of byte order in your code we need check what endianness your file was generated and if necessary rectify. This C++ code exemplifies.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> Endian <span style="color: #008000;">&#123;</span>
&nbsp;
     <span style="color: #0000ff;">int</span> _byteSwapped<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
&nbsp;
     Endian<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span> dw<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>0xaabbccddL <span style="color: #000080;">==</span> dw<span style="color: #008000;">&#41;</span> _byteSwapped <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>0xddccbbaaL <span style="color: #000080;">==</span> dw<span style="color: #008000;">&#41;</span>  _byteSwapped <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> 
        <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">throw</span> <span style="color: #FF0000;">&quot;error&quot;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #008000;">&#125;</span>
&nbsp;
     <span style="color: #0000ff;">long</span> rectify<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> dw<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>_byteSwapped<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> dw<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">char</span> result<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>dw<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span>,<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>dw<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span>,<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>dw<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>,<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>dw<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span> result<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
     <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Another way to do byte swapping is using an macro. The following example swap two bytes:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define SwapTwoBytes(data) ( (((data) &gt;&gt; 8) &amp; 0x00FF) | (((data) &lt;&lt; 8) &amp; 0xFF00) )</span></pre></div></div>

<p style="text-align: justify;">One of the best practices is provide one software that will work correctly no matter which processor Endian-architecture the code is executed on, eliminating the need to rewrite the code. Intel has a interesting <a href="http://www.intel.com/design/intarch/papers/endian.pdf">paper</a> on Converting Endian-specific to Endian-neutral Code (pg. 15-16).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/byte-order/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assembly inline</title>
		<link>http://www.coding.com.br/programacao/assembly-inline/</link>
		<comments>http://www.coding.com.br/programacao/assembly-inline/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 02:20:11 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[asm]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=444</guid>
		<description><![CDATA[I&#8217;ve made some code snippets about assembly inline with GCC. A quick search points to a lot of good documentation.

IBM DeveloperWorks about Inline asssembly for x86 in Linux
Linux Documentation Project HOWTO describing GCC Inline Assembly 

The syntax may be confusing, if you don&#8217;t understand read the documentation available. Each example are an C function with [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">I&#8217;ve made some code snippets about assembly inline with <a href="http://gcc.gnu.org/">GCC</a>. A <a href="http://www.google.com.br/search?sourceid=chrome&amp;ie=UTF-8&amp;q=assembly+inline+gcc">quick search</a> points to a lot of good documentation.</p>
<ul>
<li>IBM DeveloperWorks about <em><a href="http://www.ibm.com/developerworks/linux/library/l-ia.html" target="_blank">Inline asssembly for x86 in Linux</a></em></li>
<li>Linux Documentation Project HOWTO describin<em>g <a href="http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html">GCC Inline Assembly</a> </em></li>
</ul>
<p style="text-align: justify;">The syntax may be confusing, if you don&#8217;t understand read the documentation available. Each example are an C function with the special construct &#8220;asm&#8221; for inline assembly code. Examples:</p>
<p>Increment an value:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> inc<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm <span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;incl %0&quot;</span>
         <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;=a&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span>
         <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>value<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Exchange to numbers:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> swap<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span>
        <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;=a&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;=b&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;b&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span>
       <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Copy an vector:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> vector_copy<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>v_src<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>v_dst<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;up: lodsl;&quot;</span>
	<span style="color: #ff0000;">&quot;    stosl;&quot;</span>
	<span style="color: #ff0000;">&quot;loop up;  &quot;</span>
	<span style="color: #339933;">:</span>
	<span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;S&quot;</span><span style="color: #009900;">&#40;</span>v_src<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;D&quot;</span><span style="color: #009900;">&#40;</span>v_dst<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;%eax&quot;</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A simple copy:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> copy<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> <span style="color: #339933;">*</span>from<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>to<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
     asm <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;movl %1, %%eax;&quot;</span>
          <span style="color: #ff0000;">&quot;movl %%eax, %0;&quot;</span>
          <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;=&amp;amp;r&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>to<span style="color: #009900;">&#41;</span>
          <span style="color: #339933;">:</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>from<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A very simple implementation for strncpy:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> _strncpy<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>src<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>dst<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> <span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    asm<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;cld;&quot;</span>
        <span style="color: #ff0000;">&quot;rep movsb;&quot;</span>
        <span style="color: #339933;">:</span>
        <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;S&quot;</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 'S' == %esi</span>
	  <span style="color: #ff0000;">&quot;D&quot;</span><span style="color: #009900;">&#40;</span>dst<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// 'D' == %edi</span>
	  <span style="color: #ff0000;">&quot;c&quot;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>count<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 'c' == %ecx</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you like to try check this <a href="http://github.com/maluta/junk/blob/master/asm-inline.c">example</a>:</p>
<pre style="padding-left: 30px;">wget http://github.com/maluta/junk/raw/master/asm-inline.c
gcc -Wall -ggdb asm-inline.c -o asm-inline
./asm-inline</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/assembly-inline/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google Translate na linha de comando</title>
		<link>http://www.coding.com.br/programacao/google-translate-na-linha-de-comando/</link>
		<comments>http://www.coding.com.br/programacao/google-translate-na-linha-de-comando/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 19:54:31 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[qt]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=445</guid>
		<description><![CDATA[Isto pode ser feito de mil maneiras diferentes (e até mais simples) : usando shell script, python, php, etc. Resolvi fazer uma prova do conceito em Qt/C++ para treinar. Para executar baixe o script de instalação install-translate.sh que ira fazer o downlad dos arquivos e compilar.
]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Isto pode ser feito de mil maneiras diferentes (e até mais simples) : usando <em>shell script</em>, <em>python</em>, <em>php</em>, etc. Resolvi fazer uma prova do conceito em Qt/C++ para treinar. Para executar baixe o script de instalação <a href="http://gist.github.com/raw/242972/a00ec3c18d8c61a29a8614045c186bc883ca4771/install-translate.sh">install-translate.sh</a> que ira fazer o <em>downlad </em>dos arquivos e compilar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/google-translate-na-linha-de-comando/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Otimização</title>
		<link>http://www.coding.com.br/programacao/otimizacao/</link>
		<comments>http://www.coding.com.br/programacao/otimizacao/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 03:00:47 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[x86]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=399</guid>
		<description><![CDATA[Dentre as diversas apresentações no Linux Kongress 2009 a palestra do Felix von Leitner&#8217;s me chamou atenção. Com título &#8220;Compiler Optimization Survey&#8221; ele discutiu os aspectos relacionados a programação nos compiladores modermos e as possibilidades de otimização (slides).  A idéia básica é que:

Otimização é importante
Mas frequentemente: Código legível é mais importante
Aprenda o que seu compilador [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Dentre as diversas <a href="http://www.linux-kongress.org/2009/program.html" target="_blank">apresentações</a> no <em>Linux Kongress 2009 </em>a palestra do Felix von Leitner&#8217;s me chamou atenção. Com título &#8220;<em>Compiler Optimization Survey</em>&#8221; ele discutiu os aspectos relacionados a programação nos compiladores modermos e as possibilidades de otimização (<a href="http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf" target="_blank">slides</a>).  A idéia básica é que:</p>
<ul>
<li>Otimização é importante</li>
<li>Mas frequentemente: Código legível é mais importante</li>
<li>Aprenda o que seu compilador faz<br />
E então deixe-o fazê-lo.</li>
</ul>
<p style="text-align: justify;">Exemplo. As quatro instruções abaixo fazem a mesma coisa, zeram um registrador da CPU x86, qual é a melhor forma?</p>
<p><img class="aligncenter size-full wp-image-400" title="Zerando registros" src="http://www.coding.com.br/wp-content/uploads/2009/11/a.png" alt="Zerando registros" width="153" height="88" /></p>
<p>Analisando no GDB (x/FMT ADDRESS) temos:</p>
<pre>movl  0xb8    0x00    0x00    0x00    0x00
andl  0x83    0xe0    0x00
subl  0x29    <span style="color: #ff0000;">0xc0 </span> 
xorl  0x31    <span style="color: #ff0000;">0xc0
</span><span style="color: #000000;"> </span></pre>
<p>E agora? <strong>sub</strong> ou <strong>xor</strong>, veja que ambos produzem uma <a href="http://en.wikipedia.org/wiki/Register_renaming" target="_blank">falsa dependência</a> em %eax, mas a CPU sabe como ignorar no caso do xor.</p>
<p style="text-align: center;">Você sabia?</p>
<p style="text-align: center;">O compilador sim.</p>
<p style="text-align: left;">Se você usar o sub na prova seu professor provavelmente irá descontar alguns pontos ou te dar zero&#8230;</p>
<ul>
<li>Se você fizer uma otimizacão, teste em dados reais.</li>
<li>Se tornar o código ilegível não tornar o programa <span style="text-decoration: underline;">drasticamente</span> rápido, não faça.</li>
</ul>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 476px; width: 1px; height: 1px;">movl  0xb8    0&#215;00    0&#215;00    0&#215;00    0&#215;00<br />
andl  0&#215;83    0xe0    0&#215;00<br />
subl  0&#215;29    0xc0<br />
xorl  0&#215;31    0xc0</div>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/otimizacao/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
