目录一、函数设计原则二、总结一、函数设计原则 函数从意义上应该是一个独立的功能模块函数名要在一定程度上反映函数的功能函数参数名要能够体现参数的意义尽量避免在函数中使用全局变量当函数参
下面来欣赏一份优秀的代码:
#include "eclipseOS.h"
#include "eclipseCommon.h"
#include "eclipseUtil.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#include <strings.h>
#endif
#define MAX_LINE_LENGTH 256
int isJ9VM( _TCHAR* vm )
{
_TCHAR * ch = NULL, *ch2 = NULL;
int res = 0;
if (vm == NULL)
return 0;
ch = lastDirSeparator( vm );
if (isVMLibrary(vm)) {
if(ch == NULL)
return 0;
ch[0] = 0;
ch2 = lastDirSeparator(vm);
if(ch2 != NULL) {
res = (_tcsicmp(ch2 + 1, _T_ECLIPSE("j9vm")) == 0);
}
ch[0] = dirSeparator;
return res;
} else {
if (ch == NULL)
ch = vm;
else
ch++;
return (_tcsicmp( ch, _T_ECLIPSE("j9") ) == 0);
}
}
int checkProvidedVMType( _TCHAR* vm )
{
_TCHAR* ch = NULL;
struct _stat stats;
if (vm == NULL) return VM_NOTHING;
if (_tstat(vm, &stats) == 0 && (stats.st_mode & S_IFDIR) != 0) {
return VM_DIRECTORY;
}
ch = _tcsrchr( vm, _T_ECLIPSE('.') );
if(ch == NULL)
return VM_OTHER;
#ifdef _WIN32
if (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0)
#else
if ((_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0))
#endif
{
return VM_LIBRARY;
}
if (_tcsicmp(ch, _T_ECLIPSE(".ee")) == 0)
return VM_EE_PROPS;
return VM_OTHER;
}
_TCHAR * checkPathList( _TCHAR* pathList, _TCHAR* programDir, int reverseOrder) {
_TCHAR * c1, *c2;
_TCHAR * checked, *result;
size_t checkedLength = 0, resultLength = 0;
size_t bufferLength = _tcslen(pathList);
result = malloc(bufferLength * sizeof(_TCHAR));
c1 = pathList;
while (c1 != NULL && *c1 != _T_ECLIPSE('\0'))
{
c2 = _tcschr(c1, pathSeparator);
if (c2 != NULL)
*c2 = 0;
checked = checkPath(c1, programDir, reverseOrder);
checkedLength = _tcslen(checked);
if (resultLength + checkedLength + 1> bufferLength) {
bufferLength += checkedLength + 1;
result = realloc(result, bufferLength * sizeof(_TCHAR));
}
if(resultLength > 0) {
result[resultLength++] = pathSeparator;
result[resultLength] = _T_ECLIPSE('\0');
}
_tcscpy(result + resultLength, checked);
resultLength += checkedLength;
if(checked != c1)
free(checked);
if(c2 != NULL)
*(c2++) = pathSeparator;
c1 = c2;
}
return result;
}
_TCHAR * concatStrings(_TCHAR**strs) {
return concatPaths(strs, 0);
}
_TCHAR * concatPaths(_TCHAR** strs, _TCHAR separator) {
_TCHAR separatorString[] = { separator, 0 };
_TCHAR * result;
int i = -1;
size_t length = 0;
while (strs[++i] != NULL) {
length += _tcslen(strs[i]) + (separator != 0 ? 1 : 0);
}
result = malloc((length + 1) * sizeof(_TCHAR));
result[0] = 0;
i = -1;
while (strs[++i] != NULL) {
result = _tcscat(result, strs[i]);
if (separator != 0)
result = _tcscat(result, separatorString);
}
return result;
}
int containsPaths(_TCHAR * str, _TCHAR** paths) {
_TCHAR * buffer;
_TCHAR * c;
int i;
buffer = malloc((_tcslen(str) + 2) * sizeof(_TCHAR));
_stprintf(buffer, _T_ECLIPSE("%s%c"), str, pathSeparator);
for (i = 0; paths[i] != NULL; i++) {
c = _tCSStr(buffer, paths[i]);
if ( c == NULL || !(c == buffer || *(c - 1) == pathSeparator))
{
free(buffer);
return 0;
}
}
free(buffer);
return 1;
}
int isVMLibrary( _TCHAR* vm )
{
_TCHAR *ch = NULL;
if (vm == NULL) return 0;
ch = _tcsrchr( vm, '.' );
if(ch == NULL)
return 0;
#ifdef _WIN32
return (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0);
#else
return (_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0);
#endif
}
#ifdef AIX
#include <sys/types.h>
#include <time.h>
char* getVMVersion( char *vmPath )
{
char cmd[MAX_LINE_LENGTH];
char lineString[MAX_LINE_LENGTH];
char* firstChar;
char fileName[MAX_LINE_LENGTH];
time_t curTime;
FILE* fp;
int numChars = 0;
char* version = NULL;
(void) time(&curTime);
(void) sprintf(fileName, "/tmp/tmp%ld.txt", curTime);
(void) sprintf(cmd,"%s -version 2> %s", vmPath, fileName);
(void) system(cmd);
fp = fopen(fileName, "r");
if (fp != NULL)
{
if (fgets(lineString, MAX_LINE_LENGTH, fp) == NULL)
lineString[0] = '\0';
fclose(fp);
unlink(fileName);
firstChar = (char *) (strchr(lineString, '"') + 1);
if (firstChar != NULL)
numChars = (int) (strrchr(lineString, '"') - firstChar);
if (numChars > 0)
{
version = malloc( numChars + 1 );
strncpy(version, firstChar, numChars);
version[numChars] = '\0';
}
}
return version;
}
int versionCmp(char *ver1, char *ver2)
{
char* dot1;
char* dot2;
int num1;
int num2;
dot1 = strchr(ver1, '.');
dot2 = strchr(ver2, '.');
num1 = atoi(ver1);
num2 = atoi(ver2);
if (num1 > num2)
return 1;
if (num1 < num2)
return -1;
if (dot1 && !dot2)
return 1;
if (!dot1 && dot2)
return -1;
if (!dot1 && !dot2)
return 0;
return versionCmp((char*)(dot1 + 1), (char*)(dot2 + 1) );
}
#endif
至此,C 语言进阶剖析学完,完结撒花。
但是,以后路还很远,还需要多加总结,才能真正掌握 C 语言。
到此这篇关于C语言容易被忽视的函数设计原则基础的文章就介绍到这了,更多相关C语言函数设计原则内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: C语言容易被忽视的函数设计原则基础
本文链接: https://lsjlt.com/news/147016.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0