zoukankan      html  css  js  c++  java
  • PJSIP-PJLIB(samples) (the usage of the pjlib lib) (eg:string/I/O)

     Here are some samples about  PJLIB!

    PJLIB is the basic lib of PJSIP, so we need master the lib first!

    String:

    In our project, string is often used.But in a project based on PJSIP,we should learn to use pj_str_t instead of  C string.Of course there have lots functions  for you to convert the string type.

    First you should know the struct of pj_str_t,and you need to  use the function of pj_str to creat a pj_str_t type from a normal C string.

    And lots of function are same like the C/C++ stytle,you can use them easily if you know the C/C++.

     1 //test for pjlib   heat nan
     2 // string
     3 #include<pjlib.h>
     4 #include<pj/string.h>
     5 #include<stdio.h>
     6 void compare(const pj_str_t* s1,const pj_str_t* s2)
     7 {
     8     
     9     pj_status_t status;
    10     status=pj_strcmp(s1,s2);
    11     if(status<0)
    12     {
    13         printf("s1<s2
    ");
    14     }
    15     else if(status==0)
    16     {
    17         printf("s1=s2
    ");
    18     }
    19     else 
    20     {
    21         printf("s1>s2
    ");
    22     }
    23 }
    24 int main()
    25 {
    26     char *str="this is a string";
    27     pj_status_t status;
    28     pj_str_t s1,s2,s3;
    29     status=pj_init();
    30     if(status!=PJ_SUCCESS)
    31     {
    32         printf("pj_init failed
    ");
    33     }
    34     s1=pj_str("helloworld");
    35     s2=pj_str("heat nan");
    36 
    37   
    38     printf("s1=%s
    ",s1.ptr);
    39     printf("s2=%s
    ",s2.ptr);
    40     printf("len1=%d;len2=%d
    ",s1.slen,s2.slen);
    41     //Create string initializer from a normal C string
    42     //pj_str_t pj_str     (     char *      str    ) 
    43     printf("now we use the function pj_str creat a pj_str_t type string from normal C string");
    44     s3=pj_str(str);
    45     puts(s3.ptr);
    46     printf("****************************************
    ");
    47     //
    48 
    49     //function pj_strcmp
    50     printf("now we have a compare about the the two string!
    ");
    51     compare(&s1,&s2);
    52     printf("****************************************
    ");
    53     
    54     pj_shutdown();
    55     getchar();
    56     return 0;
    57 
    58 }
    View Code

     INPUT/OUTPUT:

    FILE I/O:

    Use the functions (eg pj_file_open,pj_file_read) operating the file.

    Related documations :

    http://www.pjsip.org/docs/latest/pjlib/docs/html/group__PJ__FILE__IO.htm

    http://www.pjsip.org/docs/latest/pjlib/docs/html/group__PJ__FILE__ACCESS.htm

     1 //PJLIB I/O
     2 //file open
     3 //heat nan
     4 //describe:  open the file and write "heat nan" in it;and then read from it 
     5 #include<pjlib.h>
     6 #include<stdio.h>
     7 #define FILENAME "helloworld.txt"
     8 int main()
     9 {
    10     pj_status_t status;
    11     pj_oshandle_t fd=0;
    12     pj_size_t length;
    13     static char buffer[11]={"heat nan!"};
    14     char readbuffer[sizeof(buffer)+16];
    15     status=pj_init();//must
    16     if(status!=PJ_SUCCESS)
    17     {
    18         printf("pj_init failed!
    ");
    19     }
    20     PJ_LOG(3,(" ","file open test"));
    21     
    22     if(pj_file_exists(FILENAME))
    23     {
    24         pj_file_delete(FILENAME);
    25     }
    26 
    27     status=pj_file_open(NULL,FILENAME,PJ_O_WRONLY,&fd);
    28     if(status!=PJ_SUCCESS)
    29     {
    30         PJ_LOG(3,(" ","open file failed!"));
    31         return 0;
    32     }
    33     else
    34     {
    35         PJ_LOG(3,(" ","file open successed!"));
    36     }
    37 
    38     length=sizeof(buffer);
    39 
    40     status=pj_file_write(fd,buffer,&length);
    41     if(status!=PJ_SUCCESS)
    42     {
    43        PJ_LOG(3,(" ","write the file failed!"));
    44     }
    45 
    46     pj_file_close(fd);
    47 
    48     //now we check the file weather exist
    49     if(!pj_file_exists(FILENAME))
    50     {
    51         PJ_LOG(3,(" ","Sorry! the file you want is not exist!"));
    52         return 0;
    53     }
    54     else
    55     {
    56         PJ_LOG(3,(" ","Yeah! the file exist!"));
    57     }
    58 
    59     // now we will show you the content of the file
    60     status=pj_file_open(NULL,FILENAME,PJ_O_RDONLY,&fd);
    61     if(status!=PJ_SUCCESS)
    62     {
    63         PJ_LOG(3,(" ","open file failed!"));
    64     }
    65     length=0;
    66     while((pj_ssize_t)length<sizeof(readbuffer))
    67     {
    68         pj_ssize_t read;
    69         read=1;
    70         status=pj_file_read(fd,&readbuffer[length],&read);
    71         if(status!=PJ_SUCCESS)
    72         {
    73             PJ_LOG(3,(" ","read the file failed!"));
    74             return 0;
    75         }
    76         if(read==0)//EOF   the end of the text
    77         {
    78             break;
    79         }
    80         length+=read;
    81 
    82     }
    83     puts(readbuffer);
    84     pj_shutdown();
    85     getchar();
    86 
    87      
    88 }
    View Code
  • 相关阅读:
    js设计模式——代理模式
    js设计模式——策略模式
    js设计模式——单例模式
    Krpano vtourskin.xml 默认皮肤详解
    通过JS动态切换大场景xml
    krpano 户型地图雷达
    微信小程序开发
    CSS3的calc()使用
    Yslow
    微信分享
  • 原文地址:https://www.cnblogs.com/heat-man/p/3632671.html
Copyright © 2011-2022 走看看