[Pntool-developers] SF.net SVN: pntool:[207] spnbox
Brought to you by:
compaqdrew,
miordache
From: <Ste...@us...> - 2009-07-27 01:48:07
|
Revision: 207 http://pntool.svn.sourceforge.net/pntool/?rev=207&view=rev Author: StephenCamp Date: 2009-07-27 01:47:57 +0000 (Mon, 27 Jul 2009) Log Message: ----------- Actually adding fvpr.c and the test files this time... Added Paths: ----------- spnbox/fvpr.c spnbox/tests/test-fvpr.c spnbox/tests/test-fvpr.txt Added: spnbox/fvpr.c =================================================================== --- spnbox/fvpr.c (rev 0) +++ spnbox/fvpr.c 2009-07-27 01:47:57 UTC (rev 207) @@ -0,0 +1,148 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "../pnheaders/general.h" +#include "../pnheaders/matrix.h" +#include "spnbox.h" + +static char* buffer = 0; +static int bufferLength = 0; + +static int GetBufferLength(void* vector, int length, int mode, char* op, char* cl); +static int GetVectorEl(void* vector, int i, int mode); +static int CheckParams(void* vector, int length, int mode, int option); + +char* fvpr(void* vector, int length, int mode, int option) +{ + char *op, *cl; + char element[64]; + + /*If the vector is a matrix, get the length.*/ + if (mode == DISPLAY_MATRIX) length = MatrixLength(*((matrix*)vector)); + + /*Check the parameters.*/ + if (! CheckParams(vector, length, mode, option)) return 0; + + /*Get the characters that will (maybe) be printed to begin and end the + vector.*/ + switch(option) + { + case 0: + op = ""; + cl = ""; + break; + case 1: + if (length > 1) + { + op = "["; + cl = "]"; + } + else + { + op = ""; + cl = ""; + } + break; + case 2: + op = "{"; + cl = "}"; + break; + } + + /*Get the length of buffer that will be needed. Do not reallocate space unless + we need to.*/ + int i, newBufferLength = GetBufferLength(vector, length, mode, op, cl); + if (newBufferLength > bufferLength) + { + free(buffer); + bufferLength = newBufferLength; + buffer = calloc(bufferLength, sizeof(char)); + } + buffer[0] = '\0'; + if (! length) return buffer; + + /*Do the printing.*/ + strcpy(buffer, op); + for (i = 0; i < length; i++) + { + int c = GetVectorEl(vector, i, mode); + sprintf(element, i ? ", %d" : "%d", c); + strcat(buffer, element); + } + strcat(buffer, cl); + if (mode == DISPLAY_MATRIX && NumberOfRows(*((matrix*)vector)) > 1) + { + strcat(buffer, "'"); + } + return buffer; +} + +int GetBufferLength(void* vector, int length, int mode, char* op, char* cl) +{ + if (length == 0) return 1; + /*The buffer length will be, at most, the vector length times the length of + the element separator plus the number of digits required to display the + largest number in the vector, all plus the lengths of the beginning and ending + separators.*/ + /*To get the number of digits, find the maximum and then do a simple logarithm.*/ + int max = 1, digits = 1, i; + for (i = 0; i < length; i++) + { + int c = GetVectorEl(vector, i, mode); + if (c > max) max = c; + if (-c > max) max = -c; + } + /*Perform a simple logarithm. The number of digits begins at 1, not zero, + because we are leaving space for a minus sign.*/ + + while(max) + { + max /= 10; + digits++; + } + + return strlen(op) + strlen(cl) + ((strlen(", ") + digits) * length) + 1 + (mode == DISPLAY_MATRIX) ? 1 : 0; +} + +int GetVectorEl(void* vector, int i, int mode) +{ + switch(mode) + { + case DISPLAY_MATRIX: + return GetMatrixEl((matrix*) vector, i % NumberOfRows(* ((matrix*) vector)), i / NumberOfRows(* ((matrix*) vector))); + break; + case DISPLAY_INTS: + return ((int*)vector)[i]; + break; + } +} + +int CheckParams(void* vector, int length, int mode, int option) +{ + /*vector and destination are both required.*/ + if (! vector) + { + merror(0, "FVPR: A required pointer was null"); + return 0; + } + /*length cannot be non-positive.*/ + if (length < 1) + { + merror(0, "FVPR: Vector length is non-positive"); + return 0; + } + /*If the chr is 0, fill it with the default, m.*/ + if (mode != DISPLAY_MATRIX && mode !=DISPLAY_INTS) + { + merror(0, "FVPR: Invalid mode"); + return 0; + } + if (option < 0 || option > 2) + { + merror(0, "FVPR: Invalid option"); + return 0; + } + return 1; +} + + \ No newline at end of file Added: spnbox/tests/test-fvpr.c =================================================================== --- spnbox/tests/test-fvpr.c (rev 0) +++ spnbox/tests/test-fvpr.c 2009-07-27 01:47:57 UTC (rev 207) @@ -0,0 +1,52 @@ +#include "test.h" + +int main(int argc, char* argv[]) +{ + char iDesc[] = "arrayi V, matrix M, int option"; + char *result; + int* vector = 0; + int vLength = 0; + int option = 1; + int *iFill; + matrix M; + + FILE* file; + + if (! (file = ParseCmdLine(argc, argv))) + { + return 0; + } + + MemoryManager mem = CreateMemoryManager(3, 1, 0, 0); + + while (ParseStructure(file, iDesc, &iFill, &mem, &vector, &vLength, &M, &option)) + { + if (iFill[0]) iFill[1] = 0; + DisplayStructure(iDesc, iFill, vector, vLength, &M, option); + + if (iFill[0]) + { + result = fvpr(vector, vLength, DISPLAY_INTS, option); + } + else if (iFill[1]) + { + result = fvpr(&M, 0, DISPLAY_MATRIX, option); + } + else + { + result = " No vector or matrix provided!"; + } + + printf("Result:\n%s\n", result); + + FreeMemory(&mem); + mem = CreateMemoryManager(3, 0, 0, 0); + + vector = 0; + vLength = 0; + option = 1; + + printf("-------------------------------------------------------------------------------\n"); + } + return 0; +} Added: spnbox/tests/test-fvpr.txt =================================================================== --- spnbox/tests/test-fvpr.txt (rev 0) +++ spnbox/tests/test-fvpr.txt 2009-07-27 01:47:57 UTC (rev 207) @@ -0,0 +1,35 @@ +echo Problem 1. +echo Solution should be: +echo [-100, -100, -100] +V 3 +-100 -100 -100 +done + +echo Problem 2. +echo Solution should be: +echo 1 +V 1 1 +done + +echo Problem 3. +echo Solution should be: +echo 1 +M 1 1 1 +done + +echo Problem 4. +echo Solution should be: +echo [1, 4, 2, 5, 3, 6]' +M 2 3 +1 2 3 +4 5 6 +done + +echo Problem 5. +echo Solution should be: +echo {1, 2, 3, 4, 5, 6} +M 1 6 +1 2 3 4 5 6 +option 2 +done +quit \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |