You are here

Programming

OLE: How to be sure Excel recalculates your cells after you did things to them

Not so frequently asked questions and stuff: 

Situation

If you edit cells in VBA/OLE, you might want to have the whole workbook recalculated, so that you can extract data from it. This is especially true if your cells include cross-sheet formulas.

Things to try

A simple DTrace userland example on FreeBSD

Not so frequently asked questions and stuff: 

System used:

FreeBSD hostmaster1.example.com 9.1-STABLE FreeBSD 9.1-STABLE #0 r248486: Thu Jun 13 00:23:59 CEST 2013     root@hostmaster1.example.com:/usr/obj/usr/src/sys/HOSTMASTER1  amd64

Write your code

main.c

#include 
#include 
#include "main_probes.h"

int main(void) {
        unsigned int count = 0;
        while(1) {
                sleep(1);
                SUPERTEST_MACHIN_START(&count);
                printf("count: %d\n", count++);
                SUPERTEST_MACHIN_DONE();
        }
}

main_probes.d

cat main_probes.d
provider supertest {
        probe machin__start(unsigned int *);
        probe machin__done();
};

Compile

dtrace -C -h -s main_probes.d
clang -c main.c
dtrace -G -s main_probes.d main.o
clang -lelf -o prog main_probes.o main.o

Use

Start the program

./prog
count: 0
count: 1
count: 2
count: 3
count: 4
...

Check that the probes are visible

dtrace -l | grep supertest
    4 supertest4056              prog                              main machin-done
    5 supertest4056              prog                              main machin-start

Use the probes

dtrace -n ':prog::machin-*'
dtrace: description ':prog::machin-*' matched 2 probes
dtrace: buffer size lowered to 2m
CPU     ID                    FUNCTION:NAME
  0      5                main:machin-start
  0      4                 main:machin-done
  1      5                main:machin-start
  1      4                 main:machin-done
  0      5                main:machin-start
  0      4                 main:machin-done
  1      5                main:machin-start
  1      4                 main:machin-done
  1      5                main:machin-start
  1      4                 main:machin-done

How to install pecl-apd with PHP 5.4 on FreeBSD

Not so frequently asked questions and stuff: 

This was written on 2013-02-02.

Fetch source (revision was 297236 at the time).

# svn co http://svn.php.net/repository/pecl/apd/trunk

Apply the following patch:

diff -Naurp php-pecl-apd-1.0.2/apd-1.0.2/php_apd.c php-pecl-apd-1.0.2-patched/apd-1.0.2/php_apd.c
--- php-pecl-apd-1.0.2/apd-1.0.2/php_apd.c  2012-08-04 11:12:58.000000000 -0700
+++ php-pecl-apd-1.0.2-patched/apd-1.0.2/php_apd.c  2012-08-04 12:00:15.000000000 -0700
@@ -337,7 +337,7 @@ char *apd_get_active_function_name(zend_
            }
        } 
        else {
-           switch (execd->opline->op2.u.constant.value.lval) {
+           switch (execd->opline->extended_value) {
            case ZEND_EVAL:
                funcname = estrdup("eval");
                break;
@@ -441,8 +441,8 @@ static void log_time(TSRMLS_D)
        if(utime || stime || rtime) {
             int lineno = 0;
             if(EG(active_op_array)) {
-               if(EG(active_op_array)->start_op) {
-                   lineno = EG(active_op_array)->start_op->lineno;
+               if(EG(start_op)) {
+                   lineno = EG(start_op)->lineno;
                } else if(EG(active_op_array)->opcodes) {
                    lineno = EG(active_op_array)->opcodes->lineno;
                }

Configure and build:

# phpize
# ./configure
# make

Install:

# make install

Load:

zend_extension = "/usr/local/lib/php/20100525/apd.so"
apd.dumpdir="/var/tmp/apd"

How to use apache2's mod_auth_mysql with a drupal 6 user database

Not so frequently asked questions and stuff: 

Situation

You have a nice drupal 6 installation, with users and groups managed by goats or non technical people.

You have another website or system protected by apache and you want your users to be able to log in using their drupal credentials. That way the userbase can be managed through the drupal admin section and not through obscure htaccess and htgroup files.

Solution

Create a role in your drupal installation (here it's utilisateur_git) and add users into it.

Check that you fetch the information manually using a query like this one:


select users.name, users.pass from users LEFT JOIN users_roles ON (users.uid = users_roles.uid) LEFT JOIN role ON (users_roles.rid = role.rid) WHERE role.name = 'utilisateur_git';

Configure apache:

        
          AuthName MyAuthName
          AuthType Basic
          AuthBasicAuthoritative Off
          Auth_MySQL_Host 127.0.0.1
          Auth_MySQL_User mysql_ser
          Auth_MySQL_Password mysql_password
          Auth_MySQL_DB databasename
          Auth_MySQL_Password_Table "users LEFT JOIN users_roles ON (users.uid = users_roles.uid) LEFT JOIN role ON (users_roles.rid = role.rid)"
          Auth_MySQL_Password_Clause " AND role.name = 'utilisateur_git'"
          Auth_MySQL_Username_Field users.name
          Auth_MySQL_Password_Field users.pass
          Auth_MySQL_Encryption_Types PHP_MD5
          require valid-user
        

How to cast a encoded UTF-8 string into a decoded UTF-8 string in PHP

Not so frequently asked questions and stuff: 

The situation

You have some PHP strings that are encoded in utf-8 twice. Using utf8_decode or mb_convert_encoding solves the problem, but you lose the characters that aren't common with the ISO8859-1 character set.

Solution

Create a function to cast the string as binary and convert it into a utf-8 string.

/*
utf8_cast()
Packs a string into binary and convert the result into a utf-8 string.
Useful if you have a utf-8 string in iso8859-1.
ZeWaren / Erwan Martin  September 2012.
Must not be used in a production environment since random behavior can be expected if input data is invalid.
*/
function utf8_cast($str, $ignore_errors=true) {
    $result = '';
 $a = unpack('C*', $str);
  for($i=1; $i> $shift--) & 0x1) {
         $c++;
       }
       if ($c) {
           if ($c == 1) {
              #First byte of a utf-8 character is not supposed to start by 0b10xxxxxx.
                if (!$ignore_errors) {
                  return $result;
             }
               continue;
           }
           #We're dealing with a unicode character. Let's find its value.
            $unicode_value = $achar & (63 >> ($c));
         $cd = $c;
           while(--$c) {
               $unicode_value = $unicode_value 

How to know which resources are called after using apache's http server's mod_rewrite.

Not so frequently asked questions and stuff: 

The situation

You set up rewrite rules in your apache configuration.

RewriteEngine On
RewriteRule    ^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    product.php?category=$1&product=$2    [NC,L]
RewriteRule    ^blog/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    blog.php?what=$1&post=$2    [NC,L]

If you have a problem, you would like to be able to see what urls are really called (after the rewriting).

Solution

Create a new log file, containing only the requested URL and the rewrited one.

    LogFormat "%r -> %f%q" rewriting
    CustomLog /var/log/apache2/access_rewriting.log rewriting

Content:

GET /products/cake/kouignamann/ HTTP/1.1 -> /var/www/product.php?category=cake&product=kouignamann
GET /products/beverages/cider/ HTTP/1.1 -> /var/www/product.php?category=beverages&product=cider
GET /blog/life/4269/ HTTP/1.1 -> /var/www/blog.php?what=life&post=4269

You can then know which scripts are actually called, along with their arguments.

How to get ruby's strftime to have localized dates

Not so frequently asked questions and stuff: 

Here is how to use ruby's strftime in French:

require 'date'
newv = [
{'Janvier' => 1, 'Février' => 2, 'Mars' => 3, 'Avril' => 4, 'Mai' => 5, 'Juin' => 6, 'Juillet' => 7, 'Août' => 8, 'Septembre'=> 9, 'Octobre' =>10, 'Novembre' =>11, 'Décembre' => 12},
{'Dimanche' => 0, 'Lundi' => 1, 'Mardi' => 2, 'Mercredi' => 3, 'Jeudi'=> 4, 'Vendredi' => 5, 'Samedi' => 6},
{'jan' => 1, 'fév' => 2, 'mar' => 3, 'avr' => 4, 'mai' => 5, 'juin' => 6, 'juil' => 7, 'aoû' => 8, 'sep' => 9, 'oct' =>10, 'nov' =>11, 'déc' => 12},
{'dim' => 0, 'lun' => 1, 'mar' => 2, 'mer' => 3, 'jeu' => 4, 'ven' => 5, 'sam' => 6},
[nil] + %w(Janvier Février Mars Avril Mai Juin Juillet Août Septembre Octobre Novembre Décembre),
%w(Dimanche Lundi Mardi Mercredi Jeudi Vendredi Samedi),
[nil] + %w(jan fév mar avr mai juin juil aoû sep oct nov déc),
%w(dim lun mar mer jeu ven sam)
]
Date::FRENCH_MONTHS=newv[0]
Date::FRENCH_DAYS=newv[1]
Date::FRENCH_ABBR_MONTHS=newv[2]
Date::FRENCH_ABBR_DAYS=newv[3]
Date::FRENCH_MONTHNAMES=newv[4]
Date::FRENCH_DAYNAMES=newv[5]
Date::FRENCH_ABBR_MONTHNAMES=newv[6]
Date::FRENCH_ABBR_DAYNAMES=newv[7]

class Time

  def strftime_french(format)
    format = format.dup
    format.gsub!(/%a/, Date::FRENCH_ABBR_DAYNAMES[self.wday])
    format.gsub!(/%A/, Date::FRENCH_DAYNAMES[self.wday])
    format.gsub!(/%b/, Date::FRENCH_ABBR_MONTHNAMES[self.mon])
    format.gsub!(/%B/, Date::FRENCH_MONTHNAMES[self.mon])
    self.strftime(format)
  end
end

class Date

  def strftime_french(format)
    format = format.dup
    format.gsub!(/%a/, Date::FRENCH_ABBR_DAYNAMES[self.wday])
    format.gsub!(/%A/, Date::FRENCH_DAYNAMES[self.wday])
    format.gsub!(/%b/, Date::FRENCH_ABBR_MONTHNAMES[self.mon])
    format.gsub!(/%B/, Date::FRENCH_MONTHNAMES[self.mon])
    self.strftime(format)
  end
end

I did not want to override strftime, because a lot of libraries use it, and this can lead to unexpected behavior (e.g. the imap protocol use dates like 05 Feb 2012 which would get translated as 05 Fév 2012 and trigger an error).

How to kill javaw.exe

Not so frequently asked questions and stuff: 

The situation

You have some java processes running on your system but you can't stop or kill them. You tried using the task manager and sysinternals' process explorer.

The solution

Find the PID of your java process

C:\>jps -l -v
3100 sun.tools.jps.Jps -Dapplication.home=C:\Program Files\Java\jdk1.6.0_24 -Xms 8m
2904 C:\Program -Xms40m -Xmx256m -XX:MaxPermSize=96m -DXPCOM.RUNTIME=C:\Program
Files\Texas Instruments\ccsv4\DebugServer\win32 -Dxpcom.bridge.executeOnDedicate
dThread=yes -Dorg.eclipse.swt.browser.XULRunnerPath=C:\Program Files\Texas Instrument\ccsv4\DebugServer\win32 -Dosgi.instance.area.default=file:/C:/Documents and Settings/SomeUser/Mes documents/workspace/

Here we want to kill the nasty eclipse process 2904.

Use taskkill.

C:\>taskkill /F /PID 2904
Opération réussie : le processus avec PID 2904 a été terminé.

If you don't use option /F, the command will still execute successfully but nothing will happen.

How to use bazaar through a HTTP Proxy

Not so frequently asked questions and stuff: 

Bazaar Logo

How to use bazaar through a HTTP Proxy

This will quickly explain how to use bzr trough a HTTP proxy with authentication. We want to branch AND push. This requires the plugin bzr-plugins.

Windows

Download and install the plugin

Download the plugin:

>set http_proxy = http://user:pass@proxy:port
>set https_proxy = http://user:pass@proxy:port
>bzr branch lp:bzr-webdav
You have not informed bzr of your Launchpad ID, and you must do this to
write to Launchpad or access private data.  See "bzr help launchpad-login".
Branched 73 revision(s).

Copy it to C:\Program Files (x86)\Bazaar\plugins or wherever you installed bazaar, and rename it to webdav.

>move bzr-webdav webdav
        1 dir(s) moved.

>copy webdav "c:\Program Files (x86)\Bazaar\plugins"
webdav\COPYING.txt
webdav\info.py
webdav\NOTES
webdav\setup.py
webdav\TODO
webdav\webdav.py
webdav\__init__.py
        7 file(s) copied.

Install the plugin:

>cd "c:\Program Files (x86)\Bazaar\plugins\webdav"

>python.exe setup.py install
running install
running build
running build_py
copying .\info.py -> build\lib\bzrlib\plugins\webdav
copying .\webdav.py -> build\lib\bzrlib\plugins\webdav
copying .\__init__.py -> build\lib\bzrlib\plugins\webdav
copying tests\dav_server.py -> build\lib\bzrlib\plugins\webdav\tests
copying tests\test_webdav.py -> build\lib\bzrlib\plugins\webdav\tests
copying tests\__init__.py -> build\lib\bzrlib\plugins\webdav\tests
running install_lib
running install_egg_info
Removing c:\Python27\Lib\site-packages\bzr_webdav-1.12.3-py2.7.egg-info
Writing c:\Python27\Lib\site-packages\bzr_webdav-1.12.3-py2.7.egg-info

>bzr plugins
...
webdav 1.12.3dev
  An http transport, using webdav to allow pushing.
...

Use the plugin

>set http_proxy = http://user:pass@proxy:port
>bzr push http+webdav://bzr.example.org
HTTP bzr.example.org, Realm: 'SOMEREALM' username: test
HTTP test@bzr.example.org, Realm: 'SOMEREALM' password:
Pushed up to revision 7.

Ubuntu

Download and install the plugin

Configure apt to use the proxy (if necessary):

#export http_proxy = http://user:pass@proxy:port

Add the following lines to your source.list file:

deb http://ppa.launchpad.net/bzr/daily/ubuntu lucid main 
deb-src http://ppa.launchpad.net/bzr/daily/ubuntu lucid main 

Follow the link at https://launchpad.net/~bzr/+archive/daily to get the signing key: http://keyserver.ubuntu.com/pks/lookup?op=get&search=0xD702BF6B8C6C1EFD and save it to a file.

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: SKS 1.0.10

mI0ESXSSBgEEAKqURmlbPIQuqZJ6Gip/Y4hElzJzW8po0k3cQkT+xc7pqqa9QVKi2W2L5XzU
1gXKpzF+dZ+JcVlNQUNLFWeLhLF/Viw5zZTGW4evsEO1Vilp5VHS2YPmJBkibIlZ5UJ+qhaM
lOkf66mFglogG5GZqktPStO9shZfx2MqUK/t6RFBABEBAAG0I0xhdW5jaHBhZCBQUEEgZm9y
IEJhemFhciBEZXZlbG9wZXJziEYEEBECAAYFAkl3aasACgkQ/vSRAed+jpjzngCgu79TtIck
IwSTAuYbrrziZH7oFjwAoKrTHhu41jQKgFhiW7t88JA6LCUMiEYEEBECAAYFAknOwVsACgkQ
9rPTxuzZSv1+IACfVS9KKRtFUF4eS8tLvpVahzkSJsEAoMP3/ASzUP5sHa9pf/UMyzpoaC9H
iEYEEBECAAYFAknS5Z0ACgkQrZORep7Yx+rC1gCglr4g4riENgChBciGIenDAOFgpdUAn3FI
ghBiV+VuBYIE3UlNkc3aRUf5iEYEEBECAAYFAkp/uc0ACgkQuL+o2Md08jv3ogCglVdsjTpj
IVgZJuss2J/oNqAlqIoAoIDctdsxd0iX3tBodkz00+ZpOn/5iLYEEwECACAFAkl0kgYCGwMG
CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRDXAr9rjGwe/U17A/9DhXbxAzX2czO+KypV3OSz
/p5CjdnOIUbf6LHDOoH7Bxq4a3MUBve5jPe764cjmPPqYElbrtzky9byLjatUO5gSFo2M20I
LajoYUW57is5Fu+m1Y7IHRfLonC44od9XWPvpwROQ00/fqPGHmri5/mPJiaW0N3dniYu7a23
vdaVuQ==
=6i6T
-----END PGP PUBLIC KEY BLOCK-----

Import said key:

#gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --import somekey 
gpg: clé 8C6C1EFD: « Launchpad PPA for Bazaar Developers » une nouvelle signature
gpg: Quantité totale traitée: 1
gpg:         nouvelles signatures: 1
gpg: aucune clé de confiance ultime n'a été trouvée

Install bzr-webdav using the new repository (this will also fetch the latest version of bzr and bzrlib).

#apt-get update
#apt-get install bzr-webdav

Check that bzr-webdav is installed and loaded

#bzr plugins
...
webdav 1.12.2
  An http transport, using webdav to allow pushing.
...

Use bzr-webdav

# bzr branch http://bzr.example.org
HTTP bzr.example.org, Realm: 'SOMEREALM' username: test
HTTP test@bzr.example.org, Realm: 'SOMEREALM' password: 
Branched 8 revisions.                    

# #Commit something

# bzr push http+webdav://bzr.example.org
HTTP bzr.example.org, Realm: 'SOMEREALM' username: test                            
HTTP test@bzr.example.org, Realm: 'SOMEREALM' password: 
Pushed up to revision 9.   

If your proxy configuration is system wide, you can even use the explorer:
Bazaar explorer pushing trough a proxy to a webdav repository

Remember the credentials

authentication.conf:

[EXAMPLE]
scheme=http
host=bzr.example.org
path=/
user=webdav_test
password=webdav_password

Sources:

Android - Get a string (or any resource) dynamically by its name.

Not so frequently asked questions and stuff: 

res\values\truc.xml:

LapinPouletSaucisse

quelquepart.java:

for (Integer i=1; i

Pages

Subscribe to Programming