1 /*******************************************************************************
2  * Copyright (c) 2013-2017, Andrés Martinelli <andmarti@gmail.com              *
3  * All rights reserved.                                                        *
4  *                                                                             *
5  * This file is a part of SC-IM                                                *
6  *                                                                             *
7  * SC-IM is a spreadsheet program that is based on SC. The original authors    *
8  * of SC are James Gosling and Mark Weiser, and mods were later added by       *
9  * Chuck Martin.                                                               *
10  *                                                                             *
11  * Redistribution and use in source and binary forms, with or without          *
12  * modification, are permitted provided that the following conditions are met: *
13  * 1. Redistributions of source code must retain the above copyright           *
14  *    notice, this list of conditions and the following disclaimer.            *
15  * 2. Redistributions in binary form must reproduce the above copyright        *
16  *    notice, this list of conditions and the following disclaimer in the      *
17  *    documentation and/or other materials provided with the distribution.     *
18  * 3. All advertising materials mentioning features or use of this software    *
19  *    must display the following acknowledgement:                              *
20  *    This product includes software developed by Andrés Martinelli            *
21  *    <andmarti@gmail.com>.                                                    *
22  * 4. Neither the name of the Andrés Martinelli nor the                        *
23  *   names of other contributors may be used to endorse or promote products    *
24  *   derived from this software without specific prior written permission.     *
25  *                                                                             *
26  * THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY            *
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      *
29  * DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY           *
30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  *
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       *
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
36  *******************************************************************************/
37 
38 /**
39  * \file extra.c
40  * \author Andrés Martinelli <andmarti@gmail.com>
41  * \date 2017-07-18
42  * \brief TODO Write a brief file description.
43  */
44 
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 #include <curses.h>
49 #include "extra.h"
50 #include "../sc.h"
51 #include "../macros.h"
52 //#include "../range.h"
53 
54 extern int find_range(char * name, int len, struct ent * lmatch, struct ent * rmatch, struct range ** rng);
55 
56 #define freen(x) nofreeNULL(x)
nofreeNULL(void * x)57 void nofreeNULL(void *x) {
58     if (x != NULL)
59         free(x);
60     return;
61 }
62 
63 /**
64  * \brief Returns the ROW/COL cell name
65  *
66  * \param[in] row
67  * \param[in] col
68  *
69  * \return cell name (e.g. 'D4')
70  */
71 
v_name(int row,int col)72 char * v_name(int row, int col) {
73     struct ent *v;
74     struct range *r;
75     static char buf[20];
76 
77     v = lookat(row, col);
78     if ( ! find_range((char *) 0, 0, v, v, &r) ) {
79         return (r->r_name);
80     } else {
81         (void) sprintf(buf, "%s%d", coltoa(col), row);
82         return (buf);
83     }
84 }
85 
86 /**
87  * \brief Parse BUF_IN to get a cell name. Skip first blocks with IGNORE_FIRST_BLOCKS
88  *
89  * \param[in] ignore_first_blocks
90  * \param[in] buf_in
91  *
92  * \return cell name
93  */
94 
parse_cell_name(int ignore_first_blocks,struct block * buf_in)95 char * parse_cell_name(int ignore_first_blocks, struct block * buf_in) {
96     struct block * b = buf_in;
97     static char cell_name[3]; //length of max col is 3 (ZZZ)
98     cell_name[0] = '\0';
99 
100     while (ignore_first_blocks--) b = b->pnext;
101     while( b != NULL) {
102           (void) sprintf(cell_name + strlen(cell_name), "%c", b->value);
103           b = b->pnext;
104     }
105     return cell_name;
106 }
107