當前位置:首頁 » 操作系統 » cppcheck源碼

cppcheck源碼

發布時間: 2022-05-14 01:59:24

1. 俄羅斯方塊的源程序

#include<stdio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define ESC 0x01
#define P 0x19
#define ENTER 0x1c
#define PAGEUP 0x49
#define PAGEDOWN 0x51
#define add_D 0x0d
#define add_d 0x4e
#define sub_D 0x0c
#define sub_d 0x4a
#define RST 0x13
#define PAUSE 0x19

long score;
int brick_width=20;
char str[9]={'0','0','0','0','0','0','0','0','\0'}; /*積分數組!*/
int m_speed=0;
int difficulty=0;

/************************************************/
/*以下定義的是磚塊的形狀*/
int brick [6][4][4][4]=
{
{
{{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}},
{{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}},
{{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
},
{
{{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,0,0,0},{1,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,1,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}
},
{
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}},
{{0,0,1,0},{0,1,1,0},{0,1,0,0},{0,0,0,0}},
{{0,1,1,0},{0,0,1,1},{0,0,0,0},{0,0,0,0}}
},
{
{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}},
{{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}},
{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}},
{{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}
},
{
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}},
{{0,1,1,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}
},
{
{{1,1,1,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}},
{{1,1,0,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}},
{{0,0,1,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}},
{{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}}
}
};

/*以下定義的是游戲窗口的矩陣,在游戲進行時,通過檢查此矩陣來獲知磚塊的堆積情況*/
int board[22][12]={
{2,2,2,2,2,2,2,2,2,2,2,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,0,0,0,0,0,0,0,0,0,0,2},
{2,2,2,2,2,2,2,2,2,2,2,2}
};
/*以下是程序中用到漢字的點陣——————————————————*/
char e_16[]={
/* 以下是 '俄' 的 16點陣宋體 字模,32 byte */
0x10,0x20,0x19,0xA8,0x16,0x24,0x22,0x24,
0x3F,0xFE,0x62,0x20,0xA2,0x24,0x22,0xA6,
0x23,0x2C,0x26,0x28,0x2A,0x10,0x22,0x30,
0x22,0x4A,0x2E,0x8A,0x24,0x04,0x00,0x00,
};
char luo_16[]={
/* 以下是 '羅' 的 16點陣宋體 字模,32 byte */
0x00,0x00,0x3F,0xFC,0x22,0x44,0x22,0x44,
0x22,0x44,0x3F,0xFC,0x22,0x00,0x03,0xFC,
0x06,0x08,0x0C,0x08,0x12,0x10,0x61,0x20,
0x00,0xC0,0x03,0x00,0x0C,0x00,0xF0,0x00,
};
char si_16[]={
/* 以下是 '斯' 的 16點陣宋體 字模,32 byte */
0x22,0x0C,0x22,0x70,0xFF,0x40,0x22,0x40,
0x3E,0x40,0x22,0x7E,0x22,0x48,0x3E,0x48,
0x22,0x48,0x22,0x48,0xFF,0x48,0x00,0x48,
0x24,0x88,0x62,0x88,0x83,0x08,0x00,0x08,
};
char fang_16[]={
/* 以下是 '方' 的 16點陣宋體 字模,32 byte */
0x02,0x00,0x01,0x00,0x00,0x80,0xFF,0xFE,
0x02,0x00,0x02,0x00,0x03,0xF0,0x02,0x10,
0x04,0x10,0x04,0x10,0x08,0x10,0x08,0x10,
0x10,0x10,0x20,0x90,0xC0,0x60,0x00,0x00,
};
char kuai_16[]={
/* 以下是 '塊' 的 16點陣宋體 字模,32 byte */
0x20,0x80,0x20,0x80,0x20,0x80,0x23,0xF8,
0xF8,0x88,0x20,0x88,0x20,0x88,0x20,0x88,
0x2F,0xFE,0x20,0x80,0x39,0x40,0xE1,0x20,
0x42,0x18,0x02,0x0E,0x04,0x04,0x08,0x00,
};
char kai_16[]={
/* 以下是 '開' 的 16點陣宋體 字模,32 byte */
0x00,0x00,0x7F,0xFE,0x04,0x20,0x04,0x20,
0x04,0x20,0x04,0x20,0xFF,0xFE,0x04,0x20,
0x04,0x20,0x04,0x20,0x08,0x20,0x08,0x20,
0x10,0x20,0x20,0x20,0x40,0x20,0x00,0x00,
};
char shi_16[]={
/* 以下是 '始' 的 16點陣宋體 字模,32 byte */
0x10,0x20,0x18,0x30,0x10,0x20,0x10,0x48,
0xFC,0x84,0x25,0xFE,0x24,0x84,0x44,0x00,
0x44,0xFC,0x28,0x84,0x18,0x84,0x14,0x84,
0x22,0x84,0x42,0xFC,0x80,0x84,0x00,0x00,
};
char tui_16[]={
/* 以下是 '退' 的 16點陣宋體 字模,32 byte */
0x40,0x00,0x27,0xF0,0x24,0x10,0x07,0xF0,
0x04,0x10,0x04,0x10,0xE7,0xF0,0x24,0x88,
0x24,0x50,0x24,0x20,0x25,0x10,0x26,0x18,
0x24,0x08,0x50,0x00,0x8F,0xFE,0x00,0x00,
};
char chu_16[]={
/* 以下是 '出' 的 16點陣宋體 字模,32 byte */
0x01,0x00,0x01,0x00,0x21,0x04,0x21,0x04,
0x21,0x04,0x21,0x04,0x3F,0xFC,0x21,0x04,
0x01,0x00,0x21,0x04,0x21,0x04,0x21,0x04,
0x21,0x04,0x3F,0xFC,0x20,0x04,0x00,0x00,
};
char you_16[]={
/* 以下是 '游' 的 16點陣宋體 字模,32 byte */
0x44,0x20,0x22,0x20,0x22,0x20,0x0F,0xBE,
0x84,0x40,0x54,0xBC,0x57,0x08,0x25,0x10,
0x25,0x10,0x25,0x7E,0xC5,0x10,0x49,0x10,
0x49,0x10,0x49,0x10,0x55,0x50,0x42,0x20,
};
char xi_16[]={
/* 以下是 '戲' 的 16點陣宋體 字模,32 byte */
0x00,0x40,0x00,0x50,0x7E,0x48,0x02,0x48,
0x44,0x40,0x25,0xFE,0x18,0x40,0x08,0x44,
0x0C,0x28,0x14,0x30,0x12,0x20,0x22,0x50,
0x41,0x92,0x06,0x0A,0x00,0x06,0x00,0x02,
};
char xia_16[]={
/* 以下是 '下' 的 16點陣宋體 字模,32 byte */
0x00,0x04,0x7F,0xFE,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0xC0,0x01,0x60,0x01,0x30,
0x01,0x20,0x01,0x00,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
};
char ge_16[]={
/* 以下是 '個' 的 16點陣宋體 字模,32 byte */
0x01,0x00,0x01,0x00,0x02,0x80,0x02,0x40,
0x04,0x20,0x09,0x18,0x11,0x0E,0x61,0x04,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
};
char tu_16[]={
/* 以下是 '圖' 的 16點陣宋體 字模,32 byte */
0x00,0x00,0x7F,0xFC,0x44,0x04,0x47,0xE4,
0x44,0x44,0x4A,0x84,0x71,0x04,0x42,0xC4,
0x4C,0x3C,0x71,0x14,0x40,0x84,0x42,0x04,
0x41,0x84,0x41,0x04,0x7F,0xFC,0x40,0x04,
};
char xing_16[]={
/* 以下是 '形' 的 16點陣宋體 字模,32 byte */
0x00,0x04,0x7F,0x86,0x12,0x0C,0x12,0x10,
0x12,0x20,0x12,0x08,0xFF,0xCC,0x12,0x18,
0x12,0x20,0x12,0x44,0x12,0x86,0x12,0x0C,
0x22,0x10,0x22,0x20,0x42,0x40,0x80,0x80,
};
char fen_16[]={
/* 以下是 '分' 的 16點陣宋體 字模,32 byte */
0x08,0x80,0x0C,0x80,0x08,0x40,0x10,0x20,
0x10,0x30,0x20,0x18,0x40,0x0E,0x9F,0xE4,
0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,
0x08,0x20,0x10,0xA0,0x20,0x40,0x40,0x00,
};
char shu_16[]={
/* 以下是 '數' 的 16點陣宋體 字模,32 byte */
0x08,0x20,0x49,0x30,0x2A,0x20,0x1C,0x20,
0xFF,0x7E,0x1C,0x44,0x2B,0x44,0x48,0xC4,
0x08,0x28,0xFF,0x28,0x12,0x10,0x34,0x10,
0x0C,0x28,0x32,0x4E,0xC0,0x84,0x00,0x00,
};
char bd_16[]={
/* 以下是 ':' 的 16點陣宋體 字模,32 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,
0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,
};
char su_16[]={
/* 以下是 '速' 的 16點陣宋體 字模,32 byte */
0x00,0x80,0x40,0x80,0x2F,0xFC,0x20,0x80,
0x00,0x80,0x07,0xF8,0xE4,0x88,0x24,0x88,
0x27,0xF8,0x21,0xA0,0x22,0x98,0x2C,0x88,
0x20,0x80,0x50,0x80,0x8F,0xFE,0x00,0x00,
};
char _16[]={
/* 以下是 '度' 的 16點陣宋體 字模,32 byte */
0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,
0x22,0x20,0x2F,0xFC,0x22,0x20,0x23,0xE0,
0x20,0x00,0x27,0xF8,0x22,0x10,0x21,0x20,
0x20,0xC0,0x41,0x30,0x46,0x0E,0x98,0x04,
};
char cong_16[]={
/* 以下是 '從' 的 16點陣宋體 字模,32 byte */
0x08,0x40,0x08,0x40,0x08,0x40,0x08,0x40,
0x08,0x40,0x08,0x40,0x08,0x40,0x08,0xA0,
0x14,0xA0,0x12,0x90,0x12,0x90,0x21,0x08,
0x23,0x0C,0x42,0x06,0x84,0x04,0x08,0x00,
};
char xin_16[]={
/* 以下是 '新' 的 16點陣宋體 字模,32 byte */
0x10,0x00,0x08,0x0E,0x7F,0x70,0x22,0x40,
0x14,0x40,0xFF,0x7E,0x08,0x48,0x08,0x48,
0xFF,0x48,0x08,0x48,0x2C,0x48,0x2A,0x88,
0x4A,0x88,0x89,0x08,0x2A,0x08,0x10,0x08,
};
char nan_16[]={
/* 以下是 '難' 的 16點陣宋體 字模,32 byte */
0x00,0xA0,0x00,0x90,0xF9,0x10,0x09,0xFE,
0x8B,0x20,0x4B,0x20,0x2D,0xFC,0x11,0x20,
0x19,0x20,0x29,0xFC,0x25,0x20,0x45,0x20,
0x81,0x20,0x01,0xFE,0x01,0x00,0x01,0x00,
};
char xuan_16[]={
/* 以下是 '選' 的 16點陣宋體 字模,32 byte */
0x00,0x40,0x22,0x40,0x12,0x40,0x13,0xF8,
0x04,0x40,0x00,0x40,0xF7,0xFC,0x11,0x20,
0x11,0x20,0x12,0x24,0x12,0x24,0x14,0x1C,
0x10,0x00,0x28,0x00,0x47,0xFE,0x00,0x00,
};
char ze_16[]={
/* 以下是 '擇' 的 16點陣宋體 字模,32 byte */
0x20,0x00,0x23,0xF8,0x21,0x10,0xFC,0xA0,
0x20,0x40,0x20,0xA0,0x2B,0x58,0x30,0x46,
0x63,0xF8,0xA0,0x40,0x20,0x40,0x27,0xFE,
0x20,0x40,0x20,0x40,0xA0,0x40,0x40,0x40,
};

/* 函數申明 */
int getkey();
void draw_brick(int kind,int stat,int start_x,int start_y,int *position);
void display_brick(int *position,int xexcursion,int yexcursion);
void draw_freezebrick(int *space);
void draw_mainboard();
int is_movable(int *position,int *space,int dir);
void freeze(int *position,int *space);
int check_bottom(int *space,int hei);
void ERASE(int top,int flag);
int move_down(int *position,int *space,int *top,int *flag,int speed);
void begin();
void rand_select(int *kind,int *stat);
void updata_data(int *space,int *top,int flag);
void preview(int kind_new,int stat_new);
void convert_score(long score,char *str);
void drawmat(char *,int,int,int,int);
/**************************************************/

/**********************************************************/
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指針、點陣大小、起始坐標(x,y)、顏色*/
{
int i, j, k, n;
n = (matsize - 1) / 8 + 1;
for(j = 0; j < matsize; j++)
for(i = 0; i < n; i++)
for(k = 0;k < 8; k++)
if(mat[j * n + i] & (0x80 >> k)) /*測試為1的位則顯示*/
putpixel(x + i * 8 + k, y + j, color);
}

/*初始化游戲板巨陣!*/
void init_data(int **space)
{
int hei,wid,i,j;
*space=(int *)malloc(264*sizeof(int));
/*分配的空間,"264" 是數組的長度*/
for(i=0;i<difficulty;i++)
for(j=0;j<12;j++)
board[21-i][j]=random(2);
for(hei=0;hei<22;hei++)
for(wid=0;wid<12;wid++)
*(*space+hei*12+wid)=board[hei][wid];

/*初始化*/
score=0;
}

void convert_score(long score,char *str)
{
int i;
long SCORE;
SCORE=score;
for(i=7;i>=0;i--)
{
str[i]='0'+SCORE%10;
SCORE /=10;
}
}
/**************************************************/

/*獲取鍵盤掃描碼 */
int getkey()
{
union REGS key;
key.h.ah=0;
int86(0x16,&key,&key);
return key.h.ah;
}

/*****************************************************/

/*畫方格圖形*/
void mydraw(int start_x,int start_y,int brick_width,int color)
{
settextstyle(4,0,5);
setfillstyle(6,8);
setcolor(color);
rectangle(start_x,start_y,start_x+brick_width,start_y+brick_width);
rectangle(start_x+2,start_y+2,start_x+brick_width-2,start_y+brick_width-2);
outtextxy(start_x+7,start_y+7,"*");
}

/******************************************************/

/*
用mydraw函數和position數組在屏幕上畫方塊*/
void draw_brick(int kind,int stat,int start_x,int start_y,int *position)
{
int wid,hei,x,y,i=0;
void *buf;
setviewport(100,40,300,440,1);
buf=malloc(400);
for(i=0;i<8;i+=2)
{
x=position[i];y=position[i+1];
getimage(x,y,x+brick_width,y+brick_width,buf);
putimage(x,y,buf,XOR_PUT);
}
i=0;
for(hei=0;hei<4;hei++)
{
y=start_y+hei*brick_width;
for(wid=0;wid<4;wid++)
if(brick[kind][stat][hei][wid]==1)
{
x=start_x+wid*brick_width;
position[i++]=x;
position[i++]=y;
position[10]= x > position[10] ? x : position[10];
position[11]= y > position[11] ? y : position[11];
mydraw(x,y,brick_width,10);
}
}
position[8]=start_x;
position[9]=start_y;

free(buf); /* 釋放 "buf" 所佔的內存*/
}
void display_score(char *pstr) /*顯示積分和速度(10000分加一級,最大9級)*/
{
void *buf;int n,j;char speed[2];
buf=malloc(3200);
itoa(m_speed,&speed,10);/*需要改進*/
setviewport(479,139,560,170,1);
getimage(0,0,80,40,buf);
putimage(0,0,buf,XOR_PUT);
settextstyle(0,0,1);
setcolor(WHITE);
outtextxy(0,0,pstr);
outtextxy(0,22,speed);
if(m_speed<=9)
{
n=atoi(pstr);
m_speed+=n/10000;
}
free(buf);

}

2. 大家在linux上用什麼軟體看cpp的源碼

如果是只有幾個cpp文件,用文本編輯器軟體查看源碼就可以了,根據個人喜好來選擇,常見的編輯器比如gedit、VScode、Sublime Text都可以;如果是一個項目,裡面有很多源碼文件,經常需要在不同的源碼文件之間切換,用文本編輯器軟體查看就沒有那麼方便了,這時推薦用專門的源碼閱讀軟體Source Insight,這個軟體只有Windows版,但是它在Linux的Wine環境中運行的非常好,所以在Linux上安裝一個wine就可以用這個軟體了,這個軟體可以很方便的跳轉到變數、函數、類的定義處,還能前進、後退,閱讀源碼非常方便。

3. 我下載了一個小游戲源碼,裡面有好幾個CPP文件這是為什麼

不是的,一個程序一個的那是main函數,多個cpp文件是對整個程序分塊編程,這樣你看主程序的時候會比較清晰,而不是一大團代碼擠在一塊,並且這么多cpp文件中只有一個main函數

4. 如何用c++輸出該程序本身的源代碼

//這個很簡單,比如xxx.cpp文件

#include<stdio.h>
FILE*a=fopen("xxx.cpp","r");
intmain()
{charc;
/*
很多代碼。。。
*
/

while(!feof(a))
{
c=fgetc(a);
putchar(c);
}
getchar();
}

//這樣就可以了

5. 有什麼工具可以檢測 C++ 代碼抄襲嗎

本文淺談一下C++內存泄漏的檢測,首先我們需要知道程序有沒有內存泄露,然後定位到底是哪行代碼出現內存泄露了,這樣才能將其修復。最簡單的方法當然是藉助於專業的檢測工具,比較有名如BoundsCheck工具,功能非常強大,相信做C++開發的人都離不開它。此外就是不使用任何工具,而是自己來實現對內存泄露的監控,分如下兩種情況:一.在MFC中檢測內存泄漏假如是用MFC的程序的話,很簡單。默認的就有內存泄露檢測的功能。我們用VS2005生成了一個MFC的對話框的程序,發現他可以自動的檢測內存泄露.不用我們做任何特殊的操作.仔細觀察,發現在每個CPP文件中,都有下面的代碼:#ifdef_DEBUG#definenewDEBUG_NEW#endifDEBUG_NEW這個宏定義在afx.h文件中,就是它幫助我們定位內存泄漏。在含有以上代碼的cpp文件中分配內存後假如沒有刪除,那麼停止程序的時候,VisualStudio的Output窗口就會顯示如下的信息了:Detectedmemoryleaks!Dumpingobjects->d:\code\mfctest\mfctest.cpp(80):{157}normalblockat0x003AF170,4byteslong.Data:00000000Objectmpcomplete.在Output窗口雙擊粗體字那一行,那麼IDE就會打開該文件,定位到該行,很容易看出是哪出現了內存泄露。二.檢測純C++的程序內存泄露我試了下用VisualStudio建立的Win32ConsoleApplication和Win32Project項目,結果都不能檢測出內存泄露。下面一步一步來把程序的內存泄露檢測的機制建立起來。首先,我們需要知道C運行庫的Debug版本提供了許多檢測功能,使得我們更容易的Debug程序。在MSDN中有專門的章節講這個,叫做DebugRoutines,建議大家先看看裡面的內容吧。我們會用到裡面很重要的幾個函數。

6. c++編寫的.cpp源代碼怎麼執行

菜單build目錄下面點擊build solution,然後點擊Debug下的start就可以了

7. 在VC++6.0中編譯下面的代碼後怎麼解決出現的那個問題

  1. 確保兩個源代碼文件在同一目錄下;

  2. 確認文件都已經加入到同一工程中;

  3. 確認工程文件已保存。

8. cppcheck怎麼安裝和實用

首先先下載 成功後:
編譯:
g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp
安裝:
make install
cppcheck默認安裝會報錯如下:
cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.
(information) Failed to load std.cfg. Your Cppcheck installation is broken, please re-install. The Cppcheck binary was compiled without CFGDIR set. Either the std.cfg should be available in cfg or the CFGDIR should be configured.
因此我直接不用他們的make,而是直接使用g++參數編譯:g++ -o cppcheck -Ilib cli/*.cpp lib/*.cpp
然後就是檢查了
cppcheck -j 3 --enable=all src/
使能檢查規則:
默認:--enable=error
--enable=all
--enable=unusedFuntion path
--enable=style
規則定義:
error:出現的錯誤
warning:為了預防bug防禦性編程建議信息
style:編碼格式問題(沒有使用的函數、多餘的代碼等)
portablity:移植性警告。該部分如果移植到其他平台上,可能出現兼容性問題
performance:建議優化該部分代碼的性能
information:一些有趣的信息,可以忽略不看的。
保存結果到文件中:重定向「>」
-j 3 使用3個線程,如果代碼工程太大,可以使用15-20個,自己隨意發揮,不過還是參考cpuinfo
如果是Windows的版本呢,就這樣使用:
1、將Cppcheck綠色版cppcheck.rar解壓
2、打開cppcheckgui.exe;
3、菜單 check->directory;
4、選擇源代碼目錄,確認;
5、Cppcheck自動開始走查;

9. 求一份c語言的RTP音頻傳輸源碼

1.項目前期工作(配置好環境)
2.發送端文件編寫(見下面的send.cpp)
3.接收端文件編寫(見下面的receive.cpp)
4.編譯文件
(1)發送端
g++-osendsend.cpp-I/usr/local/include/jrtplib3/-ljrtp
(2)接收端
g++-oreceivereceive.cpp-I/usr/local/include/jrtplib3/-ljrtp
附錄:
(1)send.cpp
[cpp]
#include"rtpsession.h"
#include"rtppacket.h"
#include"rtpudpv4transmitter.h"
#include"rtpipv4address.h"
#include"rtpsessionparams.h"
#include"rtperrors.h"
#include"rtpmemorymanager.h"
#ifndefWIN32
#include<netinet/in.h>
#include<arpa/inet.h>
#else
#include<winsock2.h>
#endif//WIN32
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>

//
//.Ifso,itdisplaysanerror
//messageandexists.
//

voidcheckerror(intrtperr)
{
if(rtperr<0)
{
std::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
exit(-1);
}
}

//
//Themainroutine
//

#ifdefRTP_SUPPORT_THREAD

classMyMemoryManager:publicRTPMemoryManager
{
public:
MyMemoryManager()
{
mutex.Init();
alloccount=0;
freecount=0;
}
~MyMemoryManager()
{
std::cout<<"alloc:"<<alloccount<<"free:"<<freecount<<std::endl;
}
void*AllocateBuffer(size_tnumbytes,intmemtype)
{
mutex.Lock();
void*buf=malloc(numbytes);
std::cout<<"Allocated"<<numbytes<<"bytesatlocation"<<buf<<"(memtype="<<memtype<<")"<<std::endl;
alloccount++;
mutex.Unlock();
returnbuf;
}

voidFreeBuffer(void*p)
{
mutex.Lock();
std::cout<<"Freeingblock"<<p<<std::endl;
freecount++;
free(p);
mutex.Unlock();
}
private:
intalloccount,freecount;
JMutexmutex;
};

#else

classMyMemoryManager:publicRTPMemoryManager
{
public:
MyMemoryManager()
{
alloccount=0;
freecount=0;
}
~MyMemoryManager()
{
std::cout<<"alloc:"<<alloccount<<"free:"<<freecount<<std::endl;
}
void*AllocateBuffer(size_tnumbytes,intmemtype)
{
void*buf=malloc(numbytes);
std::cout<<"Allocated"<<numbytes<<"bytesatlocation"<<buf<<"(memtype="<<memtype<<")"<<std::endl;
alloccount++;
returnbuf;
}

voidFreeBuffer(void*p)
{
std::cout<<"Freeingblock"<<p<<std::endl;
freecount++;
free(p);
}
private:
intalloccount,freecount;
};

#endif//RTP_SUPPORT_THREAD

intmain(void)
{
#ifdefWIN32
WSADATAdat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif//WIN32

MyMemoryManagermgr;
RTPSessionsess(&mgr);
uint16_tportbase,destport;
uint32_tdestip;
std::stringipstr;
intstatus,i,num;

//First,we'

std::cout<<"Enterlocalportbase:"<<std::endl;
std::cin>>portbase;
std::cout<<std::endl;

std::cout<<"EnterthedestinationIPaddress"<<std::endl;
std::cin>>ipstr;
destip=inet_addr(ipstr.c_str());
if(destip==INADDR_NONE)
{
std::cerr<<"BadIPaddressspecified"<<std::endl;
return-1;
}

//Theinet_,but
//,soweuseacallto
//ntohl
destip=ntohl(destip);

std::cout<<"Enterthedestinationport"<<std::endl;
std::cin>>destport;

std::cout<<std::endl;
std::cout<<":"<<std::endl;
std::cin>>num;

//Now,we'llcreateaRTPsession,setthedestination,sendsome
//packetsandpollforincomingdata.

;
RTPSessionParamssessparams;

//IMPORTANT:,otherwise
//
//Inthiscase,we',sowe'll
//putthetimestampunitto(1.0/10.0)
sessparams.SetOwnTimestampUnit(1.0/10.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status=sess.Create(sessparams,&transparams);
checkerror(status);

RTPIPv4Addressaddr(destip,destport);

status=sess.AddDestination(addr);
checkerror(status);

for(i=1;i<=num;i++)
{
printf(" Sendingpacket%d/%d ",i,num);

//sendthepacket
status=sess.SendPacket((void*)"1234567890",10,0,false,10);
checkerror(status);

sess.BeginDataAccess();

//checkincomingpackets
if(sess.GotoFirstSourceWithData())
{
do
{
RTPPacket*pack;

while((pack=sess.GetNextPacket())!=NULL)
{
//Youcanexaminethedatahere
printf("Gotpacket! ");

//wedon'tlongerneedthepacket,so
//we'lldeleteit
sess.DeletePacket(pack);
}
}while(sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndefRTP_SUPPORT_THREAD
status=sess.Poll();
checkerror(status);
#endif//RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdefWIN32
WSACleanup();
#endif//WIN32
return0;
}
(2)receive.cpp
[cpp]viewplain
#include"rtpsession.h"
#include"rtppacket.h"
#include"rtpudpv4transmitter.h"
#include"rtpipv4address.h"
#include"rtpsessionparams.h"
#include"rtperrors.h"
#ifndefWIN32
#include<netinet/in.h>
#include<arpa/inet.h>
#else
#include<winsock2.h>
#endif//WIN32
#include"rtpsourcedata.h"
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>

//
//.Ifso,itdisplaysanerror
//messageandexists.
//

voidcheckerror(intrtperr)
{
if(rtperr<0)
{
std::cout<<"ERROR:"<<RTPGetErrorString(rtperr)<<std::endl;
exit(-1);
}
}

//
//Thenewclassroutine
//

classMyRTPSession:publicRTPSession
{
protected:
voidOnNewSource(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
AddDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Addingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}

voidOnBYEPacket(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
DeleteDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Deletingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}

voidOnRemoveSource(RTPSourceData*dat)
{
if(dat->IsOwnSSRC())
return;
if(dat->ReceivedBYE())
return;

uint32_tip;
uint16_tport;

if(dat->GetRTPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTPDataAddress());
ip=addr->GetIP();
port=addr->GetPort();
}
elseif(dat->GetRTCPDataAddress()!=0)
{
constRTPIPv4Address*addr=(constRTPIPv4Address*)(dat->GetRTCPDataAddress());
ip=addr->GetIP();
port=addr->GetPort()-1;
}
else
return;

RTPIPv4Addressdest(ip,port);
DeleteDestination(dest);

structin_addrinaddr;
inaddr.s_addr=htonl(ip);
std::cout<<"Deletingdestination"<<std::string(inet_ntoa(inaddr))<<":"<<port<<std::endl;
}
};

//
//Themainroutine
//

intmain(void)
{
#ifdefWIN32
WSADATAdat;
WSAStartup(MAKEWORD(2,2),&dat);
#endif//WIN32

MyRTPSessionsess;
uint16_tportbase;
std::stringipstr;
intstatus,i,num;

//First,we'

std::cout<<"Enterlocalportbase:"<<std::endl;
std::cin>>portbase;
std::cout<<std::endl;

std::cout<<std::endl;
std::cout<<"Numberofsecondsyouwishtowait:"<<std::endl;
std::cin>>num;

//Now,we'llcreateaRTPsession,setthedestination
//andpollforincomingdata.

;
RTPSessionParamssessparams;

//IMPORTANT:,otherwise
//
//Inthiscase,we'.
sessparams.SetOwnTimestampUnit(1.0/8000.0);

sessparams.SetAcceptOwnPackets(true);
transparams.SetPortbase(portbase);
status=sess.Create(sessparams,&transparams);
checkerror(status);

for(i=1;i<=num;i++)
{
sess.BeginDataAccess();

//checkincomingpackets
if(sess.GotoFirstSourceWithData())
{
do
{
RTPPacket*pack;

while((pack=sess.GetNextPacket())!=NULL)
{
//Youcanexaminethedatahere
printf("Gotpacket! ");

//wedon'tlongerneedthepacket,so
//we'lldeleteit
sess.DeletePacket(pack);
}
}while(sess.GotoNextSourceWithData());
}

sess.EndDataAccess();

#ifndefRTP_SUPPORT_THREAD
status=sess.Poll();
checkerror(status);
#endif//RTP_SUPPORT_THREAD

RTPTime::Wait(RTPTime(1,0));
}

sess.BYEDestroy(RTPTime(10,0),0,0);

#ifdefWIN32
WSACleanup();
#endif//WIN32
return0;
}

熱點內容
我的世界迪士尼神奇寶貝伺服器地址 發布:2024-10-10 09:03:02 瀏覽:556
win7存儲並顯示 發布:2024-10-10 09:02:30 瀏覽:550
oracle資料庫導出 發布:2024-10-10 08:34:56 瀏覽:363
androidn特性 發布:2024-10-10 08:30:41 瀏覽:729
存儲過程修改記錄 發布:2024-10-10 08:23:28 瀏覽:58
呱呱編程 發布:2024-10-10 08:12:54 瀏覽:895
androidoa 發布:2024-10-10 08:07:14 瀏覽:894
安卓手機怎麼關掉開了的游戲 發布:2024-10-10 07:50:14 瀏覽:681
idea新建java類 發布:2024-10-10 07:50:12 瀏覽:71
教務處的賬號和密碼是什麼 發布:2024-10-10 07:47:51 瀏覽:790