Archive for the ‘Program’ Category

实现了一个SCAR的zotero translator

Posted on 五月 27th, 2009 in Program, RS | No Comments »

SCAR: Science of Cold and Arid Regions 是所里新办的一个期刊,看了几篇文章,打算摘录下来,zotero却不支持,需要自己再写一个转换器(translator),花了一下午的时间,终于搞出来了,暂时不支持搜索功能。
把下面的代码保存到zotero下的translator目录,给个名字,如SCAR.js,然后重新启动一下FIREFOX,应该就可以使用了。

{
"translatorID":"d5770df3-b41a-45cb-bb3a-261948c9af49",
"translatorType":4,
"label":"SCAR",
"creator":"Liangxu Wang<wangliangxu@gmail.com>",
"target":"http://www.scar.ac.cn/hhkxen/ch/reader",
"minVersion":"1.0",
"maxVersion":"",
"priority":100,
"inRepository":true,
"lastUpdated":"2009-5-27 22:33:00"
}

function detectWeb(doc, url) {
var articleRe = /view_abstract.aspx/;
var s = articleRe.exec(url);

if(s) {
return "journalArticle";
} else {
return "multiple";
}

return false;
}

function scrape(doc) {
var nsResolver = null;

var itemType = "journalArticle";
var newItem = new Zotero.Item(itemType);
Zotero.debug(itemType);

// 标题
var title = doc.getElementById('EnTitle').textContent;
Zotero.debug("Title:"+title);
newItem.title = title;

// 附件,网页快照
var snapName = title + " (SCAR)";
Zotero.debug(snapName);
newItem.attachments.push({document:doc, title:snapName, mimeType:"text/html"});
//Zotero.debug(doc);

//关键词
var keys=doc.getElementById('EnKeyWord');
var tags=keys.getElementsByTagName('u');
var i=0;
for(i=0;i<tags.length;i++){
newItem.tags.push(tags[i].textContent);
Zotero.debug("tag:"+tags[i].textContent);
}
//摘要
var abstract=doc.getElementById('EnAbstract');
newItem.abstractNote = Zotero.Utilities.trim(abstract.textContent);
// 出版社
newItem.publicationTitle = 'Sciences in Cold and Arid Regions';
newItem.ISSN='1674-3822';
newItem.url = doc.location.href;
//出版时间
var ref=doc.getElementById('ReferenceText').textContent;
if (ref.match(/,(\d+).+Sciences in Cold and Arid Regions,(\d+)\((\d+)\):(.+)\./))
{
newItem.volume=ref.match(/,(\d+).+Sciences in Cold and Arid Regions,(\d+)\((\d+)\):(.+)\./)[2];
newItem.issue=ref.match(/,(\d+).+Sciences in Cold and Arid Regions,(\d+)\((\d+)\):(.+)\./)[3];
newItem.pages=ref.match(/,(\d+).+Sciences in Cold and Arid Regions,(\d+)\((\d+)\):(.+)\./)[4];
newItem.date=ref.match(/,(\d+).+Sciences in Cold and Arid Regions,(\d+)\((\d+)\):(.+)\./)[1];
}
//作者
if (ref.match(/(.+),\d+\.+/))
{
authors=ref.match(/(.+),\d+\.+/)[1].split(',');
for(i=0;i<authors.length-1;i++) {
newItem.creators.push(Zotero.Utilities.cleanAuthor(authors[i], "author", true));
}
author=authors[authors.length-1].split('and');
for(i=0;i<author.length;i++) {
newItem.creators.push(Zotero.Utilities.cleanAuthor(author[i], "author", true));
}
}
Zotero.debug("finished.");
newItem.complete();
}

function doWeb(doc, url) {
var namespace = doc.documentElement.namespaceURI;
var nsResolver = null;

if(detectWeb(doc, url) == "multiple") {
Zotero.debug("Enter multiple~");
// TODO: implement the multiple function.
} else {
var urls = [url];
}
Zotero.debug(urls);
// 下面对每条url进行解析
Zotero.Utilities.processDocuments(urls, scrape, function() { Zotero.done(); });
Zotero.wait();
}

也可以直接下载:scar.js

cout float的显示问题

Posted on 四月 3rd, 2009 in Linux, Program | No Comments »

在c++,使用cout直接显示float类型的数据,在不同系统上显示方式可能有所不同。
测试代码:
#include <iostream>

using namespace std;

int main()
{
float t=0.000000277;
cout<<t<<endl;
return 0;
}

在LINUX上:

2.77e-07

而在WINDOWS上结果如下:

2.77e-007

ubuntu下使用netcdf库

Posted on 三月 26th, 2009 in Linux, Program | No Comments »

netcdf自身带了C++接口,可是按照例子,怎么也连接不了,最后发现是要加参数:

g++ -lnetcdf_c++ -lnetcdf -o test *.cpp

用PHP处理中文PDF

Posted on 十二月 20th, 2008 in Program | No Comments »

因项目需要,现在想在服务器端动态生成PDF文件,已一个PDF为模板,然后把所需的数据动态填入。
本来在使用zend framework,因此很直接的就使用zend_pdf类来进行测试,代码实现如下:

$pdf = Zend_Pdf::load($this->view->config->offline->template);
$pdf->pages = array_reverse($pdf->pages);
$style = new Zend_Pdf_Style();
$font = Zend_Pdf_Font::fontWithPath($this->view->config->offline->font, ( Zend_Pdf_Font::EMBED_DONT_SUBSET));
$pdf->pages[0]->setFont($font, 10);
$pdf->pages[0]->saveGS();
//datalist
$pdf->pages[0]->drawText(str_replace(";","\n",$datalist), 100, 570,"UTF-8");
//project
$pdf->pages[0]->drawText($formData['project'], 100, 430,"UTF-8");
$pdf->pages[0]->drawText($formData['realname'], 100, 78,"UTF-8");
$pdf->pages[0]->drawText($formData['realname'], 130, 590,"UTF-8");
$pdf->pages[0]->drawText($formData['unit'], 95, 58,"UTF-8");
$pdf->pages[0]->drawText($formData['address'], 285, 58,"UTF-8");
$pdf->pages[0]->drawText($formData['postcode'], 470, 58,"UTF-8");
$pdf->pages[0]->drawText($formData['email'], 95, 40,"UTF-8");
$pdf->pages[0]->drawText($formData['phone'], 295, 40,"UTF-8");
$t=getdate();
$pdf->pages[0]->drawText($t['year'], 465, 40,"UTF-8");
$pdf->pages[0]->drawText($t['mon'], 500, 40,"UTF-8");
$pdf->pages[0]->drawText($t['mday'], 525, 40,"UTF-8");
$pdf->pages[0]->restoreGS();
$fn=$formData['realname'].date('YmdHis').".pdf";
$pdf->save($this->view->config->offline->savepath."/".$fn);

这样,的确能完成要求,但是令人恼火的是,其把中文字体内嵌到PDF文件中去,因此导致PDF文件过大,而且导致PHP所需内存也增大(64M)。
因此,就开始尝试别的办法,开始锁定fpdf,PHP能完美支持,而且也能支持中文(所要单独下载chinese.php和chinese-unicode.php)。
但是,FPDF不支持打开已有的PDF文件作为模板,因此还需要使用fpdi进行模板的支持,还要修改fpdi,使其父类为PDF-UNICODE。

然后就可以这样使用:

$pagecount = $this->setSourceFile($this->template);
$tplidx = $this->importPage(1, '/MediaBox');
$this->addPage();
$this->useTemplate($tplidx);
$this->AddUniGBhwFont('ugb','AdobeSongStd-Light-Acro');

$this->SetFont('ugb','',$this->fontsize);
$this->setXY(45,65);
$this->Write($this->fontsize,$this->data['realname']);

其中,字形由默认的AdobeSongStd-Light改成AdobeSongStd-Light-Acro,是为了防止英文字体变宽和变窄,格式才会整齐,英文字不会挤在一起。(参考: http://bbs.erp100.com/thread-18424-1-1.html)

c++的类模板

Posted on 十二月 16th, 2008 in Program | No Comments »

c++的类模板中有一些要注意的地方:
1、除非编译器实现了export关键字,否则将模板成员函数放置在一个独立的实现文件中将无法运行。(在GCC中,就是无法连接,编译可以通过)
因为模板不是函数,它们不能单独编译。模板必须与特定的模板实例化请求一起使用。

2、在类模板的操作符重载的友元函数中,要先声明,如<<操作符,要在函数后添加一对<>符合:

template <class T>
class Matrix;
template <class T>
ostream& operator <<(ostream& os,const Matrix<T>& matrix);

template <class T>
class Matrix
{
public:
friend ostream& operator << <>(ostream& os,const Matrix<T>& matrix);
}

而+-*/等操作符的友元重载则需要定义在类声明内部,否则连接会出错。

C# 2008:From Novice to Professional

Posted on 十二月 11th, 2007 in Program | No Comments »

Apress.Beginning.C.Sharp.2008.From.Novice.to.Professional.Nov.2007
书名:《C# 2008:From Novice to Professional》
作者:Christian Gross (Author)
出版商:Apress
发行日期:2007年11月
语言:英语
ISBN-10/ISBN-13:1590598695/978-1590598696

http://www.amazon.com/exec/obidos/tg/detail/-/1590598695/

book image

粗看了一遍,介绍的比较全面,感觉不是很深入。

php5中的basename函数问题

Posted on 十一月 22nd, 2007 in Program | No Comments »

因为phpbb3中上传中文文件的时候会产生文件名丢失的问题,经过一番搜索后,发现是PHP5的问题。

phpbbchina
给出了解决方案

PHP5中的相关BUG见BUG
看样子,要在PHP6中才能解决此问题。

生成UUID/GUID

Posted on 十一月 13th, 2007 in Program | 1 Comment »

php的生成办法:

<?php
class System
{
function currentTimeMillis()
{
list($usec, $sec) = explode(" ",microtime());
return $sec.substr($usec, 2, 3);
}

}

class NetAddress
{

var $Name = 'localhost';
var $IP = '127.0.0.1';

function getLocalHost() // static
{
$address = new NetAddress();
$address->Name = $_ENV["COMPUTERNAME"];
$address->IP = $_SERVER["SERVER_ADDR"];

return $address;
}

function toString()
{
return strtolower($this->Name.'/'.$this->IP);
}

}

class Random
{
function nextLong()
{
$tmp = rand(0,1)?'-':'';
return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);
}
}

// 三段
// 一段是微秒 一段是地址 一段是随机数
class Guid
{

var $valueBeforeMD5;
var $valueAfterMD5;

function Guid()
{
$this->getGuid();
}
//
function getGuid()
{
$address = NetAddress::getLocalHost();
$this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();
$this->valueAfterMD5 = md5($this->valueBeforeMD5);
}

function newGuid()
{
$Guid = new Guid();
return $Guid;
}

function toString()
{
$raw = strtoupper($this->valueAfterMD5);
return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);
}

}
?>

然后就可以这样调用:

1. <?php
2. require_once("guid.class.php");
3. $Guid = new Guid();
4. print $Guid->toString();
5. ?>

linux下可以用uuidgen命令来生成UUID,也可以在c/c++程序里调用:

/* clear.c */
void uuid_clear(uuid_t uu);

/* compare.c */
int uuid_compare(const uuid_t uu1, const uuid_t uu2);

/* copy.c */
void uuid_copy(uuid_t dst, const uuid_t src);

/* gen_uuid.c */
void uuid_generate(uuid_t out);
void uuid_generate_random(uuid_t out);
void uuid_generate_time(uuid_t out);

/* isnull.c */
int uuid_is_null(const uuid_t uu);

/* parse.c */
int uuid_parse(const char *in, uuid_t uu);

/* unparse.c */
void uuid_unparse(const uuid_t uu, char *out);

/* uuid_time.c */
time_t uuid_time(const uuid_t uu, struct timeval *ret_tv);
int uuid_type(const uuid_t uu);
int uuid_variant(const uuid_t uu);

在ubuntu下,需要确认已经安装对应的库:

sudo aptitude install libuuid1 uuid-dev

c++中如何创建目录

Posted on 十一月 10th, 2007 in Program | No Comments »

c++中读写文件直接使用fstream就可以操作,但如何对目录进行操作?
一番搜索下来后,发现std库并不能处理目录的操作,需要使用额外库来进行操作。
windows平台下:

#include <windows.h>

CreateDirectory (char *DirName, SECURITY_ATTRIBUTES Attribs);

linux平台下:

#include <sys/stat.h>

mkdir (const char *path, mode_t mode);

一个例子
#include <sys/types.h>
#include <sys/stat.h>

int status;
...
status = mkdir("/home/cnd/mod1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

还可以使用boost的跨平台方案:

#include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
#include <iostream> // for std::cout
using boost::filesystem; // for ease of tutorial presentation;
// a namespace alias is preferred practice in real code
create_directory( "foobar" );

BTW: boost filesystem在g++下的编译方法:

g++ -lboost_filesystem ...

msnconvert 0.2 版本发布

Posted on 十一月 4th, 2007 in Linux, Program | 1 Comment »

msnconvet 0.1版本的基础添加了如下功能:

1 添加msnlogtest和pidgintest测试程序。
msnlogtest可以对一个MSN的聊天记录XML进行解析并进行显示。
pidgintest可以对pidgin的blist.xml文件进行解析和显示。
2 使用permit信息进行额外的判断。当contacts无法找到对应的信息时,再使用permit信息进行二次的判断。
3 实现了结果的输出显示。
4 添加了-a\-b参数,分别指定account\buddy。就是说,可以在指定logfile后再继续指定你的msn帐号以及对应聊天记录的好友帐号。

msnconver 0.2 source code
msnconvert 0.2 binary for ubuntu gutsy