How to print UTF-8 chars in ncurses? - unicode

Traditionally I can perfectly print this:
char str[] = "▄█▀█████";
printf(str);
However, under ncurses I cannot print this string neither with:
mvprintw(0, 0, str);
nor with:
mvaddwstr(0,0, L"▄█▀█████");
What's my mistake?

Whooh, this was quick... I already solved it; dear nowox:
You forgot to configure your locales:
#include <locale.h>
setlocale(LC_ALL, "");

Related

Coldfusion Encryption and Perl Decryption

I have a situation where I need to encrypt content in Coldfusion and then decrypt in Perl. Here's a sample Coldfusion code:
<cfscript>
input = "Amidst the roar of liberated Rome, Of nations freed, and the world overjoy'd";
encryptionKey = "8kbD1Cf8TIMvm8SRxNNfaQ==";
encryptedInput = encrypt( input, encryptionKey, "AES/ECB/PKCS5Padding", "hex" );
writeOutput( "Encrypted Input: #encryptedInput# <br />" );
</cfscript>
This produces:
27B0F3EB1286FFB462BDD3F14F5A41724DF1ED888F1BEFA7174CA981C7898ED2EF841A15CDE4332D030818B9923A2DBA0C68C8352E128A0744DF5F9FA955D3C72469FEFDAE2120DE5D74319ED666DDD0
And the Perl:
use 5.24.1;
use Crypt::ECB qw(encrypt_hex);
my $input = "Amidst the roar of liberated Rome, Of nations freed, and the world overjoy'd";
my $encryption_key = "8kbD1Cf8TIMvm8SRxNNfaQ==";
my $encrypted_input = encrypt_hex($encryption_key, 'Rijndael', $input);
say $encrypted_input;
This produces:
e220ff2efe5d41e92237622ba969f35158d20e2c9c44995d44136d928d517462980321d4d6193fe62dc942fd717128442972524207777366954e5ceb2d1812ac997e06767a27d6a0145176d717c3836b
Why is the encrypted content different? Does anyone have any insights into this?
Your encryption key is base64 encoded, but Crypt::ECB expects a raw byte string (this isn't clear from the docs, though).
use Convert::Base64;
...
my $encryption_key = decode_base64("8kbD1Cf8TIMvm8SRxNNfaQ==");
...
New output:
27b0f3eb1286ffb462bdd3f14f5a41724df1ed888f1befa7174ca981c7898ed2ef841a15cde4332d030818b9923a2dba0c68c8352e128a0744df5f9fa955d3c72469fefdae2120de5d74319ed666ddd0

How can I fix garbled multibyte text when using Text::vCard's as_string() method?

When using multibyte UTF-8 characters in a NOTE node, characters are garbled/lost around the newline.
For example:
$vcard = $address_book->add_vcard();
$vcard->version('3.0');
$vcard->FN('Tèśt Ûšér');
$vcard->NOTE('①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳');
say $vcard->as_string();
Produces:
BEGIN:VCARD
VERSION:3.0
FN:Tèśt Ûšér
NOTE:①②③④⑤⑥⑦⑧⑨⑩⑪��
�⑬⑭⑮⑯⑰⑱⑲⑳①②③④
⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯�
��⑱⑲⑳①②③④⑤⑥⑦⑧��
�⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳①
②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬�
��⑮⑯⑰⑱⑲⑳
END:VCARD
How would go about fixing this? I also posted this as an issue on the text-vcard project page. I think this is related to how the new lines are inserted (by inserting the raw bytes: \x0D\x0A), but I'm not sure.
It looks like the culprit is Text::vCard::Node->_wrap_utf8(). I was able to at least get it to stop cutting up characters by bypassing that method all together.
sub _wrap_utf8 {
my ( $self, $key, $value, $max, $newline ) = #_;
#bypass wrapping
return $key . $value;
…
}

Lex Unrecognized rule in Lex - Ubuntu

I'm writing a program in lex, and it gives me the following error:
LexInput.l:12: unrecognized rule
Line 12 is: \"([^\042\134]|"\"(.|[\n]))*\" printf("string : %s\n", yytext);
Here's my code:
%{
#include <stdio.h>
%}
L [a-zA-Z]
D [0-9]
%%
{L}({L}|{D})* printf("id : %s\n", yytext);
[a-zA-Z_][a-zA-Z0-9_]* printf("C id : %s\n", yytext);
[+-]?[0-9]+ printf("integer : %s\n", yytext);
[0-9]+"."[0-9]+(e[+-]?[0-9]+)? printf("real : %s\n", yytext);
\"([^\042\134]|"\"(.|[\n]))*\" printf("string : %s\n", yytext);
"/*"([^*]|"*"+[^*)])*"*"+"/" printf("text comment : /* ... */\n");
"//".* printf("line comment : // ... \n");
"\n" |
. ;
%%
int yywrap()
{
return 1;
}
void main()
{
yylex();
}
The line in question has an unclosed double quote, although it is written in such an obfuscated manner that it is not at all obvious.
Here's the pattern from the line:
\"([^\042\134]|"\"(.|[\n]))*\"
What you meant to write was:
\"([^\042\134]|"\\"(.|[\n]))*\"
The way it was written, the quoted string starting just after the | was never closed, because the closing " was backslash-escaped. However, there is no need to enclose backslash-escaped characters in quotes, since they are already quoted by the backslash.
So here's a possibly more readable version:
["]([^"\\]|\\(.|\n))*["]
Inside [], quote characters are not special, which is why I prefer using ["] to represent a literal double-quote. Also, you can just write \n; there is no need to enclose it in any other punctuation.

STL map in Perl using SWIG

This is duplicate of my question on SWIG mailing list.
I am trying to use stl containers in my SWIG bindings. Everything works perfectly except for stl map handling in Perl. On C++ side, I have
std::map<std::string, std::string> TryMap(const std::map<std::string, std::string> &map) {
std::map<std::string, std::string> modified(map);
modified["7"] = "!";
return modified;
}
SWIG config look like this
%module stl
%include "std_string.i"
%include "std_map.i"
%template(StringStringMap) std::map<std::string, std::string>;
%{
#include "stl.h"
%}
%include "stl.h"
In my Python script I can call TryMap this way
print dict(stl.TryMap({'a': '4'}))
and get beautiful output
{'a': '4', '7': '!'}
but in Perl I call
print Dumper stl::TryMap({'a' => '4'});
and get an error
TypeError in method 'TryMap', argument 1 of type 'std::map< std::string,std::string > const &' at perl.pl line 7.
I can actually do something like
my $map = stl::TryMap(stl::StringStringMap->new());
print $map->get('7');
and get '!', but this is not an option because there is a lot of legacy code using "TryMap" that expects normal Perl hash as its output.
I believe there is a way work this out because SWIG solves this particular problem nicely in Python and even in Perl if I use stl vectors and strings but not maps.
Is there any way to handle stl map with Perl in SWIG? I am using latest SWIG 2.0.7
UPDATE Maybe there is something wrong with perl5/std_map.i. It is too short =)
$ wc -l perl5/std_map.i python/std_map.i
74 perl5/std_map.i
305 python/std_map.i
I put your C++ function into header file as an inline function for testing.
I was then able to construct a SWIG interface that does what you are looking for. It has two key parts. Firstly I wrote a typemap that will allow either a std::map, or a perl hash to be given as input to C++ functions that expect a std::map. In the case of the latter it builds a temporary map from the perl hash to use as the argument. (Which is convenient but potentially slow). The typemap picks the correct behaviour by checking what it was actually passed in.
The second part of the solution is to map some of the C++ map's member functions onto the special functions that perl uses for overloading operations on hashes. Most of these are implemented simply with %rename where the C++ function and perl functions are compatible however FIRSTKEY and NEXTKEY don't map well onto C++'s iterators, so these were implemented using %extend and (internally) another std::map to store the iteration state of the maps we're wrapping.
There are no special typemaps implemented here for returning the maps, however there is extra behaviour via the special operations that are now implemented.
The SWIG interface looks like:
%module stl
%include <std_string.i>
%include <exception.i>
%rename(FETCH) std::map<std::string, std::string>::get;
%rename(STORE) std::map<std::string, std::string>::set;
%rename(EXISTS) std::map<std::string, std::string>::has_key;
%rename(DELETE) std::map<std::string, std::string>::del;
%rename(SCALAR) std::map<std::string, std::string>::size;
%rename(CLEAR) std::map<std::string, std::string>::clear;
%{
#include <map>
#include <string>
// For iteration support, will leak if iteration stops before the end ever.
static std::map<void*, std::map<std::string, std::string>::const_iterator> iterstate;
const char *current(std::map<std::string, std::string>& map) {
std::map<void*, std::map<std::string, std::string>::const_iterator>::iterator it = iterstate.find(&map);
if (it != iterstate.end() && map.end() == it->second) {
// clean up entry in the global map
iterstate.erase(it);
it = iterstate.end();
}
if (it == iterstate.end())
return NULL;
else
return it->second->first.c_str();
}
%}
%extend std::map<std::string, std::string> {
std::map<std::string, std::string> *TIEHASH() {
return $self;
}
const char *FIRSTKEY() {
iterstate[$self] = $self->begin();
return current(*$self);
}
const char *NEXTKEY(const std::string&) {
++iterstate[$self];
return current(*$self);
}
}
%include <std_map.i>
%typemap(in,noblock=1) const std::map<std::string, std::string>& (void *argp=0, int res=0, $1_ltype tempmap=0) {
res = SWIG_ConvertPtr($input, &argp, $descriptor, %convertptr_flags);
if (!SWIG_IsOK(res)) {
if (SvROK($input) && SvTYPE(SvRV($input)) == SVt_PVHV) {
fprintf(stderr, "Convert HV to map\n");
tempmap = new $1_basetype;
HV *hv = (HV*)SvRV($input);
HE *hentry;
hv_iterinit(hv);
while ((hentry = hv_iternext(hv))) {
std::string *val=0;
// TODO: handle errors here
SWIG_AsPtr_std_string SWIG_PERL_CALL_ARGS_2(HeVAL(hentry), &val);
fprintf(stderr, "%s => %s\n", HeKEY(hentry), val->c_str());
(*tempmap)[HeKEY(hentry)] = *val;
delete val;
}
argp = tempmap;
}
else {
%argument_fail(res, "$type", $symname, $argnum);
}
}
if (!argp) { %argument_nullref("$type", $symname, $argnum); }
$1 = %reinterpret_cast(argp, $ltype);
}
%typemap(freearg,noblock=1) const std::map<std::string, std::string>& {
delete tempmap$argnum;
}
%template(StringStringMap) std::map<std::string, std::string>;
%{
#include "stl.h"
%}
%include "stl.h"
I then adapted your sample perl to test:
use Data::Dumper;
use stl;
my $v = stl::TryMap(stl::StringStringMap->new());
$v->{'a'} = '1';
print Dumper $v;
print Dumper stl::TryMap({'a' => '4'});
print Dumper stl::TryMap($v);
foreach my $key (keys %{$v}) {
print "$key => $v->{$key}\n";
}
print $v->{'7'}."\n";
Which I was able to run successfully:
Got map: 0x22bfb80
$VAR1 = bless( {
'7' => '!',
'a' => '1'
}, 'stl::StringStringMap' );
Convert HV to map
a => 4
Got map: 0x22af710
In C++ map: a => 4
$VAR1 = bless( {
'7' => '!',
'a' => '4'
}, 'stl::StringStringMap' );
Got map: 0x22bfb20
In C++ map: 7 => !
In C++ map: a => 1
$VAR1 = bless( {
'7' => '!',
'a' => '1'
}, 'stl::StringStringMap' );
7 => !
a => 1
!
You can also tie this object to a hash, for example:
use stl;
my $v = stl::TryMap(stl::StringStringMap->new());
print "$v\n";
tie %foo, "stl::StringStringMap", $v;
print $foo{'a'}."\n";
print tied(%foo)."\n";
In theory you can write an out typemap to set up this tie automatically on return from every function call, but so far I've not succeeded in writing a typemap that works with both the tying and the SWIG runtime type system.
It should be noted that this isn't production ready code. There's a thread safety issue for the internal map and some error handling missing too that I know of. I've also not fully tested all of hash operations work from the perl side beyond what you see above. It would also be nice to make it more generic, by interacting with the swig_map_common macro. Finally I'm not a perl guru by any means and I've not used the C API much so some caution in that area would be in order.

Zend PDF charset - croatian chartchters not suported - tried everything please HELP

Here is my code to create PDF document I cant see č ć ž š đ i tried importing .TTF file but can t import and use ttf properly please help
$pdf = new Zend_Pdf();
// Add new page to the document
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
// Draw something on a page
// Set font
$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER), 20);
///tried to import TTF not working
//$goodDogCoolFont = Zend_Pdf_Font::fontWithPath('dokumenti/cro.TTF');
//$page->setFont($goodDogCoolFont, 36);
// Draw text
#
$page->setFillColor(Zend_Pdf_Color_Html::color('#990000'));
$page->drawText('Račćšđžčun za apartman AID '.$this->ukupnacjena[1]['AID'] , 10, 800, 'Windows-1250');// UTF-8 Also doesnt work
pdfData = $pdf->render();
$filename = $this->ukupnacjena[1]['OD-DO'];
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$filename.'.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
echo $pdfData;
I was facing similar issues with German Characters and found solution by replacing such characters[UMLAUTS in my situation] with its equivalent HTML Code like :
$str='German chars ü ä ö';
$str = html_entity_decode($str, ENT_COMPAT, "UTF-8");
$page->drawText($str, 115, 524,"UTF-8");
Will Print
German chars ü ä ö
May help you.....
I can see similar problems on this post. It i s actually a import problem.
Unicode characters not showing in Zend_Pdf?