Chinaunix首页 | 论坛 | 博客
  • 博客访问: 453357
  • 博文数量: 142
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-03 21:00
文章分类

全部博文(142)

文章存档

2011年(3)

2010年(32)

2009年(107)

我的朋友

分类:

2009-11-11 10:37:11

测试时蓝牙的sync功能时,需要同步Vcard,需要同步大量的通讯资料,这时手动添加就比较麻烦了,用perl自动生成Vcard比较方便,而且节省了大量的时间。
 
自动生成vcrad需要两个文件,一个配置文件,一个源码。
 
配置文件如下:vcrad_config.txt
 
#Path means the vcards that you want to put in.
D:\testvcard
#FirstName and LastName are seperated by one space
FirstName LastName
#VcardsNumber means the vcards number that you want to generate.
10
perl源码genVcard.pl如下:
 
 

use strict;
use warnings;

use Win32::Guidgen;

my $guid0 ;
my $guid1 ;
my $guid2 ;
my $guid3 ;
my $guid4 ;
my $guid5 ;
my $guid6 ;
my $guid7 ;
my $guid8 ;
my $guid9 ;
my $guid10;
my $guid11;
my $guid12;
my $guid13;
my $guid14;
my $guid15;
my $guid16;

my $path = GetSetting("Path");
my $fileName = GetSetting("VcardName");
my $VcardsNumber = GetSetting("VcardsNumber");

my @fileName = split(/ /,$fileName);

my $i = 0;
my $greenTime;
for($i = 0; $i < $VcardsNumber; $i++)
{
    $greenTime = GenGreenTime();
    $guid0 = GetGuid();
    $guid1 = GetGuid();
    $guid2 = GetGuid();
    $guid3 = GetGuid();
    $guid4 = GetGuid();
    $guid5 = GetGuid();
    $guid6 = GetGuid();
    $guid7 = GetGuid();
    $guid8 = GetGuid();
    $guid9 = GetGuid();
    $guid10 = GetGuid();
    $guid11 = GetGuid();
    $guid12 = GetGuid();
    $guid13 = GetGuid();
    $guid14 = GetGuid();
    $guid15 = GetGuid();
    $guid16 = GetGuid();
    GenVcardFile("$path\\$fileName$i"."\.contact");
}

sub GenVcardFile {
    my $filename = shift;
    open (FH,">$filename") || die "can not open $filename";
print FH <<EOF;
<?xml version="1.0" encoding="UTF-8"?>
<c:contact c:Version="1" xmlns:c="" xmlns:xsi="" xmlns:MSP2P="/Extended/MSP2P" xmlns:MSWABMAPI="/Extended/MSWABMAPI">
    <c:Notes>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#

\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$
\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\%\$\$\$\$\$\$\$\$\$\$\$\$\$\$\$
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(
\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)</c:Notes>
    <c:Gender>Male</c:Gender>
    <c:CreationDate>$greenTime</c:CreationDate>
    <c:Extended>
        <MSWABMAPI:PropTag0x3A58101F c:ContentType="binary/x-ms-wab-mapi" c:type="binary">AwAAAEAAAABDAGgAaQBsAGQAMQAxADEAMQAxADEAMQAxADEAMQAxADEAMQAxADEAMQAxADEAMQAx
ADEAMQAxADEAMQAxAAAAQAAAAEMAaABpAGwAZAAyADIAMgAyADIAMgAyADIAMgAyADIAMgAyADIA
MgAyADIAMgAyADIAMgAyADIAMgAyADIAAAAUAAAATgBlAHcAIABDAGgAaQBsAGQAAAA=</MSWABMAPI:PropTag0x3A58101F>
    </c:Extended>
    <c:ContactIDCollection>
        <c:ContactID c:ElementID="$guid1">
            <c:Value>$guid0</c:Value>
        </c:ContactID>
    </c:ContactIDCollection>
    <c:EmailAddressCollection>
        <c:EmailAddress c:ElementID="$guid2">
            <c:Type>SMTP</c:Type>
            <c:Address>vcard1111\@atheros.com</c:Address>
            <c:LabelCollection>
                <c:Label>Preferred</c:Label>
            </c:LabelCollection>
        </c:EmailAddress>
        <c:EmailAddress c:ElementID="$guid3" xsi:nil="true"/>
    </c:EmailAddressCollection>
    <c:NameCollection>
        <c:Name c:ElementID="$guid4">
            <c:NickName>$fileName[0]$i</c:NickName>
            <c:FormattedName>$fileName[0]$i $fileName[1]</c:FormattedName>
            <c:FamilyName>$fileName[1]</c:FamilyName>
            <c:GivenName>$fileName[0]$i</c:GivenName>
        </c:Name>
    </c:NameCollection>
    <c:PhysicalAddressCollection>
        <c:PhysicalAddress c:ElementID="$guid5">
            <c:Country>China</c:Country>
            <c:PostalCode>20000000000000000000000</c:PostalCode>
            <c:Region>Shanghai</c:Region>
            <c:Locality>Shanghai of China</c:Locality>
            <c:Street>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
Zhang jiang Bi bo road
690
9\# building No. 4

Floor
\~\~\~\~\~\~\~\~\~\~\~\~\~\~
\!\!\!\!\!\!\!\!\!\!\!\!\!\!
\@\@\@\@\@\@\@\@\@\@\@\@\@\@
\#\#\#\#\#\#\#\#\#\#\#\#\#\#

\$\$\$\$\$\$\$\$\$\$\$\$\$\$
\%\%\%\%\%\%\%\%\%\%\%\%\%\%
^^^^^^^^^^^^
\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(
\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)
-------------------
++++++++++++</c:Street>
            <c:LabelCollection>
                <c:Label>Business</c:Label>
            </c:LabelCollection>
        </c:PhysicalAddress>
        <c:PhysicalAddress c:ElementID="$guid6">
            <c:Country>China</c:Country>
            <c:PostalCode>2111111</c:PostalCode>
            <c:Region>Nanjing</c:Region>
            <c:Locality>Jiangsu of China</c:Locality>
            <c:Street>\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
Zhang jiang Bi bo road
690
9\# building No. 4

Floor
\~\~\~\~\~\~\~\~\~\~\~\~\~\~
\!\!\!\!\!\!\!\!\!\!\!\!\!\!
\@\@\@\@\@\@\@\@\@\@\@\@\@\@
\#\#\#\#\#\#\#\#\#\#\#\#\#\#

\$\$\$\$\$\$\$\$\$\$\$\$\$\$
\%\%\%\%\%\%\%\%\%\%\%\%\%\%
^^^^^^^^^^^^
\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;\&amp;
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(\(
\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)\)
-------------------
++++++++++++</c:Street>
            <c:LabelCollection>
                <c:Label>Personal</c:Label>
            </c:LabelCollection>
        </c:PhysicalAddress>
    </c:PhysicalAddressCollection>
    <c:PhoneNumberCollection>
        <c:PhoneNumber c:ElementID="$guid7">
            <c:Number>021-6666666666666666666</c:Number>
            <c:LabelCollection>
                <c:Label>Pager</c:Label>
            </c:LabelCollection>
        </c:PhoneNumber>
        <c:PhoneNumber c:ElementID="$guid8">
            <c:Number>021-5555555555555555555</c:Number>
            <c:LabelCollection>
                <c:Label>Fax</c:Label>
                <c:Label>Business</c:Label>
            </c:LabelCollection>
        </c:PhoneNumber>
        <c:PhoneNumber c:ElementID="$guid9">
            <c:Number>021-4444444444444444444</c:Number>
            <c:LabelCollection>
                <c:Label>Voice</c:Label>
                <c:Label>Business</c:Label>
            </c:LabelCollection>
        </c:PhoneNumber>
        <c:PhoneNumber c:ElementID="$guid10">
            <c:Number>021-3333333333333333333</c:Number>
            <c:LabelCollection>
                <c:Label>Cellular</c:Label>
            </c:LabelCollection>
        </c:PhoneNumber>
        <c:PhoneNumber c:ElementID="$guid11">
            <c:Number>021-2222222222222222222</c:Number>
            <c:LabelCollection>
                <c:Label>Fax</c:Label>
                <c:Label>Personal</c:Label>
            </c:LabelCollection>
        </c:PhoneNumber>
        <c:PhoneNumber c:ElementID="$guid12">
            <c:Number>021-1111111111111111111</c:Number>
            <c:LabelCollection>
                <c:Label>Voice</c:Label>
                <c:Label>Personal</c:Label>
            </c:LabelCollection>
        </c:PhoneNumber>
    </c:PhoneNumberCollection>
    <c:UrlCollection>
        <c:Url c:ElementID="$guid13">
            <c:Value>http://www.itblogren.cn</c:Value>
            <c:LabelCollection>
                <c:Label>Personal</c:Label>
            </c:LabelCollection>
        </c:Url>
    </c:UrlCollection>
    <c:PositionCollection>
        <c:Position c:ElementID="$guid14">
            <c:Office>SPE in Shanghai China</c:Office>
            <c:Department>SPE of Atheros</c:Department>
            <c:JobTitle>Software Engineer</c:JobTitle>
            <c:Company>Atheros</c:Company>
            <c:LabelCollection>
                <c:Label>Business</c:Label>
            </c:LabelCollection>
        </c:Position>
    </c:PositionCollection>
    <c:PersonCollection>
        <c:Person c:ElementID="$guid15">
            <c:FormattedName>Merryyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</c:FormattedName>
            <c:LabelCollection>
                <c:Label>wab:Spouse</c:Label>
            </c:LabelCollection>
        </c:Person>
    </c:PersonCollection>
    <c:PhotoCollection>
        <c:Photo c:ElementID="$guid16">
            <c:LabelCollection>
                <c:Label>UserTile</c:Label>
            </c:LabelCollection>
        </c:Photo>
    </c:PhotoCollection>
</c:contact>


EOF
close FH || die "can not close";
}

sub GetGuid
{
    my $tmpGuid = Win32::Guidgen::create();
    if ($tmpGuid =~ /{(.*)}/)
    {
        $tmpGuid = $1;
    }
    return $tmpGuid;
}


sub GenCurrentTime
{
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
    $year = $year + 1900;
    $mon = $mon + 1;
    my $currentTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year,$mon,$mday,$hour,$min,$sec);
    return $currentTime;
}


sub GenGreenTime
{
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime;
    $year = $year + 1900;
    $mon = $mon + 1;
    my $greenTime = sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ",$year,$mon,$mday,$hour,$min,$sec);
    return $greenTime;
}

sub GetSetting
{
    my $Item = shift;
    
    open IN, "vcard_config.txt";
    my @contents = <IN>;
    close IN;
    
    my ($path, $vcardname, $vcardsNumber);
    foreach my $line (@contents)
    {
        #print "line $line \n";

        if ($line =~ /<Path>(.*)/i)
        {
            $path = $1;
        }
        elsif ($line =~ /<VcardName>(.*)/i)
        {
            $vcardname = $1;
        }
        elsif ($line =~ /<VcardsNumber>(.*)/i)
        {
            $vcardsNumber = $1;
        }
    }

    if ($Item =~ /Path/i)
    {
        return $path;
    }
    elsif ($Item =~ /VcardName/i)
    {
        return $vcardname;
    }
    elsif ($Item =~ /VcardsNumber/i)
    {
        return $vcardsNumber;
    }
    else
    {
        return 0;
    }
}



运行  就可以生产指定数量的Vcard
阅读(1165) | 评论(0) | 转发(0) |
0

上一篇:自动生成vcalendar

下一篇:疯狂的程序员3

给主人留下些什么吧!~~