⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trsapi.c

📁 微软的基于HMM的人脸识别原代码, 非常经典的说
💻 C
📖 第 1 页 / 共 5 页
字号:
/*M*
//               NSL PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with NSL and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 1997 NSL. All Rights Reserved.
//
//  RCS:
//      $Source: trsapi.c $
//      $Revision: 1.5 $
//      $Date: 1997/27/01 $
//
//  Author: Smirnov I.E.
//  Purpose: Test System API Implementation
//
*M*/

#define USE_WIN32_TIMER
#define HIGH_PRIORITY
#define WIN32_LEAN_AND_MEAN

#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <assert.h>


#if (defined USE_WIN32_TIMER || defined HIGH_PRIORITY) && defined WIN32
# include <windows.h>
#endif

#include "trsapi.h"                     /* <stdio.h> inside                  */

/* --------------------------------------------------------------------------*/

#if (defined USE_WIN32_TIMER || defined HIGH_PRIORITY) && defined WIN32
#include "trswind.h"
#endif

/* --------------------------------------------------------------------------*/
#if defined putchar
# undef putchar
#endif

#define putchar _putchar
#define printf  _printf
#define fprintf _fprintf
#define gets    _gets

/* === For High Priority Timers 0..4 ========================================*/
#if defined HIGH_PRIORITY && defined WIN32
  void trsHighPriority(void) {
    SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_TIME_CRITICAL);
  }
  void trsNormalPriority(void) {
    SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_NORMAL);
  }
#else
#define trsHighPriority()
#define trsNormalPriority()
#endif

#define TRS_MIN_HP_TIMER 0
#define TRS_MAX_HP_TIMER TRS_MAX_TIMER/2

/* ==========================================================================*/

#define COMMENT_EMPTY     0
#define COMMENT_STORING   1
#define COMMENT_READY    -1
#define ____(str)                       /* No action                         */

/* ==========================================================================*/

typedef struct _CommentsType {          /* Test Comment List                 */
  struct _CommentsType  *next;          /* Next test in a list               */
  char                  *comment;       /* Testing function name             */
} CommentsType;

/* ==========================================================================*/

TRSVAR(TRSid_t   *) Trs_CurrentTest = NULL;
TRSVAR(int        ) Trs_FlowStatus  = TRS_CONTINUE;
TRSVAR(int        ) Trs_PageSize    = 0;
TRSVAR(int        ) Trs_ReadSource  = TRS_None;
TRSVAR(char       ) Trs_String[INPBUF_LEN] = {0};

/* --- previous TRSVAR variables ------------------------------------------*/
static char          CatalogSep  = '\\';
static char          Cmd[1024]   = "";
static char          cmdprefix   = '!';
static char          ExePath[512]= "";
static char *        helpprompt  = ";!e;!q;!p;!r;!t;\?;\?\?;?h;?l;?L;<;*<;>;*>"
                                ";*<file[sec];*>file[sec]";
static FILE *        IniFile     = NULL;
static char *        IniFileName = NULL;
static FILE *        LstFile     = NULL;
static char *        LstFileName = NULL;
static char *        NullStr     ="";   /* ==NullStr - always length = 0     */
static char *        prompt      = "Enter ";
static FILE *        SumFile     = NULL;
static char *        SumFileName = NULL;
static char *        TestAppName = NULL;
static char *        TestID      = "TestSystem";
static TRSid_t *     TestList    = NULL;;
static FILE *        CsvFile     = NULL;
static char *        CsvFileName = NULL;

/* ------------------------------------------------------------------------- */

static int           TestResult      = FALSE;
static time_t        TotalTime;
static time_t        TestTime;
static CommentsType *CommentList     = NULL;
static int           CommentStatus   = COMMENT_EMPTY;
static int           CurrLine;
static int           FullHelp        = FALSE;
static char         *TmpIniFileName= "TRSFUN.tmp";
static char         *LoadIniFileName;

static int           RunMode         = FALSE;
static int           DebugMode       = FALSE;
static int           ReRunMode       = FALSE;
/*static int           LoopMode        = FALSE;*/

static int           ReRunCount      = 0;
static int           LoopMax         = 1;
static int           LoopCount;
static char         *ResultString    = NULL;
static TRSProcTerm_t Terminate       = NULL;

/* ==========================================================================*/

#if !defined _TRSREAD_C
TRSFUN(int,  trs_arInit,(void)){return 0;}
TRSFUN(void, trs_arTerminate,(void)){}
#endif

/* ==========================================================================*/
TRSFUN(int, trsStatus,(void)){                    /* Return flow status        */
  return Trs_FlowStatus;
}

TRSFUN(int, trsReadSource,(void)){                        /* Return read source        */
  return Trs_ReadSource;
}

TRSFUN(TRSProcTerm_t,trsRegTerm,(TRSProcTerm_t Term)) {
  TRSProcTerm_t  oldTerm = Terminate;

  Terminate=Term;
  return oldTerm;
}

TRSFUN(void *,trsGet,(int item)){
  if (item) return NULL;
  return NULL;
}

/* --- Allocate / Deallocate ------------------------------------------------*/

/*typedef long int       INT32;*/
#ifndef WIN32
    typedef int INT32;
#endif

static INT32     Pattern=0xFFFFFFFFL;
static INT32     Patterns=3;
static INT32     MemAllocated=0;              /* through guardAlloc        */
static INT32    *FullBlock;                   /* returned by calloc        */
static INT32     FullPatterns;                /* pat no in releasing block */
static INT32     OriginalSize;                /* called size to alloc,bytes*/

TRSFUN(void,  trsGuardPattern,(long pattern, int number)){
  if ((number>0) && (number<=16)) {
    Patterns=number;                            /* No of check patterns      */
    Pattern=pattern;
  }
}

TRSFUN(long int,  trsGuardAllocated,(long int base)) {
  return (MemAllocated-base);
}

TRSFUN(void *, _trsGuardcAlloc,(size_t number, size_t size,char *file,int line)) {
  size_t     newnumber;                        /* + extra space for guards  */
  INT32     *res;
  INT32     *block;
  INT32      i;

  newnumber=number+((size_t)(Patterns*4+2)*sizeof(Pattern) - 1 + size)/size;
  res=(INT32*)calloc(newnumber,size);

  if (!res)
    trsError(TRS_StsNoMem,"trsGuardcAlloc","",file,line);
  else {
    block=res;
    i=Patterns-1;
    while (i--) {                               /* if more, than 1 pattern   */
      (*block++) =Pattern^0xAAAAAAAAL;          /* pattern^10101010          */
      (*block++) =Pattern;
    }

    (*block++) =Patterns;                       /* Number of check patterns  */
    (*block++) =(INT32)number*(INT32)size;      /* Orig size of block, bytes */
    (*block++) =Pattern^0xAAAAAAAAL;            /* pattern^10101010          */
    (*block++) =Pattern;

    res   = block;                              /* start of allocated block  */
    block = (INT32*)((INT32)res+
                     (INT32)number*(INT32)size);/* end of allocated block    */

    i=Patterns;
    while (i--) {
      (*block++) = Pattern^0x55555555L;         /* pattern^01010101          */
      (*block++) =~Pattern;
    }
    MemAllocated = MemAllocated + (INT32) number * (INT32) size;
  }
  return res;
}

TRSFUN(int, _trsGuardCheck,(void * ptr,char *file,int line)){
  INT32  *block;
  INT32   pattern;
  INT32   i;

  if (ptr) {
    block=(INT32*)ptr;
    pattern=*(--block);
    if ((INT32)(pattern^0xAAAAAAAAL) != *(--block)) {
      trsError(TRS_StsBadMem,"trsGuardCheck","Base(low) guard",file,line);
      return -1;
    }
    OriginalSize =      *(--block);             /* get block size in bytes   */
    FullPatterns = (int)*(--block);             /* get patterns number       */
    i=FullPatterns-1;
    while (i--) {
      if ( ((INT32)(pattern            ) != *(--block)) ||
           ((INT32)(pattern^0xAAAAAAAAL) != *(--block))) {
        trsError(TRS_StsBadMem,"trsGuardCheck","Low guard",file,line);
        return -1;
      }
    }
    FullBlock=block;                            /* original start of mem block*/
    block = (INT32*)((INT32)ptr+
                     (INT32)OriginalSize);      /* end of allocated block    */
    i=FullPatterns;
    while (i--) {
      if ( (( pattern^0x55555555L) != (*block++)) ||
           ((~pattern            ) != (*block++)) ) {
        trsError(TRS_StsBadMem,"trsGuardCheck","High guard",file,line);
        return 1;
      }
    }
  }
  return 0;
}

TRSFUN(void *, _trsGuardFree,(void * ptr,char *file, int line)){
  INT32 i;
  if (ptr) {
    if (!_trsGuardCheck(ptr,file,line)) {
      for (i=0; i<(FullPatterns*2+2); i=i+1)    /* +Patt no+size             */
        FullBlock[i]=0L;                        /* Clear Low Guard           */
      free(FullBlock);                          /* FullBlock,FullSize        */
      MemAllocated = MemAllocated-OriginalSize; /* were set in _guardCheck   */
      ptr=NULL;
    } else trsError(TRS_StsInternal,"trsGuardFree",
                    "Unable to free corrupted memory",file,line);
  }
  return ptr;
}
/* ------------------------------------------------------------------------- */
TRSFUN(void *, _trsmAlloc,(size_t size,char *file, int line)) {
/*  static void * res;*/
  void * res;
  res=malloc(size);
  if (!res)
    trsError(TRS_StsNoMem,"trsmAlloc","",file,line);
/*  else
    MemAllocated=MemAllocated+(unsigned long int)size;
*/
  return res;
}

TRSFUN(void *, trsFree, (void * ptr)) {
  if (ptr)  free(ptr);
  return NULL;
}

TRSFUN(void *, _trsreAlloc,(void *buf,size_t size, char *file,int line)) {
/*  static void * res;*/
  void * res;

  res=realloc(buf,size);
  if (!res) trsError(TRS_StsNoMem,"trsreAlloc","",file,line);
  return res;
}

TRSFUN(float  *, trssbAllocate,(int n)) {
  float *ptr;
  ptr=(float*)calloc(n,sizeof(float));
  if (!ptr) trsError(TRS_StsNoMem,"trssbAllocate","",TRS_FILE,__LINE__);
  return ptr;
}

TRSFUN(double *, trsdbAllocate,(int n)){
  double *ptr;

  ptr=(double*)calloc(n,sizeof(double));
  if (!ptr) trsError(TRS_StsNoMem,"trsdbAllocate","",TRS_FILE,__LINE__);
  return ptr;
}

TRSFUN(short  *, trswbAllocate,(int n)){
  short *ptr;

  ptr=(short*)calloc(n,sizeof(short));
  if (!ptr) trsError(TRS_StsNoMem,"trswbAllocate","",TRS_FILE,__LINE__);
  return ptr;
}

TRSFUN(int  *, trsibAllocate,(int n)){
  int *ptr;

  ptr=(int*)calloc(n,sizeof(int));
  if (!ptr) trsError(TRS_StsNoMem,"trsibAllocate","",TRS_FILE,__LINE__);
  return ptr;
}


TRSFUN(long  *, trslbAllocate,(int n)){
  long *ptr;

  ptr=(long*)calloc(n,sizeof(long));
  if (!ptr) trsError(TRS_StsNoMem,"trslbAllocate","",TRS_FILE,__LINE__);
  return ptr;
}

/* ==========================================================================*/

TRSFUN(char *, StringDup,(const char *str)){
  char * res;
  int    len=1;

  if (str) len=len+strlen(str);
  res=(char*)trsmAlloc(len);
  if (!res) printf("No Memory, StringDup(%s)\n",str);
  else {
    if (str) strcpy(res,str);
    else *res=0;
  }
  return res;
}

TRSFUN(char *, reStringDup,(char *oldptr,const char *str)){
  char * res;
  trsFree(oldptr);
  res=StringDup(str);
  if (!res) printf("No Memory, reStringDup(%s)\n",str);
  return res;
}

TRSFUN(char *, StringCat,(char *str,const char *suffix)) {
  char *res;
  int   len=1;

  if (str)    len=len+strlen(str);
  if (suffix) len=len+strlen(suffix);
  res=(char*)realloc(str,len);
  if (!res) {
    printf("?StringCat-No Memory for \"%s%s\"\n",str,suffix);
  } else
    if (suffix) strcat(res,suffix);
  return res;
}

TRSFUN(int, StringnCpy,(char *dst,const char *src,int size)) {
  int i=0;

  if (!dst) return 0;
  if (src)
    for (;i<size-1;i=i+1) {
      if (!src[i]) break;
      dst[i]=src[i];
    }
  dst[i]=0;
  return i;
}

TRSFUN(int, SpaceCut,(char *str)) {
  int i=0,j=0,zeropos=0;
  int copy=FALSE;

  if (!str) return 0;

  while (str[i]!=0) {
    if (str[i]!=' ') {
      copy=copy||TRUE;                          /* Set after leading spaces  */
      zeropos=j+1;                              /* First trailing space      */
    }
    if (copy) {                                 /* start copying             */
      str[j]=str[i];
      j=j+1;
    }
    i=i+1;
  }
  str[zeropos]=0;
  return zeropos;
}

TRSFUN(int, StrParse,(char *buf,char *seps,int n,...)){
  va_list   argptr;
  int       k=1,sepno=0;
  char     *dst;
  char      sep;

  va_start(argptr,n);

  if ((buf) && (seps)) {
    sep=seps[0];

    while (n>0) {
      dst=va_arg(argptr,char*);                 /* new dst                   */
      while ((*buf)!=0) {                       /* until end of string       */
        if (((*buf)==sep) && (n>1)) {           /* not last dst              */
          sepno=sepno+1;                        /* number of used separators */
          if (seps[k]!=0) {sep=seps[k];k=k+1;}  /* new separator             */
          buf++;                                /* skip sep in buf           */
          break;
        }
        (*dst)=(*buf);                          /* fill dst                  */
        buf++;
        dst++;
      }
      (*dst)=0;                                 /* terminate dst             */
      n=n-1;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -