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\n"); 14 } 15 else if(status==0) 16 { 17 printf("s1=s2\n"); 18 } 19 else 20 { 21 printf("s1>s2\n"); 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\n"); 33 } 34 s1=pj_str("helloworld"); 35 s2=pj_str("heat nan"); 36 37 38 printf("s1=%s\n",s1.ptr); 39 printf("s2=%s\n",s2.ptr); 40 printf("len1=%d;len2=%d\n",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("****************************************\n"); 47 // 48 49 //function pj_strcmp 50 printf("now we have a compare about the the two string!\n"); 51 compare(&s1,&s2); 52 printf("****************************************\n"); 53 54 pj_shutdown(); 55 getchar(); 56 return 0; 57 58 }
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!\n"); 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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?