• 第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(''))
    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('');
    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] = '';
    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] = '';
    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 */

    小结:

  • 相关阅读:
    python 学习笔记(四)(流程控制)
    python 写斐波那契数列
    python 部分术语对照表
    python 学习笔记(三)(对前两节的补充)
    python # -*- coding: utf-8 -*-
    写出更好的 JavaScript 条件语句
    PHP消息队列实现及应用
    VUE3.0 路由去掉#号
    php设计模式
    workerman 可能需要用到的函数
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9563272.html
Copyright © 2020-2023  润新知