Programming

[Mobile version(QRCode)]
Access record[Graph / PV Info.(Past 1 day / Past 1 week) / Access from outside (Yesterday / Past 1 week) / Vistors's list]
ProfilePmail(Mail)
Inbox   /Send   /Sent
Reviews(List   /Limit)
Poll   /Agree:Got   /Sent
Fan
Works/Music
Blog
[Write]
Links
My Play List
 Created dateCategoryArticle title
12017/05/22ProgrammingComparison of Programming ..
22016/09/25ProgrammingList to be studied more
32016/03/09ProgrammingSoftware Architecture Fund..
42016/03/08ProgrammingLearning path provided by ..
52012/10/24ProgrammingC++::Iconv
62009/11/04ProgrammingSuggestion for favstar.fm ..
 Created dateNameRecent messages

1.
2017/05/22 (Updated 2017/12/08) "Programming > Comparison of Programming languages"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. Purpose of this documentation
2. Programming Language Comparison
  1. Hello, world
    1. C
    2. C++
    3. C#
    4. Go
    5. Java
    6. Javascript(node.js)
    7. Ruby
    8. Perl
    9. PHP
    10. Python
  2. Print ARGV
    1. JavaScript (node.js)
    2. Perl
    3. PHP
    4. Python 3
  3. Trim blank characters from the head and tail of the string
    1. JavaScript (node.js)
  4. Output to file
    1. Javascript (node.js)
    2. Perl
    3. PHP
    4. Python 3
  5. Array
    1. C
    2. C++
    3. C#
    4. Go
    5. Java
    6. Javascript(node.js)
    7. Ruby
    8. Perl
    9. PHP
    10. Python
  6. Associative array
    1. C
    2. C++
    3. C#
    4. Go
    5. Java
    6. Javascript(ES6)
    7. Ruby
    8. Perl
    9. PHP
    10. Python
  7. Dump associative array
    1. Javascript(node.js)
    2. Ruby
    3. Perl
    4. PHP
    5. Python 3
  8. Get the lowest value from array
    1. Javascript(node.js)
    2. Ruby
    3. Perl
    4. PHP
    5. Python 3
  9. if defined or not
    1. Javascript (node.js)
    2. Ruby
    3. Per
    4. PHP
    5. Python 3
  10. Regex match
    1. Javascript(node.js)
    2. Ruby
    3. Perl
    4. PHP
    5. Python 3
  11. Replace all - with _ in the string with lowering the string
    1. Javascript(node.js)
    2. Ruby
    3. Perl
    4. PHP
    5. Python 3
  12. Return the length of the string
    1. Javascript(node.js)
  13. Get only first 2 characters
    1. Javascript(node.js)
    2. Perl
  14. Anonymous function
    1. Javascript (node.js)
  15. Immediately invoked function without name
    1. Javascript (node.js)
  16. Loop associative array sorted by key number
    1. Javascript (node.js)
    2. Perl
    3. PHP
    4. Python3
  17. Create human class
    1. Java
    2. Perl
    3. PHP
    4. Python
  18. Create Doctor class extending human class
    1. PHP
  19. Convert vertical columns to horizontal columns
    1. JavaScript (node.js)
    2. Perl
    3. PHP
    4. Python
3. React
  1. Update parent's component state

1. Purpose of this documentation

- Make the cost of switching programming language lower by writing down basic procedure of each programming language.
- Use for questioning the understanding of programming language

I will put the code at
https://github.com/hikarine3/programming_language_comparison
in public status.
2. Programming Language Comparison


  1. Hello, world


    1. C

hello_world.c
/* 
 * cc -o hello_world hello_world.c;./hello_world;rm hello_world;
 */
#include<stdio.h>
int main() {
    printf("Hello World\n");
}

    2. C++

hello_world.cpp
/*
 * g++ -o hello_world hello_world.cpp;./hello_world;rm hello_world;   
 */
#include <iostream>
int main() {
     std::cout << "Hello World\n";
}

    3. C#

hello_world.cs
/*
* mcs hello_world.cs;mono hello_world.exe;rm hello_world.exe;
*/
using System;
public class HelloWorld {
    public void helloWorld() {
        Console.WriteLine("Hello World");
    }
    public static void Main()  {
         HelloWorld hw = new HelloWorld();
         hw.helloWorld();
    }
}

    4. Go

hello_world.go
// go build hello_world 
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}

    5. Java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

    6. Javascript(node.js)

console.log("Hello World");
    7. Ruby

print("Hello World")
    8. Perl

print("Hello World");
    9. PHP

print("Hello World");
    10. Python

print("Hello World")
  2. Print ARGV


    1. JavaScript (node.js)

console.log(process.argv[2]);
    2. Perl

print $ARGV[0];
    3. PHP

<?php
print $argv[1];

    4. Python 3

import sys
argvs = sys.argv
print(argvs[1])
  3. Trim blank characters from the head and tail of the string


    1. JavaScript (node.js)

trim.js
import assert from 'assert';
let str = "    aaa    ";
assert.equal(str, "aaa");

  4. Output to file


    1. Javascript (node.js)

// Async write
const fs = require('fs');
let output_file = "/tmp/output_js.txt";
fs.writeFile(output_file, "Hello World!", (err, resp) => {
    if (err) throw err;
    fs.readFile(output_file, (err, data) => {
      if (err) throw err;
      console.log(data.toString());
    });
});

    2. Perl

use strict;
use FileHandle;
my $output_file = "/tmp/output_perl.txt";
if (my $fh = new FileHandle('> '.$output_file)) {
  print $fh "Hello World!\n";
  $fh->close();
}

if (my $fh = new FileHandle($output_file)) {
  local $/ = undef;
  print <$fh>;
  $fh->close();
}

    3. PHP

<?php
$output_file = "/tmp/output_php.txt";
$fh = fopen($output_file, "w") or die("Failed to open ".$output_file);
fwrite($fh, "Hello World!");
fclose($fh);

$fh = fopen($output_file, "r") or die("Failed to open ".$output_file);
while(!feof($fh)) {
  echo fgets($fh);
}
fclose($fh);

    4. Python 3

###
def main():
  output_file = "/tmp/output_python.txt"
  try:
    fh = open(output_file, "w") or die("Failed to write to " + output_file)
  except IOError:
    print("Failed to write to " + output_file)
    return

  fh ._ write("Hello World!")
  fh.close()

  fh = open(output_file, "r")
  print(fh.read())
  fh.close()

if __name__ == '__main__':
  main()
###
  5. Array


    1. C

int array[] = {3, 1, 2};
    2. C++

int array[] = {3, 1, 2};
    3. C#

int [] array = new int[] {3, 1, 2};
    4. Go

array := [3]int{3, 1, 2}
    5. Java

int[] myList = {3, 1, 2};
    6. Javascript(node.js)

let array = [3, 1, 2]
    7. Ruby

array = [3, 1, 2]
    8. Perl

my @array = (3, 1, 2);
    9. PHP

$array = [3, 1, 2];
    10. Python

array = [3, 1, 2]
  6. Associative array


    1. C

N/A
    2. C++

std::map<std::string, std::string> array = {{"1", "January"}, {"2", "Feburuary"}, {"3", "March"}};
    3. C#

Dictionary<string, string> array = new Dictionary<string, string>(); 
array["1"] = "January"; 
array["2"] = "Feburuary"; 
array["3"] = "March";
    4. Go

array := map[string]string{1: 'Janualry', 2:'Feburuary', 3:'March'}
    5. Java

Map<String, String> array = new HashMap<String, String>();
map.put("1", "Janualry");
map.put("2", "Feburuary");
map.put("3", "March");
    6. Javascript(ES6)

let array = {1: 'Janualry', 2:'Feburuary', 3:'March'};
    7. Ruby

array = {1 => 'Janualry', 2 => 'Feburuary', 3 => 'March'}
    8. Perl

my $array = {1 => 'Janualry', 2 => 'Feburuary', 3 => 'March'};
    9. PHP

$array = [1 => 'Janualry', 2 => 'Feburuary', 3 => 'March'];
    10. Python

array = {1: 'Janualry', 2:'Feburuary', 3:'March'}

print(array[1])
  7. Dump associative array


    1. Javascript(node.js)

let array = {1: 'Janualry', 2:'Feburuary', 3:'March'};
console.log(array);
    2. Ruby

array = {1 => 'Janualry', 2 => 'Feburuary', 3 => 'March'}
print array.inspect
    3. Perl

use Data::Dump qw(dump);
my $array = {1 => 'Janualry', 2 => 'Feburuary', 3 => 'March'};
print dump($array);
    4. PHP

$array = [1 => 'Janualry', 2 => 'Feburuary', 3 => 'March'];
print var_dump($array);
    5. Python 3

from pprint import pprint
array = {1: 'Janualry', 2:'Feburuary', 3:'March'}
pprint(array)
  8. Get the lowest value from array


    1. Javascript(node.js)

let array = [3, 1, 2]
console.log(Math.min(...array))
    2. Ruby

array = [3, 1, 2]
print(array.min)
    3. Perl

use List::Util 'min';
my @array = (3, 1, 2);
print min(@array);
    4. PHP

$array = [3, 1, 2];
print(min($array));
    5. Python 3

array = [3, 1, 2]
print(min(array))
  9. if defined or not


    1. Javascript (node.js)

if (typeof value !== 'undefined') {
  console.log('value is defined');
} else {
  console.log('value is not defined');
}

    2. Ruby

if defined? value
  puts "value is defined"
else
  puts "value is not defined"
end

    3. Per

if (defined($value)) {
  print "value is defined.";
}
else {
  print "value is not defined";
}

    4. PHP

if (isset($value)) {
  print "value is defined";
}
else {
  print "value is not defined.";
}

    5. Python 3

try:
  value
except NameError
  print "value is not defined"
else:
  print "sure, it was defined."
  10. Regex match


    1. Javascript(node.js)

if (value.match(/target/)
    2. Ruby

if value.match("target")
    3. Perl

if ($value=~ m!target!s ) {}
    4. PHP

if (preg_match('/target/s', $value) {}
    5. Python 3

import re
if re.search( r'target', value ):

  11. Replace all - with _ in the string with lowering the string


    1. Javascript(node.js)

let str = "A-B-C";
console.log(str.replace(/-/g, "_").toLowerCase());
    2. Ruby

str = "A-B-C"
print(str.gsub("-", "_").downcase)
    3. Perl

my $str="A-B-C";
$str=~ s!-!_!gs;
print lc($str);
    4. PHP

$str="A-B-C";
echo strtolower(preg_replace("/-/", "_", $str));
    5. Python 3

str = "A-B-C"
print(str.replace("-", "_").lower())
  12. Return the length of the string


    1. Javascript(node.js)

let str = "A-B-C";
console.log(str.length);
  13. Get only first 2 characters


    1. Javascript(node.js)

let str = "1122";
console.log(str.substring(0, 2));
    2. Perl

my $str = "1122";
print substr(0, 2, $str);
  14. Anonymous function


    1. Javascript (node.js)

var something = function (a, b) {
       return a + b;
};
console.log(something(1, 2));

  15. Immediately invoked function without name


    1. Javascript (node.js)

let something = ((a, b) => a + b)(1, 2);
console.log(something);

  16. Loop associative array sorted by key number


    1. Javascript (node.js)

let example_array = new Map( [ [1, "January"], [12, "December"], [3, "March"] ].sort((a, b) => {a = a[0]; b = b[0]; return a - b;}) );
let twentyFourHoursList = '';
for (let [key, value] of example_array.entries()) {
  console.log(key + ' => ' + value);
}

    2. Perl

use strict;
my $example_array = {1 => 'Janualry', 12 => 'December', 3 => 'March' };
foreach my $key (sort {$a <=> $b} keys %{$example_array}) {
   print $key. " => " . $example_array->{$key} ."\n";
}

    3. PHP

<?php
$example_array = [1 => 'Janualry', 12 => 'December', 3 => 'March'];
ksort($example_array);
foreach ($example_array as $key => $value) {
  echo $key . " => " . $value . "\n";
}

    4. Python3

import collections
example_array = {1: "Janualry", 12:"December", 3:"March" }
example_array2 = collections.OrderedDict(sorted(example_array.items(), key=lambda t: t[0]))
for key, value in example_array2.items():
  print(str(key) + " => " + value)

  17. Create human class


    1. Java

// javac -d . Human.java;
// java jp.co.firstclass.Human;

package jp.co.firstclass;

public class Human {
    private String name;
    private String sex;

    public Human(String name, String sex){
        this.name = name;
        this.sex = sex;
    }

    public void sayName() {
        System.out.println("My name is " + this.name);
    }

    public void saySex() {
        System.out.println("My sex is " + this.sex);
    }

    public static void main(String args[]) {
        Human human = new Human("FirstName LastName", "Male");
        human.sayName();
        human.saySex();
    }
}

    2. Perl

Human.pm
package Human;
use strict;
use Data::Dumper;
use warnings;

sub new(){
  my $class = shift;
  my $op = shift;
  my $name = $op->{'name'} || "";
  my $sex = $op->{'sex'} || "";
  my $self = {'name' => $name,
  'sex' => $sex};
  return bless($self);
}

sub sayName(){
  my $self = shift;
  if($self->{'name'}){
    print "My name is ".$self->{'name'}."\n";
  }
}

sub saySex(){
  my $self = shift;
  if($self->{'sex'}){
    print "My sex is ".$self->{'sex'}."\n";
  }
}

my $pro = new Human({'name' => 'FirstName LastName', 'sex' => 'Male'});
$pro->sayName();
$pro->saySex();

    3. PHP

HumanClass.php
///////////////////
<?php
namespace FirstClass\Example;
class Human{
  private $name;
  private $sex;
  public function __construct($name, $sex){
    $this->name = $name;
    $this->sex = $sex;
  }

  public function sayName(){
    print "My name is ".$this->name."\n";
  }

  public function saySex(){
    print "My sex is ".$this->sex."\n";
  }
}

if ( !isset(debug_backtrace()[0]) ) {
  $pro = new Human('FirstName LastName', 'Male');
  $pro->sayName();
  $pro->saySex();
}
///////////////////
    4. Python

###
class Human:
 def __init__(self, opt):
  self.name = opt["name"]
  self.sex = opt["sex"]

 def sayName(self):
  print(self.name)

 def saySex(self):
  print(self.sex)

if __name__ == "__main__":
 person1 = Human({"name":"First Last", "sex":"Male"})
 person1.sayName()
 person1.saySex()
###
  18. Create Doctor class extending human class


    1. PHP

Doctor.php
/////////
<?php
namespace FirstClass\Example;
require '../human_class/HumanClass.php';
use \FirstClass\Example;

class Doctor extends Human{
  private $specialty;

  public function __construct($name, $sex, $specialty){
    parent::__construct($name, $sex);
    $this->specialty = $name;
  }

  public function saySpecialty(){
    print "My specialty is ".$this->specialty."\n";
  }
}

if (!isset(debug_backtrace()[0])) {
  $doctor1 = new Doctor('FirstName LastName', 'Male', 'Cardiology');
  $doctor1->sayName();
  $doctor1->saySex();
  $doctor1->saySpecialty();
}
/////////
  19. Convert vertical columns to horizontal columns

Please consider the case that you have the file like
1
2
3
4
5
and want to change the line from vertical to horizontal. 
    1. JavaScript (node.js)

const fs = require('fs');
console.log(fs.readFileSync(process.argv[2], 'utf-8').replace(/\n/g, "\t"));

    2. Perl

use FileHandle;
if(my $fh = new FileHandle($ARGV[0])) {
 local $/ = undef;
 my $con = <$fh>;
 $fh->close();
 $con=~ s!\n!\t!gs;
 print $con;
}

    3. PHP

<?php
if(file_exists($argv[1])){
  print(preg_replace('/\n/', "\t", file_get_contents($argv[1])));
}

    4. Python

///////////////////////////
import sys
def main():
  argvs = sys.argv
  argc = len(argvs)
  file = ""
  if argc >= 2 and argvs[1]:
    file = argvs[1]

  if file == "":
    sys.exit("Please specify input file name");

  try:
    fh = open(file)
  except IOError:
    print("Failed to open " + file)
    return
  con = fh.read()
  print(con.replace("\n", "\t"))

if __name__ == '__main__':
  main()

///////////////////////////
3. React


  1. Update parent's component state

// Pass function to child
class Parent extends React.Component {
  constructor(props) {
    super(props)

    this.handler = this.handler.bind(this)
  }

  handler(e) {
    e.preventDefault()
    this.setState({
      someVar: someValue
    })
  }

  render() {
    return <Child handler = {this.handler} />
  }
}

class Child extends React.Component {
  render() {
    return <Button onClick = {this.props.handler}/ >
  }
}


Add comment to this article

2.
2016/09/25 "Programming > List to be studied more"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

Big CategoryCategoryTo be doneStatus
Azure   
AWS   
Clean code The Art of Readable Code 
C#.Net CoreC# 6 and .NET Core 1.0  
Docker   
Gamification   
GCEGoogle Big Query  
Meteor Create stop watch appli  
MySQL    
PHPLarabelDotinstall (JP) 
 OOP 
 PHP7  
PythonMachine LearningIntroduction of Machine Learning with PythonDoing
 Serverless  
React   
Ruby on Rails    
Scrum   


Add comment to this article

3.
2016/03/09 "Programming > Software Architecture Fundamentals"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. Architecture soft skills
2. Large Codebases
What is important for achitect
  1. Architecture soft skills

Communication and negotiation skill
Technical knowledge
Business domain knowledge
Methodology and strategy
  2. Large Codebases



Add comment to this article

4.
2016/03/08 "Programming > Learning path provided by oreilly"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. About this document
2. Cources
    1. Software Architecture Fundamentals
    2. Advanced CSS & Sass
    3. Android Basics
    4. Architect and Build Big Data Applications
    5. Become Fluent in Python
    6. Beginning Java, 2nd Edition
    7. Beginning JavaScript
    8. Beginning UX Design
    9. Beginning UX Design Part 2
    10. Build Apps for Amazon Web Services
    11. C#
    12. Cisco Routing and Switching Exam Prep
    13. Preparation for CISSP Certification, Domains
    14. CSS Fundamentals
    15. Data Science with R
    16. Data Visualization
    17. Data-Informed Design
    18. Design for Mobile
    19. Designing for Physical UX
    20. Designing Hardware with Autodesk 123D, Inventor, and AutoCAD 3D
    21. Designing Web APIs
    22. Dive Deeper into PHP Programming
    23. Enterprise Java Development
    24. Git
    25. Go Fundamentals
    26. Groovy
    27. Hadoop, 2nd Edition
    28. HTML5 Fundamentals
    29. Intermediate UX Design
    30. Introduction to Flask
    31. Introduction to the Modern Front-End Web
    32. Lead and Manage a Design Team
    33. Learn to Build a Website
    34. Learn to Code for the Web
    35. Learn Web Programming with PHP
    36. Linux Sysadmin Basics
    37. Machine Learning
    38. Marketing and Consumer Research
    39. Network and Cloud Security
    40. Networking for Sysadmins
    41. Programming for Sysadmins
    42. Programming the Internet of Things
    43. Python for Data
    44. Real-Time Data Applications
    45. SolidWorks for Hardware Creators
    46. SQL and Relational Databases
    47. User Research Fundamentals
    48. Web Ops Automation and Continuous Delivery
    49. Windows System Administration

  1. About this document

Oreilly has started offering "Learning path".

One of the my target of this year is to view all videos which can be benefit for me in it.

Here is the list of the courses.
  2. Cources


    1. Software Architecture Fundamentals

2 courses | 12 hours of video training
I am viewing this course now.
    2. Advanced CSS & Sass

5 courses | 14 hours of video training
    3. Android Basics

4 courses | 15 hours of video training
    4. Architect and Build Big Data Applications

7 courses | 30 hours of video training
    5. Become Fluent in Python

3 courses | 15 hours of video training
    6. Beginning Java, 2nd Edition

4 courses | 23 hours of video training
    7. Beginning JavaScript

3 courses | 14 hours of video training
    8. Beginning UX Design

3 courses | 6 hours of video training
    9. Beginning UX Design Part 2

3 courses | 6 hours of video training
    10. Build Apps for Amazon Web Services

5 courses | 17 hours of video training
    11. C#

4 courses | 27 hours of video training
    12. Cisco Routing and Switching Exam Prep

3 courses | 24 hours of video training
    13. Preparation for CISSP Certification, Domains

4 courses | 33 hours of video training
    14. CSS Fundamentals

5 courses | 12 hours of video training
    15. Data Science with R

5 courses | 24 hours of video training
    16. Data Visualization

5 courses | 14 hours of video training
    17. Data-Informed Design

3 courses | 10 hours of video training
    18. Design for Mobile

4 courses | 12 hours of video training
    19. Designing for Physical UX

2 courses | 10 hours of video training
    20. Designing Hardware with Autodesk 123D, Inventor, and AutoCAD 3D

3 courses | 17 hours of video training
    21. Designing Web APIs

5 courses | 21 hours of video training
    22. Dive Deeper into PHP Programming

3 courses | 10 hours of video training
    23. Enterprise Java Development

5 courses | 31 hours of video training
    24. Git

5 courses | 22 hours of video training
    25. Go Fundamentals

4 courses | 17 hours of video training
    26. Groovy

3 courses | 14 hours of video training
    27. Hadoop, 2nd Edition

5 courses | 19 hours of video training
    28. HTML5 Fundamentals

4 courses | 12 hours of video training
    29. Intermediate UX Design

4 courses | 8 hours of video training
    30. Introduction to Flask

2 courses | 7 hours of video training
    31. Introduction to the Modern Front-End Web

5 courses | 13 hours of video training
    32. Lead and Manage a Design Team

2 courses | 4 hours of video training
    33. Learn to Build a Website

4 courses | 13 hours of video training
    34. Learn to Code for the Web

4 courses | 15 hours of video training
    35. Learn Web Programming with PHP

3 courses | 22 hours of video training
    36. Linux Sysadmin Basics

5 courses | 23 hours of video training
    37. Machine Learning

6 courses | 23 hours of video training
    38. Marketing and Consumer Research

3 courses | 5 hours of video training
    39. Network and Cloud Security

5 courses | 34 hours of video training
    40. Networking for Sysadmins

3 courses | 17 hours of video training
    41. Programming for Sysadmins

5 courses | 21 hours of video training
    42. Programming the Internet of Things

3 courses | 12 hours of video training
    43. Python for Data

4 courses | 19 hours of video training
    44. Real-Time Data Applications

5 courses | 26 hours of video training
    45. SolidWorks for Hardware Creators

4 courses | 18 hours of video training
    46. SQL and Relational Databases

12 courses | 62 hours of video training
    47. User Research Fundamentals

3 courses | 9 hours of video training
    48. Web Ops Automation and Continuous Delivery

3 courses | 13 hours of video training
    49. Windows System Administration

3 courses | 20 hours of video training

Add comment to this article

5.
2012/10/24 "Programming > C++ > Iconv"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. How to use iconv from C++
2. Install iconv first
3. The source code

    1. How to use iconv from C++

Here is how to use iconv from C++ code.
    2. Install iconv first

VERSION=1.14;
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-$VERSION.tar.gz
tar xvfz libiconv-$VERSION.tar.gz; 
cd libiconv-$VERSION;
./configure 
make
make install
    3. The source code

You should compile the code with the following option.
-L/usr/local/lib/ -I/usr/local/include/ -liconv

And here are the source codes.

IconvWrapper.hpp
#ifndef INCLUDED_IconvWrapper
#define INCLUDED_IconvWrapper

#include <iconv.h>
#include <string>
#include <iostream>
#include <errno.h>
#include <string.h>

using namespace std;

class IconvWrapper{
public:
 IconvWrapper();
 ~IconvWrapper();
 string from_to(string src, string fromcode, string tocode);

protected:
 iconv_t open(const char* tocode, const char* fromcode);
 void close();

private:
 iconv_t conversion_descriptor;
 char* tocode_cache;
 char* fromcode_cache;
};

#endif

IconvWrapper.cpp
#include "IconvWrapper.hpp"

using namespace std;

IconvWrapper::IconvWrapper(){
 conversion_descriptor = 0;
 tocode_cache = NULL;
 fromcode_cache = NULL;
}

IconvWrapper::~IconvWrapper(){
 close();
}

iconv_t IconvWrapper::open(const char* tocode, const char* fromcode){
 if(conversion_descriptor > 0){
 close();
 }
 iconv_t ret = iconv_open(tocode, fromcode);
 if(ret > 0) {
 conversion_descriptor = ret;
 tocode_cache = new char[strlen(tocode) + 1];
 strcpy(tocode_cache, tocode);
 fromcode_cache = new char[strlen(fromcode) + 1];
 strcpy(fromcode_cache, fromcode);
 }
 else{
 conversion_descriptor = 0;
 }
 return conversion_descriptor ;
}

void IconvWrapper::close(){
 if(conversion_descriptor > 0){
 iconv_close(conversion_descriptor );
 conversion_descriptor = 0;
 }
 
 if(tocode_cache){
 delete [] tocode_cache;
 tocode_cache = NULL;
 }
 
 if(fromcode_cache){
 delete [] fromcode_cache;
 fromcode_cache = NULL;
 }
}

string IconvWrapper::from_to(string src, string fromcode, string tocode){
 string ret;
 int break_flag=0;
 
 if (!tocode_cache || !fromcode_cache || tocode != tocode_cache || fromcode != fromcode_cache) {
 open(tocode.c_str(), fromcode.c_str());
 }
 char* inbuf = const_cast<char*>(src.c_str());
 size_t inbytesleft = src.size();

 char* dst = new char[inbytesleft*2+1];
 char* outbuf = dst;
 size_t outbytesleft = inbytesleft;
 size_t dstmax = inbytesleft;

 while (inbytesleft > 0) {
 char* inbuf_org = inbuf;
 size_t n = iconv(conversion_descriptor , &inbuf, &inbytesleft, &outbuf, &outbytesleft);
 if ((n != (size_t)-1 && inbytesleft == 0) || (errno == EINVAL)) {
 /* EINVAL = An incomplete multibyte sequence has been encountered in the input. */
 inbytesleft = 0;
 ret.append(dst, 0, dstmax-outbytesleft);
 }
 else {
 
 switch (errno) {
 case E2BIG: /* There is not sufficient room at *outbuf. And something wrong can happen, so break the loop*/
 ret.append(dst, 0, dstmax-outbytesleft);
 outbuf = dst;
 outbytesleft = dstmax;
 break_flag++;
 break;
 case EILSEQ: /* An invalid multibyte sequence has been encountered in the input. */
 ret.append(dst, 0, dstmax-outbytesleft);
 ret.append(inbuf, 0, 1);
 inbuf++;
 inbytesleft--;
 outbuf = dst;
 outbytesleft = dstmax;
 break;
 default:
 ret.append(inbuf_org);
 inbytesleft = 0;
 break;
 }
 
 if(break_flag){
 break;
 }
 }
 }
 outbuf = dst;
 outbytesleft = dstmax;
 if ( iconv(conversion_descriptor , NULL, NULL, &outbuf, &outbytesleft) != (size_t)-1) {
 ret.append(dst, 0, dstmax-outbytesleft);
 }
 delete [] dst;
 return ret;
}

Caller
#include "IconvWrapper.hpp"
...
IconvWrapper iconv;
...
string euc_string;
euc_string=iconv.from_to(sjis_string,"shift_jis", "euc-jp");

Example of make
.PHONY: all
all: LogAnalyzer

LogAnalyzer: LogAnalyzer.cpp
 g++ -Wall -O3 -finline-functions -o -L/usr/local/lib:/usr/lib -I/usr/local/include/boost -I/usr/include/boost-1_33_1/ -I/usr/local/include/ -I../common/ -o LogAnalyzer LogAnalyzer.cpp ../common/IconvWrapper.cpp ../common/CGICommon.cpp ../common/CharConverter.cpp -liconv /usr/local/lib/libboost_regex.so

.PHONY: install
install:
 mkdir -p /home/www/bin/;
 mv -f LogAnalyzer /home/www/bin/;
 cp /home/www/bin/LogAnalyzer .;

.PHONY: clean
clean:
 rm LogAnalyzer;
 rm *~;
 rm *.o;


Add comment to this article

6.
2009/11/04 "Programming > Suggestion for favstar.fm to make it internationalized"
[Show only this article / Modify / Delete / Send trackback / Add to the shared category]

1. Bug report 1
2. Bug report 2

    1. Bug report 1

Recently I started using twitter ( http://twitter.com/hikarine3/ ) basically for announcing the release of web service which I make.
In most of cases, I write in Japanese because my readers are Japanese.

I found the service http://favstar.fm/ which enables us to find the comment marked as favorite.
The service is used more in English world though http://favotter.matope.com/en/ started earlier and the author of favotter wrote the blog post in Japanese about why favstar won over favotter even though it started later and many discussion has been done.

Great growth.

Anyway, due to the discussion, I noticed the existence of http://favstar.fm/ and tried it.
It is good service, I think.
Today http://favotter.matope.com/ was down due to the distruction of database caused by power loss at his house, so considering such kind of incident, favstar can be good choice even for Japanese to use as the alternative of Favotter.
But favstar has quite bad point compared to favotter for not english users.

The sentence written in Japanese can be made shorten and the full comment is not shown.
And the character at the end of the sentence can be broken in many cases if you write long sentence as twitter's post.
http://ja.favstar.fm/users/hikarine3/recent

I think it is because favstar doesn't except correct bytes which must be allowed for storing 1 Japanese twitter's post.
In the case of English, only 140 bytes are enough because 1 character of ascii consumes only 1 byte.
But in the case of Japanese character, 1 character consumes 3 bytes in the case of utf8 character, so you have to assign 420 bytes (=140 chars x 3 bytes) for storing Japanese twitter's 1 post.
Even if it doesn't try to store full sentence, you cannot cut the sentence just seeing the length of the bytes but you have to cut the sentence to make the last character is compatible with utf8's 1 character.

UTF8's character can be expressed following way if you express it in regex.
1 byte characterAscii[\x00-\x7F]
2 bytes character [\xC0-\xDF][\x80-\xBF]
3 bytes characterJapanese and so on[\xE0-\xEF][\x80-\xBF][\x80-\xBF]
4 bytes character [\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]
5 bytes character [\xF8-\xFB][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF]
6 bytes character [\xFC-\xFD][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF]

So you can remove the corrupted character at the end of the utf8 character after cutting the sentence in the certain bytes following way.
my $utf8_sentence='一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';

my $utf8_char_regex=join('|','[\x00-\x7F]',
'[\xC0-\xDF][\x80-\xBF]',
'[\xE0-\xEF][\x80-\xBF][\x80-\xBF]',
'[\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF]',
'[\xF8-\xFB][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF]',
'[\xFC-\xFD][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF][\x80-\xBF]');

my $max_bytes=280;
print 'Before'."\t".$utf8_sentence."\n";
print 'Before cut'."\t".length($utf8_sentence)." bytes\n";

$utf8_sentence=substr($utf8_sentence, 0, $max_bytes);
print 'Cutted in '.$max_bytes." bytes \t".length($utf8_sentence)." bytes\n";
print $utf8_sentence."\n";

$utf8_sentence=~ s!^((?:$utf8_char_regex)*).*!$1!s;
print 'Final length'."\t".length($utf8_sentence)." bytes\n";
print $utf8_sentence."\n";

Anyway, it seems that favstar tries to be compatible with Japanese, so I hope it will be able to store 140 Japanese characters.
    2. Bug report 2

I encountered another issue.
http://ja.favstar.fm/users/hikarine3/recent
It seems that favstar behaves wrongly about which part should be made url.
Maybe assuming that url starts only from http:// , https:// or ftp:// will solve this issue becauase the second link is not starting from http:// in this case.

Add comment to this article
Subscribe to RSS
RSS
Display Style of blog
List/Mobile(QRCode)
Term
Category
All
1.Japan
2.Atlassian's products
3.Self
4.Development of this site
5.Japanese comics
6.Japanese anime
7.Weekly hot news of Japanese culture
8.OP/ED/PV
9.Japanese game
10.Ranking
11.Japanese Comics (Manga)
12.Search Engine
13.Japanese drama
14.Japanese otaku culture
15Programming
16.Ineternet world
17.Movie
18.C/C++
19.BerkeleyDB
20.Apache programming
21.Spam
22.Meteor
23.Marketing
24.Python
25.Scrum
26.JIRA
27.Git
28.CI
29.Jenkins
30.AWS
31.Operation
32.Singapore
33.Cloud
34.Mysql Cluster
35.DevOps
36.Bitbucket
37.Xamarin
38.Good and new
39.Monitoring
40.JavaScript(node.js)
41.React
42.Phillipines
43.Hiring
44.Python
45.SEO
46.Malaysia
47.Mongodb
48.Perl
49.Docker
50.Life hack
51.Dance
52.QA
53.Mysql
54.Digital Life Hack
Sayings from S-Cry-Ed

Rule in this world is speed. Even stupid person can write cool novel if he can spend 20 years for it.

If someone helped me, I will help him in return, which is my rule.

To become stronger, consider what is cowardliest thinking. And rebel against the thinking, which will make you stronger.



I am Japanese but working for some English sites.

Doctor Job Career
Nurse Job Career
Top Page top MetaSeachJP Works