1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /*                                                                           */
3 /*   File....: conname.c                                                     */
4 /*   Name....: Constraint Names                                              */
5 /*   Author..: Thorsten Koch                                                 */
6 /*   Copyright by Author, All rights reserved                                */
7 /*                                                                           */
8 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
9 /*
10  * Copyright (C) 2001-2018 by Thorsten Koch <koch@zib.de>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public License
14  * as published by the Free Software Foundation; either version 3
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 
32 #include <stdbool.h>
33 #include "zimpl/mshell.h"
34 #include "zimpl/ratlptypes.h"
35 #include "zimpl/numb.h"
36 #include "zimpl/mme.h"
37 #include "zimpl/bound.h"
38 #include "zimpl/mono.h"
39 #include "zimpl/term.h"
40 #include "zimpl/elem.h"
41 #include "zimpl/tuple.h"
42 #include "zimpl/local.h"
43 #include "zimpl/stmt.h"
44 #include "zimpl/prog.h"
45 #include "zimpl/xlpglue.h"
46 #include "zimpl/conname.h"
47 
48 static char*        cpfix  = NULL;
49 static int          count  = 1;
50 static char*        cname  = NULL;
51 static size_t       clen   = 0;
52 static ConNameForm  cform  = CON_FORM_NAME;
53 
conname_format(ConNameForm format)54 void conname_format(ConNameForm format)
55 {
56    cform = format;
57 }
58 
conname_free()59 void conname_free()
60 {
61    assert(cname != NULL);
62    assert(cpfix != NULL);
63 
64    free(cname);
65    free(cpfix);
66 
67    cname = NULL;
68    clen  = 0;
69 }
70 
71 /* return False if we are in mode CON_FORM_NAME and
72  * already a constraint with the prefix exists. Otherwise this
73  * is unimportant, because all constraints will get a unique
74  * number anyway.
75  */
conname_set(const char * prefix)76 bool conname_set(const char* prefix)
77 {
78    assert(prefix != NULL);
79    assert(cname  == NULL);
80 
81    cpfix = strdup(prefix);
82    clen  = strlen(cpfix) + 16;
83    cname = malloc(clen);
84 
85    assert(cname != NULL);
86 
87    if (cform != CON_FORM_NAME)
88       return true;
89 
90    assert(cform == CON_FORM_NAME);
91 
92    count = 1;
93 
94    strcpy(cname, cpfix);
95    strcat(cname, "_1");
96 
97    if (xlp_conname_exists(prog_get_lp(), cname))
98       return false;
99 
100    strcat(cname, "_a_0");
101 
102    return !xlp_conname_exists(prog_get_lp(), cname);
103 }
104 
conname_get()105 const char* conname_get()
106 {
107    char*  localstr;
108    size_t newlen;
109 
110    assert(cpfix != NULL);
111    assert(cname != NULL);
112 
113    switch(cform)
114    {
115    case CON_FORM_MAKE :
116       sprintf(cname, "c%d", count);
117       break;
118    case CON_FORM_NAME :
119       sprintf(cname, "%s_%d", cpfix, count);
120       break;
121    case CON_FORM_FULL :
122       localstr = local_tostrall();
123       newlen   = strlen(localstr) + strlen(cpfix) + 16;
124 
125       if (newlen > clen)
126       {
127          clen  = newlen;
128          cname = realloc(cname, clen);
129 
130          assert(cname != NULL);
131       }
132       sprintf(cname, "%s_%s%s",
133          cpfix,
134          strlen(localstr) > 0 ? ";" : "",
135          localstr);
136 
137       free(localstr);
138       break;
139    }
140    assert(strlen(cname) < clen);
141 
142    return cname;
143 }
144 
conname_next()145 void conname_next()
146 {
147    count++;
148 }
149 
150