Mercurial > forge
changeset 12428:760249bf866e octave-forge
Use binary modules by MarkusB for colnum<->colstr conversion
author | prnienhuis |
---|---|
date | Sun, 13 Apr 2014 21:34:28 +0000 |
parents | a363fb87a236 |
children | 358647dcea1d |
files | main/io/src/__char2num__.c main/io/src/__num2char__.c |
diffstat | 2 files changed, 147 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/io/src/__char2num__.c Sun Apr 13 21:34:28 2014 +0000 @@ -0,0 +1,72 @@ +// Copyright (C) 2014 Markus Bergholz <markuman@gmail.com> +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, see <http://www.gnu.org/licenses/>. + +#include "mex.h" +#include <stdio.h> +#include <math.h> +#include <stdlib.h> +#include <limits.h> +#include <string.h> +#include <ctype.h> + +int column_index_from_string(const char *s) +{ + int ret=0; + for (; *s; s++) + { + if (!isupper(*s)) + return -1; + ret = ret*26 + *s-'A' +1; + if (ret<0) //overflow + return -1; + } + return ret; +} + +void mexFunction( int nlhs, mxArray *plhs[], + int nrhs, const mxArray *prhs[]) +{ + if(nrhs!=1) + { + mexErrMsgIdAndTxt("IO:arrayProduct:nrhs", "Only one input allowed."); + } + else if ( !mxIsChar(prhs[0])) + { + mexErrMsgIdAndTxt("IO:arrayProduct:nrhs", "Input must be a string."); + } + else + { + char *str; + str = (char *) mxCalloc(mxGetN(prhs[0])+1, sizeof(char)); + mxGetString(prhs[0], str, mxGetN(prhs[0])+1); + if (strlen(str)>3) + { + mexErrMsgIdAndTxt("IO:arrayProduct:nrhs", "Input string is too long."); + } + else + { + int i = column_index_from_string(str); + if (i<0) + mexErrMsgIdAndTxt("IO:arrayProduct:nrhs", "Illegal characters."); + else + { + plhs[0] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL); + double *index; + index = mxGetPr(plhs[0]); + index[0] = i; + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/io/src/__num2char__.c Sun Apr 13 21:34:28 2014 +0000 @@ -0,0 +1,75 @@ +// Copyright (C) 2014 Markus Bergholz <markuman@gmail.com> +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 3 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, see <http://www.gnu.org/licenses/>. + +#include "mex.h" +#include <stdio.h> +#include <math.h> +#include <stdlib.h> +#include <limits.h> +#include <string.h> + + +#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + +static int map[256]; + +void init_map(void) +{ + unsigned i; + for (i = 0; ALPHABET[i]; i++) { + map[(unsigned char)ALPHABET[i]] = i + 1; + } +} + +char *column_index_to_string(int c, char *s) +{ + int i, x; + if (c < 1 || c > 18278) { + return NULL; + } + + for (i = 0; (c) && i < 3; i++) { + c -= 1; + x = c % 26; + s[2 - i] = ALPHABET[x]; + c /= 26; + } + memmove(s, s + 3 - i , i); + s[i] = '\0'; + return s; +} + +void mexFunction( int nlhs, mxArray *plhs[], + int nrhs, const mxArray *prhs[]) +{ + + + if(nrhs!=1) { + mexErrMsgIdAndTxt("IO:arrayProduct:nrhs", "One INPUT argument, no more nor less!"); + } else if ( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) { + mexErrMsgIdAndTxt("IO:arrayProduct:nrhs", "INPUT argument has to be double and no complex number!"); + } else { + + double *raw; + int *index; + raw = mxGetPr (prhs[0]); + index[0]=raw[0]; + + char str[4]; + plhs[0]=mxCreateString(column_index_to_string(index[0], str)); + + } + +}