summaryrefslogtreecommitdiff
path: root/support/nlatexdb/nlatexdb/SqlQueryVar.cs
blob: 6c4c88db3b2e1842899930b989aa91c0a0deb795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//    nlatexdb Version 0.03
//    Database Access in LaTeX
//    Copyright (C) 2011 Robin Höns, Integranova GmbH
//
//    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/>.
//
//    For more information see the web page http://hoens.net/robin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nlatexdb
{
    class SqlQueryVar
    {
        public SqlQueryVar(string key, object value)
        {
            m_varname = key;
            m_regexreplace = new List<string>();
            setValue(value);
        }
            
        public SqlQueryVar(string vartext)
        {
            string[] varsplit = vartext.Split(m_regexsplitter);
            m_varname = varsplit[0];
            m_regexreplace = new List<string>(varsplit);
            m_regexreplace.RemoveAt(0);
        }

        public void setValue(object value)
        {
            m_value = value;
            string valuestring = objectToLatex(m_value);
            for (int i = 1; i < m_regexreplace.Count; i += 2)
            {
                string search = m_regexreplace[i - 1];
                string replace = m_regexreplace[i];
				Processer.Debug("Search: {0} Replace: {1}", search, replace);
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(search);
                valuestring = regex.Replace(valuestring, replace);
                // so im groben
            }
            m_valuelatex = valuestring;
        }

        private static string objectToLatex(object o)
        {
            string s = o.ToString();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
				if (m_latexreplace.ContainsKey(s[i]))
				{
					sb.Append(m_latexreplace[s[i]]);
				}
				else
				{
					sb.Append(s[i]);
				}
            }
            return sb.ToString();
        }

        public string Name
        {
            get
            {
                return m_varname;
            }
        }

        public string getValueLatex()
        {
            return m_valuelatex;
        }

        public object getValueSql()
        {
            return m_value;
        }
		
		public static void ClearLatexReplace()
		{
			if (m_latexreplace == null)
			{
				m_latexreplace = new Dictionary<char, string>();
			}
			m_latexreplace.Clear();
		}
		
		public static void AddLatexReplace(char latexchar, string replace)
		{
			m_latexreplace[latexchar] = replace;
		}
		
		public static void SetRegexSplitter(string regex_splitter)
		{
			m_regexsplitter = regex_splitter.ToCharArray();
		}
		

        private string m_varname;
        private object m_value;
        private string m_valuelatex;
        private List<string> m_regexreplace;
		private static char[] m_regexsplitter = { '/' };
		
		private static Dictionary<char, string> m_latexreplace;
    }
}