viernes, 4 de febrero de 2011

Algunas cosillas sobre LaTeX (1).

Un ejemplo de cómo comenzar un fichero escrito en Latex:
\documentclass[a4paper,12pt]{article} \usepackage[T1]{fontenc} \usepackage[latin9]{inputenc} \usepackage[english,spanish]{babel} \frenchspacing  \begin{document} \end{document}


La virgulilla (~) especifica que dos palabras a pesar de llevar un espacio de por medio no se deben separar en dos líneas.

\footnote{Texto a pie de página} inserta un texto a pie de página.

\begin{verbatim}
  Texto sin formatear.                 El texto se vuelca tal cual en el formato de salida.
\end{verbatim}

Si se usa \begin{verbatim*} ...\end{verbatim} marcará los espacios en blanco.

jueves, 3 de febrero de 2011

Escribir código resaltado en Blogger, Wordpress, ... sin plug-ins

Una fantástica herramienta para añadir código resaltado sin necesidad de instalar ningún plug-in.


La única pega es que no soporta muchos lenguajes.  Un ejemplo de salida de esta herramienta es el siguiente código en Java.

public class Example { private int a; private String b; public Example() { } public static void main(String [] args) { System.out.println("http://airamrguez.blogspot.com"); } }

martes, 1 de febrero de 2011

How to implement a getchar or getch function using C++ libraries.

There is no getchar() function in C++ as it's not in iostream.h library. We can implement a getchar() function using some bash commands to implement the function under GNU/Linux. In Windows you can import the library conio.h so you don't have this problem.

Here is a sample code of getch() or getchar() function development:

For example, we create a file named nstdio.h which contains the function getch().


#include <iostream> #include <string> #include <sstream> #include <fstream> #include <cstdlib> using namespace std; static bool isGenerated = false; const string readCharScript[7] = {"#!/bin/sh" , "tput smso", "tput rmso", "oldstty=`stty -g`", "stty -icanon min 1 time 0", "dd bs=1 count=1 of=output >/dev/null 2>&1", "stty \"$oldstty\"" }; void resetOutputFile() { ofstream outputFile( "output", fstream::out ); outputFile << "$"; outputFile.close(); } void GenereFile() { resetOutputFile(); ofstream readFile( ".readone", fstream::out ); for ( int i = 0; i < 7; i++ ) readFile << readCharScript[i] << endl; readFile.close(); system("chmod ugo+x .readone"); isGenerated = true; } char getch() { if (!isGenerated) GenereFile(); system("./.readone"); const int EOL = '$'; char ch; ifstream file("output", fstream::in); while (ch == EOL) file >> ch; file.close(); resetOutputFile(); return ch; }

A file to test the getch() function called test.cpp.


#include <iostream> #include "nstdio.h" using namespace std; int main() { char ch = getch(); if (ch == '\n') cout << "Se pulsó intro" << endl; else cout << endl << "El caracter es: " << ch << endl; return 0; }



As you can see I didn't use objects, but you can encapsulate the methods in a class. The important thing is that I only use C++ libraries.

Furthermore, don't forget that C++ is a multiparadigm language, so you can use both, OO and procedural programming.

Have a nice programming!

lunes, 31 de enero de 2011

¿Cómo implementar una librería estática usando C++ en GNU/Linux?

En este post se explica lo fácil que es realizar una librería estática.
Para explicarlo, vamos a considerar un fichero que ni si quiera contiene una clase (para simplificar las cosas) que usa una llamada a una función getchar() como la de Pascal o getch(), getche() de la libreria conio.h que se usa en Windows.

Supongamos el siguiente fichero nstdio.h

#include <iostream> #include <string> #include <sstream> #include <fstream> #include <cstdlib> using namespace std; static bool isGenerated = false; const string readCharScript[7] = {"#!/bin/sh" , "tput smso", "tput rmso", "oldstty=`stty -g`", "stty -icanon min 1 time 0", "dd bs=1 count=1 of=output >/dev/null 2>&1", "stty \"$oldstty\"" }; void resetOutputFile() { ofstream outputFile( "output", fstream::out ); outputFile << "$"; outputFile.close(); } void GenereFile() { resetOutputFile(); ofstream readFile( ".readone", fstream::out ); for ( int i = 0; i < 7; i++ ) readFile << readCharScript[i] << endl; readFile.close(); system("chmod ugo+x .readone"); isGenerated = true; } char getch() { if (!isGenerated) GenereFile(); system("./.readone"); const int EOL = '$'; char ch; ifstream file("output", fstream::in); while (ch == EOL) file >> ch; file.close(); resetOutputFile(); return ch; }



Ahora un fichero nstdio.cpp que lo incluya.

#include "nstdio.h"



[airam~/Documentos/lib]-> g++ -Wall -c nstdio.cpp
[airam~/Documentos/lib]-> ar -cvq libnstdio.a nstdio.o
a - nstdio.o
[airam~/Documentos/lib]-> ar -t libnstdio.a
nstdio.o

A continuación creamos un fichero test.cpp. Este fichero debe contener las cabeceras de las funciones que se encuentran en la librería y que se van a utilizar.


#include <iostream> char getch(); using namespace std; int main() { char ch = getch(); if (ch == '\n') cout << "Se pulsó intro" << endl; else cout << endl << "El caracter es: " << ch << endl; return 0; }



y lo compilamos de la siguiente manera:

[airam~/Documentos/lib]-> g++ -o test test.cpp libnstdio.a
[airam~/Documentos/lib]-> ./test

sábado, 15 de enero de 2011

Un prompt para la consola de Linux.

Un prompt simple para la consola de Linux:


PS1="\[\033[1;33m\][\[\033[1;32m\]\u\[\033[1;32m\]\[\033[0;1;33m\]\w\[\033[0;1;33m\]]-> \[\033[0;0m\]"



Para hacerlo permanente, lo guardamos en .bashrc, por ejemplo así:

echo 'PS1="\[\033[1;33m\][\[\033[1;32m\]\u\[\033[1;32m\]\[\033[0;1;33m\]\w\[\033[0;1;33m\]]-> \[\033[0;0m\]"' > .bashrc