第48课 函数设计原则

函数设计原则:

函数应该是无状态的,就是第一次调用和第二次调用是一样的。

getchar返回值是int型。

优秀代码欣赏:Eclipse代码

  1 /*******************************************************************************
  2  * Copyright (c) 2000, 2005 IBM Corporation and others.
  3  * All rights reserved. This program and the accompanying materials
  4  * are made available under the terms of the Eclipse Public License v1.0
  5  * which accompanies this distribution, and is available at
  6  * http://www.eclipse.org/legal/epl-v10.html
  7  *
  8  * Contributors:
  9  *     IBM Corporation - initial API and implementation
 10  *     Kevin Cornell (Rational Software Corporation)
 11  *******************************************************************************/
 12
 13 /* Eclipse Launcher Utility Methods */
 14
 15 #include "eclipseOS.h"
 16 #include "eclipseCommon.h"
 17 #include "eclipseUtil.h"
 18
 19 #include <string.h>
 20 #include <stdlib.h>
 21 #include <stdio.h>
 22 #include <sys/stat.h>
 23 #ifdef _WIN32
 24 #include <direct.h>
 25 #else
 26 #include <unistd.h>
 27 #include <strings.h>
 28 #endif
 29
 30 #define MAX_LINE_LENGTH 256
 31
 32 /* Is the given VM J9 */
 33 int isJ9VM( _TCHAR* vm )
 34 {
 35     _TCHAR * ch = NULL, *ch2 = NULL;
 36     int res = 0;
 37
 38     if (vm == NULL)
 39         return 0;
 40
 41     ch = lastDirSeparator( vm );
 42     if (isVMLibrary(vm)) {
 43         /* a library, call it j9 if the parent dir is j9vm */
 44         if(ch == NULL)
 45             return 0;
 46         ch[0] = 0;
 47         ch2 = lastDirSeparator(vm);
 48         if(ch2 != NULL) {
 49             res = (_tcsicmp(ch2 + 1, _T_ECLIPSE("j9vm")) == 0);
 50         }
 51         ch[0] = dirSeparator;
 52         return res;
 53     } else {
 54         if (ch == NULL)
 55             ch = vm;
 56         else
 57             ch++;
 58         return (_tcsicmp( ch, _T_ECLIPSE("j9") ) == 0);
 59     }
 60 }
 61
 62 int checkProvidedVMType( _TCHAR* vm )
 63 {
 64     _TCHAR* ch = NULL;
 65     struct _stat stats;
 66
 67     if (vm == NULL) return VM_NOTHING;
 68
 69     if (_tstat(vm, &stats) == 0 && (stats.st_mode & S_IFDIR) != 0) {
 70         /* directory */
 71         return VM_DIRECTORY;
 72     }
 73
 74     ch = _tcsrchr( vm, _T_ECLIPSE(‘.‘) );
 75     if(ch == NULL)
 76         return VM_OTHER;
 77
 78 #ifdef _WIN32
 79     if (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0)
 80 #else
 81     if ((_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0))
 82 #endif
 83     {
 84         return VM_LIBRARY;
 85     }
 86
 87     if (_tcsicmp(ch, _T_ECLIPSE(".ee")) == 0)
 88         return VM_EE_PROPS;
 89
 90     return VM_OTHER;
 91 }
 92
 93 /*
 94  * pathList is a pathSeparator separated list of paths, run each through
 95  * checkPath and recombine the results.
 96  * New memory is always allocated for the result
 97  */
 98 _TCHAR * checkPathList( _TCHAR* pathList, _TCHAR* programDir, int reverseOrder) {
 99     _TCHAR * c1, *c2;
100     _TCHAR * checked, *result;
101     size_t checkedLength = 0, resultLength = 0;
102     size_t bufferLength = _tcslen(pathList);
103
104     result = malloc(bufferLength * sizeof(_TCHAR));
105     c1 = pathList;
106     while (c1 != NULL && *c1 != _T_ECLIPSE(‘\0‘))
107     {
108         c2 = _tcschr(c1, pathSeparator);
109         if (c2 != NULL)
110             *c2 = 0;
111
112         checked = checkPath(c1, programDir, reverseOrder);
113         checkedLength = _tcslen(checked);
114         if (resultLength + checkedLength + 1> bufferLength) {
115             bufferLength += checkedLength + 1;
116             result = realloc(result, bufferLength * sizeof(_TCHAR));
117         }
118
119         if(resultLength > 0) {
120             result[resultLength++] = pathSeparator;
121             result[resultLength] = _T_ECLIPSE(‘\0‘);
122         }
123         _tcscpy(result + resultLength, checked);
124         resultLength += checkedLength;
125
126         if(checked != c1)
127             free(checked);
128         if(c2 != NULL)
129             *(c2++) = pathSeparator;
130         c1 = c2;
131     }
132
133     return result;
134 }
135
136 _TCHAR * concatStrings(_TCHAR**strs) {
137     return concatPaths(strs, 0);
138 }
139
140 _TCHAR * concatPaths(_TCHAR** strs, _TCHAR separator) {
141     _TCHAR separatorString[] = { separator, 0 };
142     _TCHAR * result;
143     int i = -1;
144     size_t length = 0;
145     /* first count how large a buffer we need */
146     while (strs[++i] != NULL) {
147         length += _tcslen(strs[i]) + (separator != 0 ? 1 : 0);
148     }
149
150     result = malloc((length + 1) * sizeof(_TCHAR));
151     result[0] = 0;
152     i = -1;
153     while (strs[++i] != NULL) {
154         result = _tcscat(result, strs[i]);
155         if (separator != 0)
156             result = _tcscat(result, separatorString);
157     }
158     return result;
159 }
160
161 /*
162  * buffer contains a pathSeparator separated list of paths, check
163  * that it contains all the paths given.  Each path is expected to be
164  * terminated with a pathSeparator character.
165  */
166 int containsPaths(_TCHAR * str, _TCHAR** paths) {
167     _TCHAR * buffer;
168     _TCHAR * c;
169     int i;
170
171     /* terminate the string with a pathSeparator */
172     buffer = malloc((_tcslen(str) + 2) * sizeof(_TCHAR));
173     _stprintf(buffer, _T_ECLIPSE("%s%c"), str, pathSeparator);
174
175     for (i = 0; paths[i] != NULL; i++) {
176         c = _tcsstr(buffer, paths[i]);
177         if ( c == NULL || !(c == buffer || *(c - 1) == pathSeparator))
178         {
179             /* entry not found */
180             free(buffer);
181             return 0;
182         }
183     }
184     free(buffer);
185     return 1;
186 }
187
188 int isVMLibrary( _TCHAR* vm )
189 {
190     _TCHAR *ch = NULL;
191     if (vm == NULL) return 0;
192     ch = _tcsrchr( vm, ‘.‘ );
193     if(ch == NULL)
194         return 0;
195 #ifdef _WIN32
196     return (_tcsicmp(ch, _T_ECLIPSE(".dll")) == 0);
197 #else
198     return (_tcsicmp(ch, _T_ECLIPSE(".so")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".jnilib")) == 0) || (_tcsicmp(ch, _T_ECLIPSE(".dylib")) == 0);
199 #endif
200 }
201
202 #ifdef AIX
203
204 #include <sys/types.h>
205 #include <time.h>
206
207 /* Return the JVM version in the format x.x.x
208  */
209 char* getVMVersion( char *vmPath )
210 {
211     char   cmd[MAX_LINE_LENGTH];
212     char   lineString[MAX_LINE_LENGTH];
213     char*  firstChar;
214     char   fileName[MAX_LINE_LENGTH];
215     time_t curTime;
216     FILE*  fp;
217     int    numChars = 0;
218     char*  version  = NULL;
219
220     /* Define a unique filename for the java output. */
221     (void) time(&curTime);
222     (void) sprintf(fileName, "/tmp/tmp%ld.txt", curTime);
223
224     /* Write java -version output to a temp file */
225     (void) sprintf(cmd,"%s -version 2> %s", vmPath, fileName);
226     (void) system(cmd);
227
228     fp = fopen(fileName, "r");
229     if (fp != NULL)
230     {
231         /* Read java -version output from a temp file */
232         if (fgets(lineString, MAX_LINE_LENGTH, fp) == NULL)
233             lineString[0] = ‘\0‘;
234         fclose(fp);
235         unlink(fileName);
236
237         /* Extract version number */
238         firstChar = (char *) (strchr(lineString, ‘"‘) + 1);
239         if (firstChar != NULL)
240             numChars = (int)  (strrchr(lineString, ‘"‘) - firstChar);
241
242         /* Allocate a buffer and copy the version string into it. */
243         if (numChars > 0)
244         {
245             version = malloc( numChars + 1 );
246             strncpy(version, firstChar, numChars);
247             version[numChars] = ‘\0‘;
248         }
249     }
250
251     return version;
252 }
253
254 /* Compare JVM Versions of the form "x.x.x..."
255  *
256  *    Returns -1 if ver1 < ver2
257  *    Returns  0 if ver1 = ver2
258  *    Returns  1 if ver1 > ver2
259  */
260 int versionCmp(char *ver1, char *ver2)
261 {
262     char*  dot1;
263     char*  dot2;
264     int    num1;
265     int    num2;
266
267     dot1 = strchr(ver1, ‘.‘);
268     dot2 = strchr(ver2, ‘.‘);
269
270     num1 = atoi(ver1);
271     num2 = atoi(ver2);
272
273     if (num1 > num2)
274         return 1;
275
276     if (num1 < num2)
277         return -1;
278
279     if (dot1 && !dot2)   /* x.y > x */
280         return 1;
281
282     if (!dot1 && dot2)   /* x < x.y */
283         return -1;
284
285     if (!dot1 && !dot2)  /* x == x */
286         return 0;
287
288     return versionCmp((char*)(dot1 + 1), (char*)(dot2 + 1) );
289 }
290 #endif /* AIX */

小结:

原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9563272.html

时间: 2024-08-14 20:29:01

第48课 函数设计原则的相关文章

js函数设计原则

一般认为函数指具有返回值的子程序,过程指没有返回值的子程序.C++中把所有子程序成为函数,其实那些返回值为void的 函数在语义上也是过程.函数与过程的区别更多是语义上的区别,而不是语法的区别. 语言纯化论者认为一个函数应该只有一个返回值,这和数学函数一样.即函数只接受输入(参数),通过参数运算返回结果. 除此之外的效果被称为函数的副作用,比如修改全局变量. function sum1(x, y) { return x+y }function sum2(x, y) { alert(x+y) }s

Python函数设计原则

在任何编程语言中,函数的应用主要出于以下两种情况: 代码块重复,这时候必须考虑用到函数,降低程序的冗余度 代码块复杂,这时候可以考虑用到函数,增强程序的可读性 当流程足够繁杂时,就要考虑函数,及如何将函数组合在一起.在Python中做函数设计,主要考虑到函数大小.聚合性.耦合性三个方面,这三者应该归结于规划与设计的范畴.高内聚.低耦合则是任何语言函数设计的总体原则. 如何将任务分解成更有针对性的函数从而导致了聚合性 如何设计函数间的通信则又涉及到耦合性 如何设计函数的大小用以加强其聚合性及降低其

C之函数设计原则(四十二)

我们今天来讲下函数的设计原则.函数从意义上应该是一个独立的功能模块,函数名要在一定程度上反映函数的功能:函数参数名要能够体现参数的意义,尽量避免在函数中使用全局变量. A.当函数参数不应该在函数体内部被修改时,应加上 const 声明:如果参数是指针,且仅作输入参数,则一个加上 const 声明.例: void str_copy(char *str_dest, const char *str_src): B.不能省略返回值的类型,如果函数没有返回值,则应声明其为 void 类型:对参数进行有效性

C语言-第36课 - 函数递归与函数设计技巧

第36课 - 函数递归与函数设计技巧 一. 递归 递归概述 (1) 递归是数学领域中的概念在程序设计中的应用. (2) 递归是一种强有力的程序设计的方法. (3) 递归的本质为函数内部在适当的时候调用自身. 组成部分 (1)递归点:以不同参数调用自身. (2)出口:不在递归调用 下面就是求一个数的阶乘的函数: #include <stdio.h> int func(int x) { if( x > 1 ) { return x * func(x - 1);  //递归点 } else {

设计模式之_6大设计原则(转)

一.单一职责原则(Single Responsibility Principle) 定义:不要存在多于一个导致类变更的原因.通俗的说,即一个类只负责一项职责. 问题由来:类T负责两个不同的职责:职责P1,职责P2.当由于职责P1需求发生改变而需要修改类T时,有可能会导致原本运行正常的职责P2功能发生故障. 解决方案:遵循单一职责原则.分别建立两个类T1.T2,使T1完成职责P1功能,T2完成职责P2功能.这样,当修改类T1时,不会使职责P2发生故障风险:同理,当修改T2时,也不会使职责P1发生故

面向对象重要设计原则概述

第一种讲法 凭什么要用面向对象来编程,不用是否可以?今天我们通过讲这么几个设计原则来说明为什么要用面向对象,它的好处在哪里. 开放-封闭原则:是说软件实体(类.模块.函数等等)应该可以扩展,但是不可修改. 里氏代换原则:子类型必须能够替换掉它们的父类型. 依赖倒转原则:A. 高层模块不应该依赖低层模块.两个都应该依赖抽象.B. 抽象不应该依赖细节.细节应该依赖抽象. 讲解完毕,今天培训结束. 反响:捣浆糊讲解,大家笑骂. 第二种讲法 开放-封闭原则举例 1982年1月,小平同志在会见美国华人协会

架构师内功心法之设计原则

一.架构师内功心法之设计原则 1.为什么要学习软件架构设计原则 1.1.课程目标 通过对节课内容的学习,了解设计原则的重要性. 掌握七大设计原则的具体内容. 1.2.内容定位 学习设计原则,学习设计模式的基础.在实际开发过程中,并不是一定要求所有代码都遵循设计原则,我们要考虑人力.时间.成本.质量,不是刻意追求完美,要在适当的场景遵循设计原则,体现的是一种平衡取舍,帮助我们设计出更加优雅的代码结构. 1.3.七大设计原则 [x] 第1章 Open-Closed Principle 开闭原则 [x

Java程序员应该了解的10个面向对象设计原则

面向对象设计原则: 是OOPS(Object-Oriented Programming System,面向对象的程序设计系统)编程的核心,但大多数Java程序员追逐像Singleton.Decorator.Observer这样的设计模式,而不重视面向对象的分析和设计.甚至还有经验丰富的Java程序员没有听说过OOPS和SOLID设计原则,他们根本不知道设计原则的好处,也不知道如何依照这些原则来进行编程. 众所周知,Java编程最基本的原则就是要追求高内聚和低耦合的解决方案和代码模块设计.查看Ap

设计模式2 面向对象设计原则

面向对象设计原则  原则的目的 面向对象设计原创表  单一职责原则案例 开闭原则 案例 依赖倒转原则 案例 面向对象设计原则  对于面向对象软件系统的设计而言,在支持可维护性的同时,提高系统的可复用性是一个至关重要的问题,如何同时提高一个软件系统的可维护性和可复用性是面向对象设计需要解决的核心问题之一.在面向对象设计中,可维护性的复用是以设计原则为基础的.每一个原则都蕴含一些面向对象设计的思想,可以从不同的角度提升一个软件结构的设计水平.  面向对象设计原则为支持可维护性复用而诞生,这些原则蕴含