Exemplo n.º 1
0
//功能:得到函数中所有的指针变量名,包括数组名
//返回值:数组长度
int getPointName(string func[], int flen, string pointName[])
{
	int pointLen = 0;
	int i;

	string single[255] = {""};
	int slen = 0;
	int pos[10] = {-1};
	int posCount = -1;
	for(i=0; i<flen; i++)
	{
		slen = covertCmd(func[i], single);
		if(isType(single[0]))
		{
			posCount = isContain("*", single, slen, pos); // 指针
			if(-1 != posCount)
			{
				pointName[pointLen] = single[pos[0]+1];
				pointLen++;
			}
			posCount = isContain("[", single, slen, pos); // 数组
			if(-1 != posCount)
			{
				pointName[pointLen] = single[pos[0]-1];
				pointLen++;
			}
		}
	}
	return pointLen;
}
Exemplo n.º 2
0
//功能:检查一个函数中的变量在使用前是否赋了初值,基本思想就是检查一个变量第一次出现时其后是否是"=", 是的话就不再检查,不是的话就检查它第二次出现时其后是否是等"="
//返回值:函数结尾下一行行数减1
int noInitialVarFunc(int begin)
{
	int i, j, k;
	string func[255] = {""};
	int flen = 0;
	int rowNum = getFunc(func, begin, flen);
	if(-1 != rowNum)
	{
		string funcVarName[255] = {""};
		int vlen = getFuncVar(funcVarName, begin);

		string single[255] = {""};
		int slen = 0;

		int pos[10] = {-1};
		int posCount = -1;
		for(i=0; i<vlen; i++)  // 要考虑一行中多次出现变量的情况,如for语句中
		{
			int tempFlag = -1;   //变量是否赋值的标志
			for(j=0; j<flen; j++)
			{
				tempFlag = -1;
				slen = covertCmd(func[j], single);
				posCount = isContain(funcVarName[i], single, slen, pos);
				if(1 == posCount)   // 变量在该行出现一次
				{
					if("=" == single[pos[0]+1])  // j行变量声明时赋值了,不需要再检查
					{
						break;
					}
					else // 变量声明时没有赋值,继续检查,下次出现时一定要赋值
					{
						for(k=j+1; k<flen; k++)
						{
							slen = covertCmd(func[k], single);
							posCount = isContain(funcVarName[i], single, slen, pos);
							if(-1 != posCount)   // k行第二次出现变量
							{
								if("=" != single[pos[0]+1])   // 如果第二次没有赋值就报错
								{
									errNum[errLen] = (rowNum-flen+j+1);
									errType[errLen] = 8;
									errLen++;	
								}
								tempFlag = 1;
								break;   //第二次出现了变量就跳出
							}
						}
					}

					if(1 == tempFlag)
						break;
				}
			}   //遍历命令
		} //遍历变量名
	}
	return rowNum;
}
Exemplo n.º 3
0
//功能:检查全局变量是否未赋初值就使用
void noInitialGolberVar()
{
	int i,j, k;
	string globeVar[255] = {""};
	int glen = getGlobeVar(globeVar);
	string single[255] = {""};
	int slen = 0;

	int pos[10] = {-1};
	int posCount = -1;
	for(i=0; i<glen; i++)  // 要考虑一行中多次出现变量的情况,如for语句中
	{
		int tempFlag = -1;   //变量是否赋值的标志
		for(j=0; j<cmdCount; j++)
		{
			tempFlag = -1;
			slen = covertCmd(command[j], single);
			posCount = isContain(globeVar[i], single, slen, pos);
			if(1 == posCount)   // 变量在该行出现一次
			{
				if("=" == single[pos[0]+1])  // j行变量声明时赋值了,不需要再检查
				{
					break;
				}
				else // 变量声明时没有赋值,继续检查,下次出现时一定要赋值
				{
					for(k=j+1; k<cmdCount; k++)
					{
						slen = covertCmd(command[k], single);
						posCount = isContain(globeVar[i], single, slen, pos);
						if(-1 != posCount)   // k行第二次出现变量
						{
							if("=" != single[pos[0]+1])   // 如果第二次没有赋值就报错
							{
								errNum[errLen] = j+1;
								errType[errLen] = 8;
								errLen++;	
							}
							tempFlag = 1;
							break;   //第二次出现了变量就跳出
						}
					}
				}

				if(1 == tempFlag)
					break;
			}
		}   //遍历命令
	} //遍历变量名
}
Exemplo n.º 4
0
//功能:得到一个宏在文件中使用的次数, "MAX;"、 、"MAX, 1"、"2, MAX, 1;"都不算使用了宏
//返回值:次数
int getMacroCount(string test)
{
	int i;
	int count = 0; // 字符串出现的次数 
	string temp = ""; // 保存一条命令
	string single[255] = {""};
	int slen = 0;
	int posCount = 0;
	int pos[10] = {-1};
	for(i=0; i<cmdCount; i++)
	{
		temp = command[i];
		slen = covertCmd(temp, single);
		posCount = isContain(test, single, slen, pos);
		if(-1 != posCount)  // 如果该行中含有该字符串
		{
			if(test == single[0] && ("," == single[1] || ";" == single[1]))  // MAX; MAX, 1;
			{
				count = count;
			}
			else
			{
				count = count + posCount;
			}
		}
	}

	return count;
}
Exemplo n.º 5
0
bool ObjectItem::isContain(ObjectItem **ppRet, QPoint &pos, int frame, bool bChild, bool bCheckFlag)
{
    if (bChild)
    {
        for (int i = childCount() - 1; i >= 0; i--)
        {
            if (child(i)->isContain(ppRet, pos, frame, true))
            {
                return true;
            }
        }
    }

    if (bCheckFlag)
    {
        int flag = data(Qt::CheckStateRole).toInt();
        if (!(flag & kState_Disp) || (flag & kState_Lock))
        {   // 非表示
            return false;
        }
    }

    bool valid;
    FrameData d = getDisplayFrameData(frame, &valid);
    if (valid && isContain(d, pos, getDisplayMatrix(frame)))
    {
        *ppRet = this;
        return true;
    }
    return false;
}
Exemplo n.º 6
0
bool DrawableSDL::spreadEvent(SDL_Event e)
{
	switch(e.type)
	{
	case SDL_KEYDOWN:
		for(int i=0; i<handlers.size(); i++)
			if((handlers[i].getObject())->keyDown(e, this))
				return true;
		break;
	case SDL_KEYUP:
		for(int i=0; i<handlers.size(); i++)
			if((handlers[i].getObject())->keyUp(e, this))
				return true;
	case SDL_MOUSEBUTTONDOWN:
		if(isContain(e.button.x, e.button.y))
			pressed = true;
		for(int i=0; i<handlers.size(); i++)
			if(isContain(e.button.x, e.button.y))
			{
				if(handlers[i].getObject()->mouseDown(e, this))
					return true;
			}
		break;
	case SDL_MOUSEBUTTONUP:
		if(pressed)
			pressed = false;
		else
			return false;
		for(int i=0; i<handlers.size(); i++)
			if(!pressed)
			{
				handlers[i].getObject()->mouseUp(e, this);
				if(isContain(e.button.x, e.button.y))
				{
					return this->
						handlers[i].getObject()->
							onClick(e, this);
				}
				return true;
			}
	case SDL_MOUSEMOTION:
		for(int i=0; i<handlers.size(); i++)
			if(handlers[i].getObject()->mouseMove(e, this))
				return true;
	}
	return false;
}
Exemplo n.º 7
0
//功能:检测一个函数中的内存泄漏问题 1.1、malloc开了内存,没有free这块内存。
int memoryLeakFunc1(int begin)
{
	string func[255] = {""};
	int flen = 0;
	int rowNum = -1;
	rowNum =  getFunc(func, begin, flen);

	if(-1 != rowNum)   // 如果读取到了函数
	{
		int i, j;
		string temp = "";
		string single[255] = {""};
		int slen = 0;
		for(i=0; i<flen; i++)
		{
			temp = func[i];
			slen = covertCmd(temp, single);
			// 有"malloc"没有"="的情况
			if(isIn("malloc", single, slen) && !isIn("=", single, slen))  
			{
				errNum[errLen] = (rowNum-flen+i+1);
				errType[errLen] = 7;
				errLen++;
			}
			//有"malloc", 有"="的情况
			if(isIn("malloc", single, slen) && isIn("=", single, slen))
			{
				int freeFlag = -1;
				int pos[10] = {-1};
				int posCount = -1;
				posCount = isContain("=", single, slen, pos);
				string pFree = single[pos[0]-1];
				for(j=i+1; j<flen; j++)
				{
					temp = func[j];
					slen = covertCmd(temp, single);
					if(isIn("free", single, slen) && isIn(pFree, single, slen))  // free了pFree指向的指针
					{
						freeFlag = 1;
						break;
					}
				}
				if(-1 == freeFlag)   // 如果没有free
				{
					errNum[errLen] = (rowNum-flen+i+1);
					errType[errLen] = 7;
					errLen++;
				}
			}
		}

		return rowNum;
	}
	return -1;
}
Exemplo n.º 8
0
void CASet::add(int element) {
    if (!isContain(element)) {
        _end = new Node(element, nullptr, _end);
    
        if (_count == 0){
            _begin = _end;
        }
    
        Node *tempElement = _end -> prev;
    
        if (tempElement) {
            tempElement -> next = _end;
        }
        _count++;
    }
}
Exemplo n.º 9
0
//功能:不带参数的宏替换,#define MAX (100*100)
void macroSimpleReplace()
{
	int i, j, k;
	string macroName = "";
	string macroValue = "";

	int pos[10] = {-1};
	int posCount = -1;

	string temp = "";
	string single[255] = {""};
	int slen = 0;
	for(i=0; i<cmdCount; i++)
	{
		temp = command[i];
		slen = getSingle(temp, single);
		if("#define" == single[0] && 3 == slen) // 形如 #define MAX (100*100)
		{
			macroName = single[1];
			macroValue = single[2];

			for(j=i+1; j<cmdCount; j++)
			{
				temp = command[j];
				slen = covertCmd(temp, single);
				posCount = isContain(macroName, single, slen, pos);
				if(-1 != posCount) // 说明含有宏,要替换
				{
					for(k=0; k<posCount; k++)
					{
						single[pos[k]] = " " + macroValue + " ";
					}
					command[j] = "";
					//将替换后的字符串拼回命令行中
					for(k=0; k<slen; k++)
					{
						single[k] = " " + single[k] + " ";
						command[j] = command[j] + single[k];
					}
				}
			}
		}
	}
}
/*temp file reading*/
int readWordsFile(char **words,int *wordsCount)
{
	int i=0,size=0,index=0;
	char buffer[BUFSIZE];
	char bufferWord[BUFSIZE];
	FILE* inp=fopen("tempLog.txt","r");
	if(inp==NULL)
	{
		perror("file doesnt opened");
		exit(1);
	}
	/*temp file reading line by line and adding array if isnt contain else ++ that words size*/
	while(fgets(buffer,BUFSIZE,inp)!=NULL)
	{
		/*for not to read \n*/
		char bufferWord[BUFSIZE];
		buffer[strlen(buffer)-1]='\0';
		strcpy(bufferWord,buffer);

		/*printf("%s\n",bufferWord);*/
		index=isContain(words,bufferWord,i);
		if(index==-1)
		{
			strcpy(words[i],bufferWord);
			i++;
		}
		else
		{
			wordsCount[index]+=1;
		}
			
		

		
	}
	fclose(inp);
	return i;
}
Exemplo n.º 11
0
//情况一:表达式为true或false
void if1()
{
	int i;
	string temp = "";
	string single[255] = {""};
	int slen = 0;
	for(i=0; i<cmdCount; i++)
	{
		temp = command[i];
		slen = covertCmd(temp, single);
		int pos[10] = {0};
		int posCount = isContain("if",single, slen, pos);
		if(-1 != posCount)  // 含有关键字if
		{
			if(("true" == single[pos[0]+2] || "false" == single[pos[0]+2]) && ")" == single[pos[0]+3])
			{
				errNum[errLen] = i+1;
				errType[errLen] = 3;
				errLen++;
			}
		}
	}
}
Exemplo n.º 12
0
//情况二:表达式为纯数字运算式 如"-1+(2*3/5)", 可以有大中小括号
void if2()
{
	int i, j;
	string temp = "";
	string single[255] = {""};
	int slen = 0;
	int numFlag = 1;  // 是否为纯数字运算的标志, 是为1,不是为-1
	for(i=0; i<cmdCount; i++)
	{
		numFlag = 1;
		temp = command[i];
		slen = covertCmd(temp, single);
		int pos[10] = {0};
		int posCount = isContain("if",single, slen, pos);
		if(-1 != posCount)  // 含有关键字if
		{
			for(j=pos[0]+1; j<slen; j++) // 遍历if后的所有字符,看是否是纯数字表达式
			{
				if("(" != single[j] && ")" != single[j] && "[" != single[j] && "]" != single[j] &&
					"{" != single[j] && "}" != single[j] &&
					"+" != single[j] && "-" != single[j] && "*" != single[j] && "/" != single[j]
					&& !onlyNum(single[j]))     // 纯数字表达式可能出现的字符, 满足所有条件时不是纯数字
				{
					numFlag = -1;
					break;
				}
			}
			if(1 == numFlag)
			{
				errNum[errLen] = i+1;
				errType[errLen] = 3;
				errLen++;
			}
		}
	}
}
Exemplo n.º 13
0
//功能:展开单条调用了带参数的宏的语句, 只展开第一个该句中检测到的宏
//参数:macroDef 宏定义语句, useMacro 调用语句,
//返回值:返回展开后的语句
string singleComplexMacroReplace(string macroDef, string useMacro)
{
	int i, j;
	// 得到宏参数名
	int macroVarNameLen = 0;
	string macroVarName[255] = {""};
	macroVarNameLen = getMacroVarName(macroDef, macroVarName); 


	// 如#define MAX(a, b) ((a)*(b))  得到((a)*(b)) 打散后的字符串数组
	int macroValueLen = 0;
	string complexMacroValue[255] = {""};
	macroValueLen = getComplexMacroValue(macroDef, complexMacroValue);  
	
	//得到使用带参的宏中的传递的值,如MAX(2, (3)) 中的2,(3)
	string single[255] = {""};

	covertCmd(macroDef, single);
	string macroName = single[1];
	string macroRealValue[255] = {""}; 
	int useMacroLen = 0;

	useMacroLen = getComplexMacroRealValue(macroName, useMacro, macroVarNameLen, macroRealValue);

	//展开宏
	for(i=0; i<macroVarNameLen; i++)  //宏参遍历
	{
		for(j=0; j<macroValueLen; j++)  //宏值打散后的数组遍历
		{
			if(macroVarName[i] == complexMacroValue[j]) // 宏参出现,则就传递值代替
			{
				complexMacroValue[j] = macroRealValue[i];   
			}
		}
	}

	//将展开后的宏连成一个字符串
	string replacedMacro = "";
	for(i=0; i<macroValueLen; i++)
	{
		replacedMacro += complexMacroValue[i];
	}
	
	//找到宏在该句中第一次出现的位置
	string temp = useMacro;
	int slen = covertCmd(temp, single);

	int pos[10] = {-1};
	int posCount = -1;
	posCount = isContain(macroName, single, slen, pos);
	if(-1 != posCount)
	{
		for(i=pos[0]; i<pos[0]+useMacroLen; i++)
		{
			single[i] = "";   //将原宏都置为空
		}
		single[pos[0]] = " " + replacedMacro + " ";  // 实现宏替换
	}

	// 将替换后的语句连起来
	string replacedUseMacro = "";
	for(i=0; i<slen; i++)
	{
		single[i] = " " + single[i] + " ";
		replacedUseMacro += single[i];
	}

	return replacedUseMacro;
}
Exemplo n.º 14
0
//功能:得到使用带参的宏中的传递的值,如MAX(2, (3)) 中的2,(3),只得到第一个该句中检测到的宏
//参数:macroName 宏名, useMacro 调用该宏的语句,macroVarLen 宏参个数
//返回值:返回将调用的宏打散后的字符串数组的长度
int getComplexMacroRealValue(string macroName, string useMacro, int macroVarLen, string macroRealValue[])
{
	string useSingle[255] = {""};  // 调用宏的语句
	int useLen = 0;

	useLen = covertCmd(useMacro, useSingle);   // 将调用宏语句打散

	int posCount = -1;  // 该语句中调用该宏的次数
	int pos[10] = {-1};   // 调用的位置

	posCount = isContain(macroName, useSingle, useLen, pos);  // 得到调用宏的位置

	int realValueLen = 0;  //传递参数数组长度
	int j;

	//以下是找到宏的右括号出现的位置
	int littleBracket = 0; // 宏的小括号匹配标志
	int rightMacro = 0; // 保存找到的位置

	for(int i=pos[0]+1; i<useLen; i++) // 从宏的左括号位置开始匹配
	{                      
		if("(" == useSingle[i])
		{
			littleBracket++;
		}
		if(")" == useSingle[i])
		{
			littleBracket--;
		}
		if(0 == littleBracket && i != pos[0]+1)
		{
			rightMacro = i;
		}
	}

	//开始提取传递的参数 MAX(a, (b))
	for(int i=pos[0]+2; i<useLen; i++)
	{
		j = i;
		if(0 != macroVarLen)  // 如果有形参
		{
			while("," != useSingle[j] && j != rightMacro) // 不为逗号说明还是一个传递参数, 没有到宏的右括号说明还要查找
			{
				if(j != rightMacro)  // 没有到宏结尾// if(MAX(2, 3))
				{
					macroRealValue[realValueLen] = macroRealValue[realValueLen] + useSingle[j];	
					j++;
				}
				else
				{
					break;   //到宏结尾则跳出
				}
			}
			realValueLen++;
			
			if(j == rightMacro)  // 到了宏的右括号位置
				break;

			i=j;   // i偏移到逗号位置
		}
	}
	int temp = rightMacro-pos[0]+1;
	return temp;
}
Exemplo n.º 15
0
//功能:情况三考虑在一个函数中的情况,判断思想是找到if语句中所有的变量名,然后在if语句之前往上查找,如果最后一次出现该变量时是
//+"a=常数"的情况,如果最后是"a>、<、>=、<=、!=、+、-、*、/"的情况就跳过继续往上查,则判断为该变量是不变的,不过这样会有很多的bug,待解决啊……
int ifFunc3(int begin)
{
	string func[255] = {""};
	int flen = 0;
	int rowNum = getFunc(func, begin, flen);	

	int i, j, k;
	if(-1 != rowNum)
	{
		int varEqualNum = 0;
		int useFlag = -1;

		string single[255] = {""};
		int slen = 0;
		string temp = "";

		string oneCmdVarName[255] = {""};
		int cmdVarLen = 0;

		int pos[10] = {-1};
		int posCount = -1;
		for(i=0; i<flen; i++)
		{
			temp = func[i];
			slen = covertCmd(temp, single);

			if(isIn("if", single, slen))  // 在func[i]中检测到if语句
			{
				cmdVarLen = getVarFromOneCmd(begin, temp, oneCmdVarName);  // 得到if条件中变量名

				for(j=0; j<cmdVarLen; j++)    // 遍历if中所有变量
				{
					useFlag = -1;
					varEqualNum = 0;
					for(k=i-1; k>=0; k--)   // 在if语句之前进行检测变量的赋值情况
					{
						temp = func[k];
						slen = covertCmd(temp, single);
						posCount = isContain(oneCmdVarName[j], single, slen, pos);
						int m = 0; // 变量在一行中出现的次数遍历
						if(-1 != posCount)   // 如果出现了变量, 检测最后出现变量的位置是不是a=1的简单形式
						{
							for(m=posCount-1; m>=0; m--) // 从一行最后开始遍历变量
							{
								//single[pos[m]+1] 变量后的第一个字符
								if(">" == single[pos[m]+1] || "<" == single[pos[m]+1] || ">=" == single[pos[m]+1] || 
									"<=" == single[pos[m]+1] || "!=" == single[pos[m]+1] || "+" == single[pos[m]+1] || "-" == single[pos[m]+1] ||
									"*" == single[pos[m]+1] || "/" == single[pos[m]+1])
								{
									continue;
								}
								if("=" == single[pos[m]+1] && onlyNum(single[pos[m]+2])) // a = 数字 的情况
								{
									varEqualNum = 1;
									break;
								}
								else
								{
									varEqualNum = -1;
									break;
								}
							}
							if(1 == varEqualNum || -1 == varEqualNum) // 如果continue了就要继续找下一行,而不是退出for循环
								break;
						}	
					}

					if(-1 == varEqualNum)
					{
						// 说明有变量会变
						useFlag = 1;
						break;   // 不是无效分去
					}
				}

				if(-1 == useFlag)  // 说明一直没有变量变,无效分支
				{
					errNum[errLen] = rowNum-flen+i+1;
					errType[errLen] = 3;
					errLen++;
				}
			}
		}
	}

	return rowNum;
}
Exemplo n.º 16
0
//功能:在一个函数中检测无效的switch语句
//返回值:返回函数结尾下一行减1
int switchInfunc(int beginFunc)
{
	string func[255] = {""};
	int flen = 0;
	int rowNum = getFunc(func, beginFunc, flen);	

	int i, j, k;
	if(-1 != rowNum)
	{
		int	useFlag = -1;
		int varEqualNum = 0;

		string switchValue = "";  // switch 的值

		string temp = "";
		string single[255] = {""};
		int slen = 0;
		string oneCmdVarName[255] = {""};
		int oneCmdVarLen = 0;
		string switchCmd[255] = {""};
		int switchLen = 0;
		int beginInfunc = 0;

		int pos[10] = {-1};
		int posCount = -1;
		for(i=0; i<flen; i++)
		{
			temp = func[i];
			slen = covertCmd(temp, single);
			if(isIn("switch", single, slen)) //在函数第i行位置 检测到switch语句
			{
				beginInfunc = i;
				int switchPos = getOneSwitch(func, flen, beginInfunc, switchCmd, switchLen); // 得到第一个switch语句
				temp = switchCmd[0];  // switch
				slen = covertCmd(temp, single);
				oneCmdVarLen = getVarFromOneCmd(beginFunc, switchCmd[0], oneCmdVarName);// 得到switch中的变量名
				if(1 == oneCmdVarLen)  //switch中只有一个变量时
				{
					for(j=0; j<oneCmdVarLen; j++)    // 遍历switch中所有变量
					{
						useFlag = -1;
						varEqualNum = 0;
						for(k=i-1; k>=0; k--)   // 在switch语句之前进行检测变量的赋值情况
						{
							temp = func[k];
							slen = covertCmd(temp, single);
							posCount = isContain(oneCmdVarName[j], single, slen, pos);
							int m = 0; // 变量在一行中出现的次数遍历
							if(-1 != posCount)   // 如果出现了变量, 检测最后出现变量的位置是不是a=1的简单形式
							{
								for(m=posCount-1; m>=0; m--) // 从一行最后开始遍历变量
								{
									//single[pos[m]+1] 变量后的第一个字符
									if(">" == single[pos[m]+1] || "<" == single[pos[m]+1] || ">=" == single[pos[m]+1] || 
										"<=" == single[pos[m]+1] || "!=" == single[pos[m]+1] || "+" == single[pos[m]+1] || "-" == single[pos[m]+1] ||
										"*" == single[pos[m]+1] || "/" == single[pos[m]+1])
									{
										continue;
									}
									if("=" == single[pos[m]+1] && onlyNum(single[pos[m]+2])) // a = 数字 的情况
									{
										switchValue = single[pos[m]+2]; // 得到switch的值
										varEqualNum = 1;
										break;
									}
									else
									{
										varEqualNum = -1;
										break;
									}
								}
								if(1 == varEqualNum || -1 == varEqualNum) // 如果continue了就要继续找下一行,而不是退出for循环
									break;
							}	
						}

						if(-1 == varEqualNum)
						{
							// 说明有变量会变
							useFlag = 1;
							break;   // 不是无效分去
						}
					}
					int defaultFlag = -1;
					if(-1 == useFlag)  // 说明一直没有变量变,无效分支
					{
						defaultFlag = -1;
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("case", single, slen))  // 出现case 分支
							{
								if(switchValue != single[1]) // 当这一行的case值不是switch的时候则报错
								{
									errNum[errLen] = (rowNum-flen+i+1+k);
									errType[errLen] = 3;
									errLen++;
								}
								else   // case值是switch值时,说明default也要报错
									defaultFlag = 1;
							}
						}
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("default", single, slen) && 1 == defaultFlag)  // 出现case 分支
							{
								errNum[errLen] = (rowNum-flen+i+1+k);
								errType[errLen] = 3;
								errLen++;
							}
						}
					}
				}//switch中只有一个变量时
				
				int defaultFlag = -1;
				if(0 == oneCmdVarLen)  // 说明switch里面没有变量
				{
					temp = switchCmd[0];
					slen = covertCmd(temp, single);
					if(onlyNum(single[2]) && ")" == single[3])   // switch(1) 的形式
					{
						switchValue = single[2];
						defaultFlag = -1;
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("case", single, slen))  // 出现case 分支
							{
								if(switchValue != single[1]) // 当这一行的case值不是switch的时候则报错
								{
									errNum[errLen] = (rowNum-flen+i+1+k);
									errType[errLen] = 3;
									errLen++;
								}
								else   // case值是switch值时,说明default也要报错
									defaultFlag = 1;
							}
						}
						for(k=0; k<switchLen; k++)
						{
							temp = switchCmd[k];
							slen = covertCmd(temp, single);
							if(isIn("default", single, slen) && 1 == defaultFlag)  // 出现case 分支
							{
								errNum[errLen] = (rowNum-flen+i+1+k);
								errType[errLen] = 3;
								errLen++;
							}
						}
					}
				}
			}   // 检测到switch的位置
		}
	}
	return rowNum;
}
Exemplo n.º 17
0
int main(int argc, char const *argv[])
{
    int fd; // ファイルディスクリプタ。サーバーとの接続
    uint32_t r_buf[DATA_NUM];

    const char *host = argv[1]; // 第一引数 ex) localhost
    const char *service = argv[2]; // 第二引数 ex) 1111

    /* サーバーとの接続 */
    fd = get_stream(host, service);
    if (fd == -1)
    {
        printf("get_stream error!!\n");
        exit(-1);
    }

    /* 一回目のデータ取得 */
    int len = -1;

    len = getData(fd, r_buf);
    printf("finish read\n");
    printf("len: %d\n", len);
    // dumpBuf(r_buf);

    /* ゲームに必要なデータの初期化 */
    struct Company companies[COMPANY_NUM];
    struct Tickets* tickets = InitTickets();
    uint32_t key, tmp_key, tmp_code;
    int money = 10000;
    int state = 0;

    key = InitCompanies(r_buf, companies);

    PrintCompanies(companies);

    /* メインルーチン。ターン数分実行される */
    for (int t = 0; t < TURNS; t++) {

        printf("\n\n------------------------%d th turn  money: %d------------------------\n", t, money);
        PrintCompanies(companies);

        /* strategy部分 */

        if (t == 0) {
            InitStrategy(fd, key, tickets, companies);
        } else if (t % 2 == 1) {
            SecondStrategy(fd, key, tickets, companies);
        } else if (t % 2 == 0) {
            ThirdBuyStrategy(fd, key, money, tickets, companies);
        } else {
            printf("bug!!!\n");
        }


        /* ターン内で投げたリクエストに対する反応を取得する */
        state = 0;
        // stateでゲームの進行状況を管理
        // state 0: 同一ターン内
        // state 1: 次のターンに進行
        // state 2: ゲーム終了。結果の出力へ
        while (state == 0) {

            getData(fd, r_buf);

            dumpBuf(r_buf);

            tmp_code = 0;
            tmp_key = 0;
            tmp_key = getKey(r_buf);
            if (key != tmp_key) {
                state = 1;
                printf("next turn\n");
            }
            tmp_code = getCode(r_buf);
            if (tmp_code == REQ_ACCEPT) {
                int accepted_idx = isContain(MakeTicketFromBuf(r_buf), tickets);
                printf("%d th ticket accepted!!\n", accepted_idx);
                if (accepted_idx != -1) {
                    money += ApplyTicket(accepted_idx, tickets, companies);
                }
            } else if (tmp_code == UNKOWN_CODE || tmp_code == INVALID_KEY ||
                       tmp_code == TOO_MUCH_REQ || tmp_code == ID_NOT_EXIST ||
                       tmp_code == TOO_MUCH_BUY || tmp_code == TOO_MUCH_SELL) {
                printf("code error type: %s\n", getCodeName(tmp_code));
                int rejected_idx = isContain(MakeTicketFromBuf(r_buf), tickets);
                printf("rejected ticket: %d\n", rejected_idx);
                if (rejected_idx != -1) {
                    DeleteTicket(rejected_idx, tickets);
                }
            } else if (tmp_code == TURN_START) {
                key = Parse(r_buf, companies);
                state = 1;
            } else if (tmp_code == GAME_END) {
                state = 2;
            } else {
                printf("something wrong in code\n");
            }
        }

        if (state == 2) {
            break;
        }

    }

    printf("\n\n\n");
    printf("##################################\n");
    printf("##############RESULT##############\n");
    printf("##################################\n\n");

    printf("your rank: %d\n", getID(0, r_buf));
    printf("your budget: %u\n", getValue(0, r_buf));

    close(fd);

    return 0;
}
Exemplo n.º 18
0
//功能:检测一个函数中的内存泄漏问题 1.2、用指针指向malloc开的内存,在没有释放这块内存之前改变了该的指针的值,使该块内存无法释放.
//改变值的形式有指针-指针,指针-数组名,指针-变量取址(&)
int memoryLeakFunc2(int begin)
{
	string func[255] = {""};
	int flen = 0;
	int rowNum = -1;
	rowNum =  getFunc(func, begin, flen);
	string pointName[255] = {""};
	
	if(-1 != rowNum)   // 如果读取到了函数
	{
		int pointLen = getPointName(func, flen, pointName);

		int i, j, k;
		string temp = "";
		string single[255] = {""};
		int slen = 0;
		for(i=0; i<flen; i++)
		{
			temp = func[i];
			slen = covertCmd(temp, single);
			//有"malloc", 有"="的情况
			if(isIn("malloc", single, slen) && isIn("=", single, slen))
			{
				int freeFlag = -1;
				int pos[10] = {-1};
				int posCount = -1;
				posCount = isContain("=", single, slen, pos);
				string pFree = single[pos[0]-1];
				for(j=i+1; j<flen; j++)
				{
					temp = func[j];
					slen = covertCmd(temp, single);
					if(isIn("free", single, slen) && isIn(pFree, single, slen))
					{
						freeFlag = 1;
						break;
					}
					if(isIn(pFree, single, slen) && isIn("&", single, slen) && isIn("=", single, slen))
					{
						// 出现  p=&a; 的情况,改变指针的值
						errNum[errLen] = (rowNum-flen+i+1);
						errType[errLen] = 7;
						errLen++;
						break;
					}
					int tempFlag = -1;
					for(k=0; k<pointLen; k++)
					{
						int pos[10] = {-1};
						int posCount = -1;
						posCount = isContain(pFree, single, slen, pos);
						if(-1 != posCount)
						{
							if("=" == single[pos[0]+1])
							{
								if(pFree != single[pos[0]+2])   // 排除p1 = p1;的情况
								{
									// 出现p1 = p2, p1=a; 之类的情况, 而且还要pFree要在=等号左边
									errNum[errLen] = (rowNum-flen+i+1);
									errType[errLen] = 7;
									errLen++;
									tempFlag = 1;
									break;
								}
							}
						}
					}
					if(1 == tempFlag)
					{
						break;
					}
				}
				
			}
		}
	}
	return rowNum;
}