예제 #1
0
static VECTORNAME* VFUN(createVector)(int max_size) {
  VECTORNAME* v = (VECTORNAME*)MYALLOC(sizeof(VECTORNAME));

  if ( !v )
  	return NULL;
  v->size = 0;
  v->max_size = max_size;
  v->values  = (VECTORTYPE*)MYALLOC(v->max_size*sizeof(VECTORTYPE));
  return v;
}
예제 #2
0
//-------------------------------------------------------------
// Get whole file name from FILECONTENT
//-------------------------------------------------------------
LPTSTR GetWholeFileName(LPFILECONTENT lpStartFC, size_t cchExtra) 
{
	LPFILECONTENT lpFC;
	LPTSTR lpszName;
	LPTSTR lpszTail;
	size_t cchName;

	cchName = 0;
	for (lpFC = lpStartFC; NULL != lpFC; lpFC = lpFC->lpFatherFC) {
		if (NULL != lpFC->lpszFileName) {
			cchName += lpFC->cchFileName + 1;  // +1 char for backslash or NULL char
		}
	}
	if (0 == cchName) {  // at least create an empty string with NULL char
		cchName++;
	}

	lpszName = MYALLOC((cchName + cchExtra) * sizeof(TCHAR));

	lpszTail = &lpszName[cchName - 1];
	lpszTail[0] = (TCHAR)'\0';

	for (lpFC = lpStartFC; NULL != lpFC; lpFC = lpFC->lpFatherFC) {
		if (NULL != lpFC->lpszFileName) {
			cchName = lpFC->cchFileName;
			_tcsncpy(lpszTail -= cchName, lpFC->lpszFileName, cchName);
			if (lpszTail > lpszName) {
				*--lpszTail = (TCHAR)'\\';
			}
		}
	}
	return lpszName;
}
예제 #3
0
파일: gosthash.c 프로젝트: 274914765/C
/* Initialize gost_hash ctx - cleans up temporary structures and
 * set up substitution blocks
 */
int init_gost_hash_ctx (gost_hash_ctx * ctx, const gost_subst_block * subst_block)
{
    memset (ctx, 0, sizeof (gost_hash_ctx));
    ctx->cipher_ctx = (gost_ctx *) MYALLOC (sizeof (gost_ctx));
    if (!ctx->cipher_ctx)
    {
        return 0;
    }
    gost_init (ctx->cipher_ctx, subst_block);
    return 1;
}
예제 #4
0
static void VFUN(add2Vector)(VECTORNAME* v, VECTORTYPE val) {
  if ( (v->size+1) >= v->max_size ) {
    int i;
    VECTORTYPE *new_values;

    v->max_size *= 2;
    new_values = (VECTORTYPE*)MYALLOC(v->max_size*sizeof(VECTORTYPE));
    for(i=0; i<v->size; i++)
  	new_values[i] = v->values[i];
    MYFREE(v->values);
    v->values = new_values;
  }
  v->values[v->size++] = val;
}
예제 #5
0
//-----------------------------------------------------------------
// Create a Trie (Prefix Tree) Node
//-----------------------------------------------------------------
trieNode_t *TrieCreateNode(wchar_t key)
{
	trieNode_t *node = NULL;
	node = (trieNode_t *)MYALLOC(sizeof(trieNode_t));

	if (NULL == node) {
		printf("  > Failed to allocated sufficient memory...\n");
		return node;
	}

	node->key = key;
	node->next = NULL;
	node->children = NULL;
	node->parent = NULL;
	node->prev = NULL;
	return node;
}
예제 #6
0
// ----------------------------------------------------------------------
// Get file snap shot
// ----------------------------------------------------------------------
VOID GetFilesSnap(LPSNAPSHOT lpShot, LPTSTR lpszFullName, LPFILECONTENT lpFatherFC, LPFILECONTENT *lplpCaller) 
{
	LPFILECONTENT lpFC;
	HANDLE hFile;
	BOOL calculateHash = TRUE;
	BOOL found = FALSE;

	// Full dir/file name is already given
	// Extra local block to reduce stack usage due to recursive calls
	{
		LPTSTR lpszFindFileName;
		lpszFindFileName = NULL;
		// Get father file data if not already provided (=called from FileShot)
		// lpFatherFC only equals "C:\" and is called once at start
		if (NULL == lpFatherFC) 
		{
			// Check if file is to be GENERIC excluded
			if ((NULL == lpszFullName)
				|| (((TCHAR)'.' == lpszFullName[0])  // fast exclusion for 99% of the cases
				&& ((0 == _tcscmp(lpszFullName, TEXT(".")))
				|| (0 == _tcscmp(lpszFullName, TEXT("..")))))) {
				return;
			}
			// Create new file content
			lpFatherFC = MYALLOC0(sizeof(FILECONTENT));

			// Set file name length
			lpFatherFC->cchFileName = _tcslen(lpszFullName);
			// Copy file name to new buffer for directory search and more
			lpszFindFileName = MYALLOC((lpFatherFC->cchFileName + 4 + 1) * sizeof(TCHAR));  // +4 for "\*.*" search when directory (later in routine)
			_tcscpy(lpszFindFileName, lpszFullName);
			// Special case if root dir of a drive was specified, needs trailing backslash otherwise current dir of that drive is used
			if ((TCHAR)':' == lpszFindFileName[lpFatherFC->cchFileName - 1]) {
				lpszFindFileName[lpFatherFC->cchFileName] = (TCHAR)'\\';
				lpszFindFileName[lpFatherFC->cchFileName + 1] = (TCHAR)'\0';
			}
			//printf("lpszFullName: %ws\n", lpszFullName);
			//printf("lpszFindFileName: %ws\n", lpszFindFileName);

			hFile = FindFirstFile(lpszFullName, &FindData);
			if (INVALID_HANDLE_VALUE != hFile) {
				FindClose(hFile);
			}
			else {
				// Workaround for some cases in Windows Vista and later
				ZeroMemory(&FindData, sizeof(FindData));
				
				hFile = CreateFile(lpszFullName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
				if (INVALID_HANDLE_VALUE != hFile) {
					BY_HANDLE_FILE_INFORMATION FileInformation;
					BOOL bResult;

					bResult = GetFileInformationByHandle(hFile, &FileInformation);
					if (bResult) {
						FindData.dwFileAttributes = FileInformation.dwFileAttributes;
						FindData.ftCreationTime = FileInformation.ftCreationTime;
						FindData.ftLastAccessTime = FileInformation.ftLastAccessTime;
						FindData.ftLastWriteTime = FileInformation.ftLastWriteTime;
						FindData.nFileSizeHigh = FileInformation.nFileSizeHigh;
						FindData.nFileSizeLow = FileInformation.nFileSizeLow;
					}
					else {
						FindData.dwFileAttributes = GetFileAttributes(lpszFullName);
						if (INVALID_FILE_ATTRIBUTES == FindData.dwFileAttributes) {
							FindData.dwFileAttributes = 0;
						}
						bResult = GetFileTime(hFile, &FindData.ftCreationTime, &FindData.ftLastAccessTime, &FindData.ftLastWriteTime);
						if (!bResult) {
							FindData.ftCreationTime.dwLowDateTime = 0;
							FindData.ftCreationTime.dwHighDateTime = 0;
							FindData.ftLastAccessTime.dwLowDateTime = 0;
							FindData.ftLastAccessTime.dwHighDateTime = 0;
							FindData.ftLastWriteTime.dwLowDateTime = 0;
							FindData.ftLastWriteTime.dwHighDateTime = 0;
						}
						FindData.nFileSizeLow = GetFileSize(hFile, &FindData.nFileSizeHigh);
						if (INVALID_FILE_SIZE == FindData.nFileSizeLow) {
							FindData.nFileSizeHigh = 0;
							FindData.nFileSizeLow = 0;
						}
					}
					CloseHandle(hFile);
				}
			}

			// Remove previously added backslash (if any)
			lpszFindFileName[lpFatherFC->cchFileName] = (TCHAR)'\0';

			// Copy pointer to current file into caller's pointer
			if (NULL != lplpCaller) {
				*lplpCaller = lpFatherFC;
			}
			          
			// Increase dir/file count
			if (ISFILE(FindData.dwFileAttributes)) {
				lpShot->stCounts.cFiles++;
			}
			else {
				lpShot->stCounts.cDirs++;
			}

			// Copy file name
			lpFatherFC->lpszFileName = MYALLOC((lpFatherFC->cchFileName + 1) * sizeof(TCHAR));
			_tcscpy(lpFatherFC->lpszFileName, lpszFullName);

			// Copy file data
			lpFatherFC->nWriteDateTimeLow = FindData.ftLastWriteTime.dwLowDateTime;
			lpFatherFC->nWriteDateTimeHigh = FindData.ftLastWriteTime.dwHighDateTime;
			lpFatherFC->nAccessDateTimeLow = FindData.ftLastWriteTime.dwLowDateTime;
			lpFatherFC->nAccessDateTimeHigh = FindData.ftLastWriteTime.dwHighDateTime;
			lpFatherFC->nFileSizeLow = FindData.nFileSizeLow;
			lpFatherFC->nFileSizeHigh = FindData.nFileSizeHigh;
			lpFatherFC->nFileAttributes = FindData.dwFileAttributes;

			// Set "lpFirstSubFC" pointer for storing the first child's pointer
			lplpCaller = &lpFatherFC->lpFirstSubFC;
		}

		// If father is a file, then leave (=special case when called from FileShot)
		if (ISFILE(lpFatherFC->nFileAttributes)) {
			if (NULL != lpszFindFileName) {
				MYFREE(lpszFindFileName);
			}
			return;
		}

		// Process all entries of directory
		// a) Create search pattern and start search
		if (NULL == lpszFindFileName) {
			lpszFindFileName = lpszFullName;
		}
		_tcscat(lpszFindFileName, TEXT("\\*.*"));
		hFile = FindFirstFile(lpszFindFileName, &FindData);
		if (lpszFindFileName != lpszFullName) {
			MYFREE(lpszFindFileName);
		}
	}  // End of extra local block
	if (INVALID_HANDLE_VALUE == hFile) {  
		// error: nothing in dir, no access, etc.
		//printf(">>> ERROR: fileshot.c: getFileSnap: INVALID_HANDLE_VALUE: %ws\n", lpszFullName);
		return;
	}
	// b) process entry then find next
	do {
		lpszFullName = NULL;
		//BOOL calculateHash = TRUE;
		//BOOL found = FALSE;

		// Check if file is to be GENERIC excluded (dot dirs)
		if ((NULL == FindData.cFileName)
			|| (((TCHAR)'.' == FindData.cFileName[0])  // fast exclusion for 99% of the cases
			&& ((0 == _tcscmp(FindData.cFileName, TEXT(".")))
			|| (0 == _tcscmp(FindData.cFileName, TEXT("..")))))) {
			continue;  // ignore this entry and continue with next file
		}

		// Create new file content
		lpFC = MYALLOC0(sizeof(FILECONTENT));
		// Set father of current key
		lpFC->lpFatherFC = lpFatherFC;
		// Set file name length
		lpFC->cchFileName = _tcslen(FindData.cFileName);

		// Allocate memory copy file name to FILECONTENT
		lpFC->lpszFileName = MYALLOC0((lpFC->cchFileName + 1) * sizeof(TCHAR));
		_tcscpy(lpFC->lpszFileName, FindData.cFileName);

		// Static blacklist for directories
		if (ISDIR(FindData.dwFileAttributes))
		{
			if (performStaticBlacklisting)
			{
				LPTSTR lpszFullPath;
				lpszFullPath = GetWholeFileName(lpFC, 4);

				found = TrieSearchPath(blacklistDIRS->children, lpszFullPath);
				if (found)
				{
					lpShot->stCounts.cFilesBlacklist++;
					MYFREE(lpszFullPath);
					FreeAllFileContents(lpFC);

					// Increase value count for display purposes
					lpShot->stCounts.cDirsBlacklist++;
					
					// Ignore this entry and continue with next brother value
					continue;  
				}
				else
				{
					MYFREE(lpszFullPath);
				}
			}
		}

		// Check if the file system entry is a symbolic link
		// If so, skip as it actually resides somewhere else on the file system!
		if (ISSYM(FindData.dwFileAttributes)) {
			if (ISFILE(FindData.dwFileAttributes)) {
				lpShot->stCounts.cFilesBlacklist++;
			}
			else {
				lpShot->stCounts.cDirsBlacklist++;
			}
			continue;
		}

		// Blacklisting implementation for files		
		if (ISFILE(FindData.dwFileAttributes))
		{
			if (dwBlacklist == 1)
			{
				// First snapshot, therefore populate the Trie (Prefix Tree)
				// Get the full file path
				LPTSTR lpszFullPath;
				lpszFullPath = GetWholeFileName(lpFC, 4);

				// Add full path to file blacklist prefix tree, then free path
				TrieAdd(&blacklistFILES, lpszFullPath);
				MYFREE(lpszFullPath);

				// Increase value count for display purposes
				lpShot->stCounts.cFiles++;

				// Ignore this entry and continue with next brother value
				//continue;  
			}
			else if (dwBlacklist == 2)
			{
				// Not the first snapshot, so either:
				// 1) If blacklisting enabled: Ignore file if its in the blacklist
				// 2) If hashing enabled (and not blacklisting): Mark file as not to be hashed
				LPTSTR lpszFullPath;
				lpszFullPath = GetWholeFileName(lpFC, 4);
				found = TrieSearchPath(blacklistFILES->children, lpszFullPath);
				if (found)
				{
					if (performDynamicBlacklisting)
					{
						MYFREE(lpszFullPath);
						FreeAllFileContents(lpFC);

						// Increase value count for display purposes
						lpShot->stCounts.cFilesBlacklist++;

						// Ignore this entry and continue with next brother value
						continue;  
					}
					if (performSHA1Hashing || performMD5Hashing)
					{
						lpShot->stCounts.cFiles++;
						MYFREE(lpszFullPath);
						calculateHash = FALSE;
					}
				}
				else
				{
					MYFREE(lpszFullPath);
				}
			}
		}
					
		// Copy pointer to current file into caller's pointer
		if (NULL != lplpCaller) {
			*lplpCaller = lpFC;
		}

		// Increase dir/file count
		if (ISFILE(FindData.dwFileAttributes)) {
			lpShot->stCounts.cFiles++;
		}
		else {
			lpShot->stCounts.cDirs++;
		}

		// Copy file data
		lpFC->nWriteDateTimeLow = FindData.ftLastWriteTime.dwLowDateTime;
		lpFC->nWriteDateTimeHigh = FindData.ftLastWriteTime.dwHighDateTime;
		lpFC->nAccessDateTimeLow = FindData.ftLastWriteTime.dwLowDateTime;
		lpFC->nAccessDateTimeHigh = FindData.ftLastWriteTime.dwHighDateTime;
		lpFC->nFileSizeLow = FindData.nFileSizeLow;
		lpFC->nFileSizeHigh = FindData.nFileSizeHigh;
		lpFC->nFileAttributes = FindData.dwFileAttributes;

		// Calculate file hash (computationally intensive!)
		// This should only be executed in the following scenarios:
		// 1) If the file system entry is a data file 
		// 2) If the data file is not blacklisted (previously known)
		if (ISFILE(FindData.dwFileAttributes))
		{
			if (dwBlacklist == 2 && calculateHash) {
			    if (performSHA1Hashing)
			    {
				    lpFC->cchSHA1 = 40;
				    lpFC->lpszSHA1 = MYALLOC((lpFC->cchSHA1 + 1) * sizeof(TCHAR));
				    _tcscpy(lpFC->lpszSHA1, CalculateSHA1(GetWholeFileName(lpFC, 4)));
                }
                if (performMD5Hashing)
                {
				    lpFC->cchMD5 = 32;
				    lpFC->lpszMD5 = MYALLOC((lpFC->cchMD5 + 1) * sizeof(TCHAR));
				    _tcscpy(lpFC->lpszMD5, CalculateMD5(GetWholeFileName(lpFC, 4)));
				}
				if (performMD5BlockHashing)
				{
					LPMD5BLOCK theMD5Block = CalculateMD5Blocks(GetWholeFileName(lpFC, 4), dwHashBlockSize);
					lpFC->lpMD5Block = MYALLOC0(sizeof(MD5BLOCK));
					lpFC->lpMD5Block = theMD5Block;
				}
			}
		}

		// Print file system shot status
		if ((dwBlacklist == 2 && performDynamicBlacklisting) || (performStaticBlacklisting)) {
			printf("  > Dirs: %d  Files: %d  Blacklisted Dirs: %d  Blacklisted Files: %d\r", lpShot->stCounts.cDirs, 
				lpShot->stCounts.cFiles,
				lpShot->stCounts.cDirsBlacklist,
				lpShot->stCounts.cFilesBlacklist);
		}
		else {
			printf("  > Dirs: %d  Files: %d\r", lpShot->stCounts.cDirs, lpShot->stCounts.cFiles);
		}

		// ATTENTION!!! FindData will be INVALID from this point on, due to recursive calls
		// If the entry is a directory, then do a recursive call for it
		// Pass this entry as father and "lpFirstSubFC" pointer for storing the first child's pointer
		if (ISDIR(lpFC->nFileAttributes)) {
			if (NULL == lpszFullName) {
				lpszFullName = GetWholeFileName(lpFC, 4);  // +4 for "\*.*" search (in recursive call)
			}
			GetFilesSnap(lpShot, lpszFullName, lpFC, &lpFC->lpFirstSubFC);
		}

		if (NULL != lpszFullName) {
			MYFREE(lpszFullName);
		}

		// Set "lpBrotherFC" pointer for storing the next brother's pointer
		lplpCaller = &lpFC->lpBrotherFC;
	} while (FindNextFile(hFile, &FindData));
	FindClose(hFile);
}
예제 #7
0
int aln_splice_local_core(uchar *gen, int gen_len, uchar *est, int est_len,
		AlnParam *ap, path_t *path, int *path_len)
{
	register dpcell_t *q;
	register int i;
	register dpscore_t *s;

	int **s_array, *score_array;
	dpcell_t **dpcell;
	dpscore_t *dpscore;
	int *donor, *acceptor;
	int j, mi, mj, max_g;
	int last_g, curr_g, curr_last_g;
	int ins, del, n;
	uchar dtmp, atmp;

	int gap_open, gap_ext, gap_oe, good_splice, bad_splice, *sc_matrix;

	gap_open = ap->gap_open;
	gap_ext = ap->gap_ext;
	gap_oe = gap_open - gap_ext;
	good_splice = ap->good_splice;
	bad_splice = ap->bad_splice;
	sc_matrix = ap->matrix;
	if (*sc_donor != ap->bad_splice) est_genome_init(ap);
	
	if (gen_len < 5) return MINOR_INF;

	/* memory allocation */
	s_array = (int**)MYALLOC(sizeof(int*) * ap->row);
	for (i = 0; i != ap->row; ++i)
		s_array[i] = (int*)MYALLOC(sizeof(int) * gen_len);
	dpcell = (dpcell_t**)MYALLOC(sizeof(dpcell_t*) * (est_len + 1));
	for (j = 0; j <= est_len; ++j)
		dpcell[j] = (dpcell_t*)MYALLOC(sizeof(dpcell_t) * (gen_len + 1));
	donor = (int*)MYALLOC(sizeof(int) * (gen_len + 2));
	acceptor = (int*)MYALLOC(sizeof(int) * (gen_len + 2));
	dpscore = (dpscore_t*)MYALLOC(sizeof(dpscore_t) * (gen_len + 1));
	
	aln_init_score_array(gen, gen_len, ap->row, sc_matrix, s_array);
	for (i = 0; i != ap->row; ++i) --s_array[i];
	--est; --gen;
	
	/* set donor-acceptor array */
	donor[gen_len - 1] = donor[gen_len] = sc_donor[0xf];
	dtmp = gen[1] & 0x3;
	for (i = 1; i <= gen_len - 1; ++i) {
		if (gen[i+1] > 3 || gen[i] > 3) donor[i] = bad_splice; /* N is encountered */
		else {
			dtmp = ((dtmp<<2) | gen[i+1]) & 0xf;
			donor[i] = sc_donor[dtmp];
		}
	}
	acceptor[gen_len+1] = acceptor[0] = acceptor[1] = acceptor[2] = sc_acceptor[0xf];
	atmp = gen[1] & 0x3;
	for (i = 3; i <= gen_len; ++i) {
		if (gen[i] > 3 || gen[i-1] > 3) acceptor[i] = bad_splice;
		else {
			atmp = ((atmp<<2) | gen[i-1]) & 0xf;
			acceptor[i] = sc_acceptor[atmp];
		}
	}
	
	/* first row */
	max_g = 0; mi = mj = 0;
	for (i = 0, q = dpcell[0], s = dpscore; i <= gen_len; ++i, ++q, ++s) {
		s->G = s->I = 0;
		q->Gt = FROM_0;
	}
	/* core dynamic programming */
	for (j = 1; j <= est_len; ++j) {
		last_g = del = n = 0;
		score_array = s_array[est[j]];
		q = dpcell[j];
		q->Gt = SL_FROM_0;
		++q;
		for (i = 1, s = dpscore; i <= gen_len; ++i, ++s, ++q) {
			curr_g = s->G + score_array[i];
			if (curr_g < 0) {
				curr_g = 0;
				q->Gt = SL_FROM_0;
			} else q->Gt = SL_FROM_M;
			if (last_g > donor[i]) {
				if (last_g - donor[i] > n) {
					n = last_g - donor[i];
					q->Nt = SL_FROM_G;
				} else q->Nt = SL_FROM_N;
				if (curr_g < n - acceptor[i+1]) {
					curr_g = n - acceptor[i+1];
					q->Gt = SL_FROM_N;
				}
			} else q->Nt = SL_FROM_N;
			if (last_g > gap_open) {
				if (del > last_g - gap_oe) {
					del -= gap_ext;
					q->Dt = SL_FROM_D;
				} else {
					del = last_g - gap_open;
					q->Dt = SL_FROM_G;
				}
				if (curr_g < del) {
					curr_g = del;
					q->Gt = SL_FROM_D;
				}
			}
			curr_last_g = (s+1)->G;
			if (curr_last_g > gap_open) {
				if (s->I > curr_last_g - gap_oe) {
					ins = s->I - gap_ext;
					q->It = SL_FROM_I;
				} else {
					ins = curr_last_g - gap_open;
					q->It = SL_FROM_G;
				}
				if (curr_g < ins) {
					curr_g = ins;
					q->Gt = SL_FROM_I;
				}
				s->I = ins;
			} else s->I = 0;
			s->G = last_g;
			last_g = curr_g;
			if (curr_g > max_g) {
				max_g = curr_g; mi = i; mj = j;
			}
		}
		s->G = last_g;
	}

	back_trace(dpcell, mi, mj, path, path_len);

	/* free */
	MYFREE(dpscore);
	MYFREE(acceptor); MYFREE(donor);
	for (j = 0; j <= est_len; ++j)
		MYFREE(dpcell[j]);
	for (i = 0; i != ap->row; ++i)
		MYFREE(s_array[i] + 1);
	MYFREE(s_array);
	MYFREE(dpcell);
	return max_g;
}
예제 #8
0
/*.BE*/
int spline                                        /* nichtparametrischer kubischer Polynomspline .......*/
/*.BA*/
/*.IX{spline}*/
/*.BE*/
(
int  m,                                           /* Anzahl der Stuetzstellen ............*/
REAL x[],                                         /* Stuetzstellen .......................*/
REAL y[],                                         /* Stuetzwerte .........................*/
int  marg_cond,                                   /* Art der Randbedingung ...............*/
REAL marg_0,                                      /* linke Randbedingung .................*/
REAL marg_n,                                      /* rechte Randbedingung ................*/
int  save,                                        /* dynamische Hilfsfelder sichern? .....*/
REAL b[],                                         /* Splinekoeffizienten von (x-x[i]) ....*/
REAL c[],                                         /* Splinekoeffizienten von (x-x[i])^2 ..*/
REAL d[]                                          /* Splinekoeffizienten von (x-x[i])^3 ..*/
)                                                 /* Fehlercode ..........................*/
/*.BA*/

/***********************************************************************
 * zu den vorgegebenen Wertepaaren                                      *
 *                  (x[i], y[i]), i = 0(1)m-1                           *
 * die Koeffizienten eines nichtparametrischen interpolierenden         *
 * kubischen Polynomplines berechnen.                                   *
.BE*)
* Die Art der Randbedingung wird durch den Parameter marg_cond         *
* festgelegt. Die x[i] muessen streng monoton wachsen.                 *
* Bei wiederholten Aufrufen mit gleichen Stuetzstellen, aber verschie- *
* denen Stuetzwerten besteht die Moeglichkeit, die erneute Aufstellung *
* und Zerlegung der Matrix des Gleichungssystems zu vermeiden, indem   *
* man den Parameter save von Null verschieden waehlt und so die Be-    *
* schreibung der Zerlegungsmatrizen fuer den naechsten Aufruf rettet.  *
* Wichtig: Damit der Speicher fuer die Hilfsfelder wieder frei wird    *
* -------- und bei weiteren Aufrufen nicht mit falschen Zerlegungs-    *
*          matrizen gearbeitet wird, muss der letzte Aufruf einer      *
 *          zusammengehoerigen Aufruffolge mit save = 0 statt mit       *
 *          save = 1 ausgefuehrt werden!                                *
 *                                                                      *
 * Eingabeparameter:                                                    *
 * =================                                                    *
 * m:          Anzahl der Stuetzstellen (mindestens 3)                  *
 * x:          [0..m-1]-Vektor mit den x-Koordinaten der Wertepaare     *
 *             (wird nicht benoetigt, falls der vorige Aufruf mit       *
 *              save != 0 stattfand)                                    *
 * y:          [0..m-1]-Vektor mit den y-Koordinaten der Wertepaare     *
 * marg_cond:  = 0: not-a-knot-Bedingung (=> marg_0, marg_n ohne        *
 *                                           Bedeutung)                 *
 *             = 1: marg_0, marg_n sind 1. Ableitungen.                 *
 *             = 2: marg_0, marg_n sind 2. Ableitungen.                 *
 *                  (Fuer marg_0 = marg_n = 0 erhaelt man einen         *
 *                  natuerlichen Spline.)                               *
 *             = 3: marg_0, marg_n sind 3. Ableitungen.                 *
 *             = 4: periodischer Spline (=> marg_0, marg_n ohne         *
 *                                          Bedeutung)                  *
 * marg_0:     Randbedingung in x[0]                                    *
 * marg_n:     Randbedingung in x[m-1]                                  *
 * save:       Flagge, die anzeigt, ob der Speicher fuer die Hilfsfel-  *
 *             der mit den Zerlegungsmatrizen fuer den naechsten Aufruf *
 *             aufbewahrt werden soll. Im Normalfall ist save = 0 zu    *
 *             setzen. Wenn man mehrere Splinefunktionen mit denselben  *
 *             Stuetzstellen x[i], aber anderen y[i] berechnen will     *
 *             (z. B. bei parametrischen Splines), kann man ab dem      *
 *             zweiten Aufruf Rechenzeit sparen, indem man beim ersten  *
 *             Aufruf save = 1 setzt. Dann wird naemlich die neuerliche *
 *             Aufstellung und Zerlegung der Tridiagonalmatrix umgangen *
 *             (=> ca. 4*m Punktoperationen weniger).                   *
 *             Im letzten Aufruf muss man save = 0 waehlen, damit der   *
 *             von den Hilfsfeldern beanspruchte dynamische Speicher    *
 *             fuer andere Programmteile wieder verfuegbar wird.        *
 *                                                                      *
 * Ausgabeparameter:                                                    *
 * =================                                                    *
 * b: \  [0..m-2]-Vektoren mit den Splinekoeffizienten nach dem Ansatz  *
 * c:  >     s(x)  =  a[i] + b[i] * (x - x[i]) + c[i] * (x - x[i]) ^ 2  *
 * d: /                    + d[i] * (x - x[i]) ^ 3.                     *
 *       a entspricht y,                                                *
 *       c hat (wie a) noch ein zusaetzliches Element c[m-1].           *
 *                                                                      *
 * Funktionswert:                                                       *
 * ==============                                                       *
 * =  0: kein Fehler                                                    *
 * = -i: Monotoniefehler: x[i-1] >= x[i]                                *
 * =  1: falscher Wert fuer marg_cond                                   *
 * =  2: m < 3                                                          *
 * =  3: nicht genuegend Heapspeicher fuer die Hilfsfelder              *
 * =  4: marg_cond = 4: Eingabedaten nichtperiodisch                    *
 * >  4: Fehler in trdiag() oder tzdiag()                               *
 * Im Fehlerfall sind die Werte der Ausgabeparameter unbestimmt, und    *
 * der Speicher fuer die Hilfsfelder wird freigegeben.                  *
 *                                                                      *
 * benutzte globale Namen:                                              *
 * =======================                                              *
 * REAL, vminit, vmalloc, vmcomplete, vmfree, VEKTOR, trdiag, tzdiag,   *
 * NULL, ZERO, THREE, HALF, TWO                                         *
.BA*)
***********************************************************************/
/*.BE*/

{

#define ciao(fehler)          /* dafuer sorgen, dass vor dem Beenden */\
{ \
/* von spline() aufgeraeumt wird       */\
vmfree(vmblock); /* Speicherplatz fuer die Hilfsfelder freigeben */\
vmblock = NULL;  /* und dies auch anzeigen                       */\
return fehler;   /* den Fehlercode an den Aufrufer weiterreichen */\
}

   static
      void *vmblock = NULL;                       /* Liste der dynamisch vereinbarten Vek- */
   /* toren. Der Wert NULL zeigt an, dass   */
   /* noch keine Hilfsvektoren aus eventu-  */
   /* ellen frueheren Aufrufen existieren,  */
   /* dass dies also der erste Aufruf einer */
   /* zusammengehoerenden Folge mit glei-   */
   /* chen Stuetzstellen ist.               */
   static
      REAL *h,                                    /* [0..m-2]-Vektor mit den Laengen der Stuetz-   */
   /* stellenintervalle                             */
      *lower,                                     /* [0..m-2]-Vektor mit der unteren Nebendiago-   */
   /* nale der Matrix, spaeter Zerlegungsmatrix     */
   /* von trdiag() bzw. tzdiag()                    */
      *diag,                                      /* [0..m-2]-Vektor mit der Hauptdiagonale der    */
   /* Matrix, spaeter Zerlegungsmatrix von          */
   /* trdiag() bzw. tzdiag()                        */
      *upper,                                     /* [0..m-2]-Vektor mit der oberen Nebendiago-    */
   /* nale der Matrix, spaeter Zerlegungsmatrix     */
   /* von trdiag() bzw. tzdiag()                    */
      *lowrow,                                    /* [0..m-4]-Vektor mit der unteren Zeile der     */
   /* Matrix, spaeter Zerlegungsmatrix von tzdiag() */
      *ricol;                                     /* [0..m-4]-Vektor mit der rechten Spalte der    */
   /* Matrix, spaeter Zerlegungsmatrix von tzdiag() */
   int  n,                                        /* m - 1, Index der letzten Stuetzstelle */
      fehler,                                     /* Fehlercode von trdiag() bzw. tzdiag() */
      i,                                          /* Laufvariable                          */
      erster_aufruf;                              /* Flagge, die anzeigt, dass gerade der  */
   /* erste Aufruf einer Folge stattfindet  */

   n = m - 1;

   if (n < 2)                                     /* zu kleinen Wert fuer n abfangen */
      ciao(2);

   if (marg_cond < 0 || marg_cond > 4)            /* falsches marg_cond abfangen */
      ciao(1);

   if (marg_cond == 4)                            /* periodischer Spline?       */
      if (y[n] != y[0])                           /* Periodizitaet ueberpruefen */
         ciao(4);

   /* 1. Aufruf: Speicher fuer die Hilfsfelder anfordern: 4 [0..n-1]-  */
   /* Vektoren (im periodischen Fall noch 2 [0..n-3]-Vektoren)         */

   if (vmblock == NULL)                           /* erster Aufruf einer Folge? */
   {
      erster_aufruf = 1;
#define MYALLOC(l)  (REAL *)vmalloc(vmblock, VEKTOR, (l), 0)
      vmblock = vminit();                         /* Speicherblock initialisieren */
      h     = MYALLOC(n);                         /* Speicher fuer die       */
      lower = MYALLOC(n);                         /* Hilfsvektoren anfordern */
      diag  = MYALLOC(n);
      upper = MYALLOC(n);
      if (marg_cond == 4)                         /* periodischer Spline mit  */
         if (n > 2)                               /* genuegend Stuetzstellen? */
            lowrow = MYALLOC(n - 2),              /* auch die zusaetzlichen   */
               ricol  = MYALLOC(n - 2);           /* Vektoren versorgen       */
#undef MYALLOC
   }
   else
      erster_aufruf = 0;
   if (! vmcomplete(vmblock))                     /* Ging eine der Speicheranforderungen */
      ciao(3);                                    /* fuer den Block schief?              */

   if (erster_aufruf)
      for (i = 0; i < n; i++)                     /* Schrittweiten berechnen und dabei die */
   {                                              /* Stuetzstellen auf Monotonie pruefen   */
      h[i] = x[i + 1] - x[i];                     /* Schrittweiten berechnen */
      if (h[i] <= ZERO)                           /* Stuetzstellen nicht monoton wachsend? */
         ciao(-(i + 1));
   }

   for (i = 0; i < n - 1; i++)                    /* das Gleichungssystem aufstellen */
   {
                                                  /* rechte Seite */
      c[i] = THREE * ((y[i + 2] - y[i + 1]) / h[i + 1]
         - (y[i + 1] - y[i])     / h[i]);
      if (erster_aufruf)
         diag[i] = TWO * (h[i] + h[i + 1]),       /* Hauptdiagonale   */
            lower[i + 1] = upper[i] = h[i + 1];   /* untere und obere */
   }                                              /* Nebendiagonale   */

   switch (marg_cond)                             /* je nach Randbedingung einige Koeffizienten */
   {                                              /* des Gleichungssystems korrigieren          */
      case 0:                                     /* not-a-knot-Bedingung?              */
         if (n == 2)                              /* nur drei Stuetzstellen?      */
         {                                        /* Da in diesem Fall das Gleichungssystem  */
            /* unterbestimmt ist, wird nur ein Polynom */
            /* 2. Grades berechnet.                    */
            c[0] /= THREE;                        /* rechte Seite    */
            if (erster_aufruf)
               diag[0] *= HALF;                   /* auch die Matrix */
         }
         else                                     /* mehr als drei Stuetzstellen? */
         {
                                                  /* rechte */
            c[0]     *= h[1]     / (h[0]     + h[1]);
                                                  /* Seite  */
            c[n - 2] *= h[n - 2] / (h[n - 1] + h[n - 2]);
            if (erster_aufruf)
               diag[0]      -= h[0],              /* auch die */
                  diag[n - 2]  -= h[n - 1],       /* Matrix   */
                  upper[0]     -= h[0],
                  lower[n - 2] -= h[n - 1];
         }
         break;

      case 1:                                     /* erste Randableitungen vorgegeben?  */
         c[0]     -= (REAL)1.5 * ((y[1] - y[0]) / h[0] - marg_0);
         c[n - 2] -= (REAL)1.5 * (marg_n - (y[n] - y[n - 1]) / h[n - 1]);
         if (erster_aufruf)
            diag[0]     -= HALF * h[0],           /* auch die Matrix */
               diag[n - 2] -= HALF * h[n - 1];    /* vorbesetzen     */
         break;

      case 2:                                     /* zweite Randableitungen vorgegeben? */
         c[0]     -= h[0]     * HALF * marg_0;
         c[n - 2] -= h[n - 1] * HALF * marg_n;
         break;

      case 3:                                     /* dritte Randableitungen vorgegeben? */
         c[0]        += HALF * marg_0 * h[0]     * h[0];
         c[n - 2]    -= HALF * marg_n * h[n - 1] * h[n - 1];
         if (erster_aufruf)
            diag[0]     += h[0],                  /* auch die Matrix */
               diag[n - 2] += h[n - 1];           /* vorbesetzen     */
         break;

      case 4:                                     /* periodischer Spline?               */
         c[n - 1] = THREE * ((y[1] - y[0])     / h[0] -
            (y[n] - y[n - 1]) / h[n - 1]);
         if (erster_aufruf)
            if (n > 2)
               diag[n - 1]  = TWO * (h[0] + h[n - 1]),
                  ricol[0] = lowrow[0] = h[0];
   }

   switch (n)                                     /* das Gleichungssystem loesen und damit  */
   {                                              /* die Splinekoeffizienten c[i] berechnen */
      case 2:                                     /* nur drei Stuetzstellen =>    */
         /* => Loesung direkt berechnen  */
         if (marg_cond == 4)                      /* periodischer Spline?         */
            c[1] = THREE * (y[0] - y[1]) / (x[2] - x[1]) / (x[1] - x[0]),
               c[2] = - c[1];
         else
            c[1] = c[0] / diag[0];
         break;

      default:                                    /* mehr als drei Stuetzstellen? */
         if (marg_cond == 4)                      /* periodischer Spline?         */
            fehler = tzdiag(n, lower, diag, upper, lowrow,
               ricol, c, !erster_aufruf);
         else                                     /* nichtperiodischer Spline? */
            fehler = trdiag(n - 1, lower, diag, upper, c, !erster_aufruf);

         if (fehler != 0)                         /* Fehler in tzdiag() oder in trdiag()? */
            ciao(fehler + 4);
         for (i = n; i != 0; i--)                 /* die Elemente des Loesungsvektors   */
            c[i] = c[i - 1];                      /* eine Position nach rechts schieben */
   }

   switch (marg_cond)                             /* in Abhaengigkeit von der Randbedingung den */
   {                                              /* ersten und letzten Wert von c korrigieren  */
      case 0:                                     /* not-a-knot-Bedingung?              */
         if (n == 2)                              /* nur drei Stuetzstellen?      */
            c[0] = c[2] = c[1];
         else                                     /* mehr als drei Stuetzstellen? */
            c[0] = c[1] + h[0] * (c[1] - c[2]) / h[1],
               c[n] = c[n - 1] + h[n - 1] *
               (c[n - 1] - c[n - 2]) / h[n - 2];
         break;

      case 1:                                     /* erste Randableitungen vorgegeben?  */
         c[0] =  (REAL)1.5 * ((y[1] - y[0]) / h[0] - marg_0);
         c[0] = (c[0] - c[1] * h[0] * HALF) / h[0];
         c[n] = (REAL)-1.5 * ((y[n] - y[n - 1]) / h[n - 1] - marg_n);
         c[n] = (c[n] - c[n - 1] * h[n - 1] * HALF) / h[n - 1];
         break;

      case 2:                                     /* zweite Randableitungen vorgegeben? */
         c[0] = marg_0 * HALF;
         c[n] = marg_n * HALF;
         break;

      case 3:                                     /* dritte Randableitungen vorgegeben? */
         c[0] = c[1]     - marg_0 * HALF * h[0];
         c[n] = c[n - 1] + marg_n * HALF * h[n - 1];
         break;

      case 4:                                     /* periodischer Spline?               */
         c[0] = c[n];

   }

   for (i = 0; i < n; i++)                        /* die restlichen      */
      b[i] = (y[i + 1] - y[i]) / h[i] - h[i] *    /* Splinekoeffizienten */
         (c[i + 1] + TWO * c[i]) / THREE,         /* b[i] und d[i]       */
         d[i] = (c[i + 1] - c[i]) / (THREE * h[i]);/* berechnen           */

   if (!save)                                     /* Hilfsfelder nicht aufbewahren */
      ciao(0);                                    /* (letzter Aufruf einer Folge)? */

   return 0;
#undef ciao
}
예제 #9
0
BOOL GetSnapRegs(HWND hDlg) //tfx 取配置文件信息
{
	int i;
	BYTE nFlag;

	lpSnapKey=MYALLOC(20);

	lpSnapRegs=MYALLOC0(sizeof(LPSTR)*MAXREGSHOT);
	lpSnapRegsStr=MYALLOC0(SIZEOF_REGSHOT);
	if(GetPrivateProfileSection(INI_SKIPREGKEY,lpSnapRegsStr,SIZEOF_REGSHOT,lpRegshotIni)>0)
	{
		for(i=0;i<MAXREGSHOT-1;i++)
		{
			sprintf(lpSnapKey,"%d%s",i,"=");
			if((lpSnapReturn=AtPos(lpSnapRegsStr,lpSnapKey,SIZEOF_REGSHOT))!=NULL)
			{
				*(lpSnapRegs+i)=(DWORD)lpSnapReturn;
				//dwSnapFiles++;
			}
			else
			{
				break;
			}
		}
	}

	lpSnapFiles=MYALLOC0(sizeof(LPSTR)*MAXREGSHOT);
	lpSnapFilesStr=MYALLOC0(SIZEOF_REGSHOT);
	if(GetPrivateProfileSection(INI_SKIPDIR,lpSnapFilesStr,SIZEOF_REGSHOT,lpRegshotIni))
	{
		for(i=0;i<MAXREGSHOT-1;i++)
		{
			sprintf(lpSnapKey,"%d%s",i,"=");
			if((lpSnapReturn=AtPos(lpSnapFilesStr,lpSnapKey,SIZEOF_REGSHOT))!=NULL)
			{
				*(lpSnapFiles+i)=(DWORD)lpSnapReturn;
				//dwSnapFiles++;
			}
			else
			{
				break;
			}
		}
	}

	nFlag=(BYTE)GetPrivateProfileInt(INI_SETUP,INI_FLAG,1,lpRegshotIni); //default from 0 to 1 in 1.8.2 (TEXT)
	//if(nFlag!=0)
	{
		SendMessage(GetDlgItem(hDlg,IDC_RADIO1),BM_SETCHECK,(WPARAM)(nFlag&0x01),(LPARAM)0);
		SendMessage(GetDlgItem(hDlg,IDC_RADIO2),BM_SETCHECK,(WPARAM)((nFlag&0x01)^0x01),(LPARAM)0);
		//SendMessage(GetDlgItem(hDlg,IDC_CHECKDIR),BM_SETCHECK,(WPARAM)((nFlag&0x04)>>1),(LPARAM)0); //1.7
		SendMessage(GetDlgItem(hDlg,IDC_CHECKDIR),BM_SETCHECK,(WPARAM)((nFlag&0x02)>>1),(LPARAM)0);
	}
	/*else  delete in 1.8.1
	{
		SendMessage(GetDlgItem(hDlg,IDC_RADIO1),BM_SETCHECK,(WPARAM)0x01,(LPARAM)0);
		SendMessage(GetDlgItem(hDlg,IDC_RADIO2),BM_SETCHECK,(WPARAM)0x00,(LPARAM)0);
		SendMessage(GetDlgItem(hDlg,IDC_CHECKDIR),BM_SETCHECK,(WPARAM)0x00,(LPARAM)0);
	}
	*/
	//added in 1.8.1 for compatible with undoreg1.46
	bUseLongRegHead=GetPrivateProfileInt(INI_SETUP,INI_USELONGREGHEAD,0,lpRegshotIni)!=0 ? TRUE:FALSE;

	if(GetPrivateProfileString(INI_SETUP,INI_EXTDIR,NULL,lpExtDir,MAX_PATH,lpRegshotIni)!=0)
		SetDlgItemText(hDlg,IDC_EDITDIR,lpExtDir);
	else
		SetDlgItemText(hDlg,IDC_EDITDIR,lpWindowsDirName);

	if(GetPrivateProfileString(INI_SETUP,INI_OUTDIR,NULL,lpOutputpath,MAX_PATH,lpRegshotIni)!=0)
		SetDlgItemText(hDlg,IDC_EDITPATH,lpOutputpath);
	else
		SetDlgItemText(hDlg,IDC_EDITPATH,lpTempPath);

	SendMessage(hDlg,WM_COMMAND,(WPARAM)IDC_CHECKDIR,(LPARAM)0);

	return TRUE;
}