1 /*
2  * CFixedText.cc
3  *
4  * Copyright 2014-2018 D. Mitch Bailey  cvc at shuharisystem dot com
5  *
6  * This file is part of cvc.
7  *
8  * cvc is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * cvc is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with cvc.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * You can download cvc from https://github.com/d-m-bailey/cvc.git
22  */
23 
24 #include "CFixedText.hh"
25 
26 #include "obstack.h"
27 
28 
CFixedText()29 CFixedText::CFixedText() {
30 	const char myDummy[] = "";
31 	obstack_init(&fixedTextObstack);
32 	firstAddress = (text_t) obstack_alloc(&fixedTextObstack, 0);
33 	obstack_copy0(&(fixedTextObstack), (void *) myDummy, 0);  // first string is null string
34 	size = 1;
35 }
36 
~CFixedText()37 CFixedText::~CFixedText() {
38 	obstack_free(&fixedTextObstack, NULL);
39 	//obstack_init(&fixedTextObstack);
40 }
41 
Clear()42 void CFixedText::Clear() {
43 	const char myDummy[] = "";
44 	obstack_free(&fixedTextObstack, firstAddress);
45 	fixedTextToAddressMap.clear();
46 	obstack_copy0(&(fixedTextObstack), (void *) myDummy, 0);  // first string is null string
47 	size = 1;
48 }
49 
SetTextAddress(const text_t theNewText)50 text_t CFixedText::SetTextAddress(const text_t theNewText){
51 	text_t myTextAddress;
52 	//static string myKeyText;
53 	string myKeyText;
54 
55 	// TODO: try with count for speed (2 hash lookups vs throwing error)
56 	try {
57 		myKeyText = theNewText;
58 		myTextAddress = fixedTextToAddressMap.at(myKeyText);
59 	}
60 	catch (const out_of_range& oor_exception) {
61 		int myNewSize = int(myKeyText.length());
62 		myTextAddress = (text_t) obstack_copy0(&(fixedTextObstack), (void *) myKeyText.c_str(), myNewSize);
63 		size += myNewSize + 1;
64 		fixedTextToAddressMap[myKeyText] = myTextAddress;
65 	}
66 	return(myTextAddress);
67 }
68 
SetTextAddress(const string theType,CTextList * theNewTextList)69 text_t CFixedText::SetTextAddress(const string theType,	CTextList* theNewTextList) {
70 	text_t myTextAddress;
71 	string myKeyText;
72 	myKeyText = theType;
73 	for (CTextList::iterator text_pit = theNewTextList->begin(); text_pit != theNewTextList->end(); text_pit++) {
74 		myKeyText += " ";
75 		myKeyText += *text_pit;
76 	}
77 	try {
78 		myTextAddress = fixedTextToAddressMap.at(myKeyText);
79 	}
80 	catch (const out_of_range& oor_exception) {
81 		int myNewSize = int(myKeyText.length());
82 		myTextAddress = (text_t) obstack_copy0(&fixedTextObstack, (void *) myKeyText.c_str(), myNewSize);
83 		size += myNewSize + 1;
84 		fixedTextToAddressMap[myKeyText] = myTextAddress;
85 	}
86 	return (myTextAddress);
87 }
88 
GetTextAddress(const text_t theNewText)89 text_t CFixedText::GetTextAddress(const text_t theNewText) {
90 	static string myKeyText;
91 
92 //	send all exceptions to caller
93 	myKeyText = theNewText;
94 	return(fixedTextToAddressMap.at(myKeyText));
95 }
96 
GetTextAddress(const string theKeyText)97 text_t CFixedText::GetTextAddress(const string theKeyText){
98 	//	send all exceptions to caller
99 	return(fixedTextToAddressMap.at(theKeyText));
100 }
101 
BiasNet(CFixedText & theCdlText)102 text_t CTextList::BiasNet(CFixedText & theCdlText) {
103 	for ( auto text_it = begin(); text_it != end(); text_it++ ) {
104 		if ( strncmp(*text_it, "$SUB=", 4) == 0 ) {
105 			text_t myBiasNet = *text_it + 5;
106 			erase(text_it);
107 			return (theCdlText.SetTextAddress(myBiasNet));
108 		}
109 	}
110 	return (0);
111 }
112 
113