-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormulaAddInShiftChars.cpp
160 lines (154 loc) · 6.76 KB
/
FormulaAddInShiftChars.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// FormulaAddInSample.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "FormulaAddIn.h"
#include <string>
//The resulting dll and accompanying XML file need to be installed into RunTimeData/FormulaAddIns to function
void SetString(FormulaAddInData *pReturnValue, const wchar_t *pString)
{
size_t nLen = wcslen(pString);
wchar_t *pStringRet = (wchar_t *)GlobalAlloc(GMEM_FIXED, (nLen+1)*sizeof(wchar_t));
wcscpy(pStringRet, pString);
pReturnValue->pVal = pStringRet;
pReturnValue->nVarType = 2;
}
// this takes a string and a "bit shift count"
// every character in the string, it will be shifted up by bitshiftcount amount
extern "C" long _declspec(dllexport) _stdcall ShiftChars(int nNumArgs, FormulaAddInData *pArgs, FormulaAddInData *pReturnValue)
{
uint64_t shiftCount = (uint64_t)(pArgs[1].dVal);
std::wstring strRet;
for (int x = 0; x < wcslen(pArgs[0].pVal); ++x) {
strRet += (wchar_t)((uint64_t)(pArgs[0].pVal[x]) + shiftCount);
}
SetString(pReturnValue, strRet.c_str());
return 1;
}
// this takes a string and a "bit shift count"
// for every numeric character in the string:
// it will be shifted up by bitshiftcount amount (mod 10: wrapping from 9 to 0)
// for all non-numeric, the chracter will be left unchanged
extern "C" long _declspec(dllexport) _stdcall ShiftCharsNumeric(int nNumArgs, FormulaAddInData *pArgs, FormulaAddInData *pReturnValue)
{
uint64_t shiftCount = (uint64_t)(pArgs[1].dVal);
uint64_t oldChar, newChar;
std::wstring strRet;
for (int x = 0; x < wcslen(pArgs[0].pVal); ++x) {
oldChar = (uint64_t)(pArgs[0].pVal[x]);
if (oldChar >= (uint64_t)(((wchar_t *)L"0")[0]) &&
oldChar <= (uint64_t)(((wchar_t *)L"9")[0])) {
// determine potential shift amount
newChar = oldChar + shiftCount;
// adjust shift if it will put us off the end
while (newChar >(uint64_t)(((wchar_t *)L"9")[0])) {
newChar -= 10;
}
while (newChar < (uint64_t)(((wchar_t *)L"0")[0])) {
newChar += 10;
}
// perform the shift and add to output string
strRet += (wchar_t)newChar;
} else {
// grab next character unchanged
strRet += pArgs[0].pVal[x];
}
}
SetString(pReturnValue, strRet.c_str());
return 1;
}
// this takes a string and a "bit shift count"
// for every alpha character in the string:
// it will be shifted up by bitshiftcount amount (mod 26: wrapping from Z to A or z to a)
// for all non alpha, the chracter will be left unchanged
extern "C" long _declspec(dllexport) _stdcall ShiftCharsAlpha(int nNumArgs, FormulaAddInData *pArgs, FormulaAddInData *pReturnValue)
{
uint64_t shiftCount = (uint64_t)(pArgs[1].dVal);
uint64_t oldChar, newChar;
std::wstring strRet;
for (int x = 0; x < wcslen(pArgs[0].pVal); ++x) {
oldChar = (uint64_t)(pArgs[0].pVal[x]);
if (oldChar >= (uint64_t)(((wchar_t *)L"A")[0]) &&
oldChar <= (uint64_t)(((wchar_t *)L"Z")[0])) {
// determine potential shift amount
newChar = oldChar + shiftCount;
// adjust shift if it will put us off the end
while (newChar >(uint64_t)(((wchar_t *)L"Z")[0])) {
newChar -= 26;
}
while (newChar < (uint64_t)(((wchar_t *)L"A")[0])) {
newChar += 26;
}
// perform the shift and add to output string
strRet += (wchar_t)newChar;
} else if (oldChar >= (uint64_t)(((wchar_t *)L"a")[0]) &&
oldChar <= (uint64_t)(((wchar_t *)L"z")[0])) {
// determine potential shift amount
newChar = oldChar + shiftCount;
// adjust shift if it will put us off the end
while (newChar >(uint64_t)(((wchar_t *)L"z")[0])) {
newChar -= 26;
}
while (newChar < (uint64_t)(((wchar_t *)L"a")[0])) {
newChar += 26;
}
// perform the shift and add to output string
strRet += (wchar_t)newChar;
} else {
// grab next character unchanged
strRet += pArgs[0].pVal[x];
}
}
SetString(pReturnValue, strRet.c_str());
return 1;
}
// this takes a string and a "bit shift count"
// for every numeric character in the string:
// it will be shifted up by bitshiftcount amount (mod 10: wrapping from 9 to 0)
// for every alpha character in the string:
// it will be shifted up by bitshiftcount amount (mod 26: wrapping from Z to A or z to a)
// for all non alpha, non-numeric, the chracter will be left unchanged
extern "C" long _declspec(dllexport) _stdcall ShiftCharsAlphaNum(int nNumArgs, FormulaAddInData *pArgs, FormulaAddInData *pReturnValue)
{
uint64_t shiftCount = (uint64_t)(pArgs[1].dVal);
uint64_t oldChar, newChar;
std::wstring strRet;
for (int x = 0; x < wcslen(pArgs[0].pVal); ++x) {
oldChar = (uint64_t)(pArgs[0].pVal[x]);
if (oldChar >= (uint64_t)(((wchar_t *)L"A")[0]) && oldChar <= (uint64_t)(((wchar_t *)L"Z")[0])) {
// determine potential shift amount
newChar = oldChar + shiftCount;
// adjust shift if it will put us off the end
while (newChar >(uint64_t)(((wchar_t *)L"Z")[0])) {
newChar -= 26;
}
while (newChar < (uint64_t)(((wchar_t *)L"A")[0])) {
newChar += 26; // in case they shifted a negative amount off the front...
}
// add shifted char to output string
strRet += (wchar_t)newChar;
} else if (oldChar >= (uint64_t)(((wchar_t *)L"a")[0]) && oldChar <= (uint64_t)(((wchar_t *)L"z")[0])) {
newChar = oldChar + shiftCount;
while (newChar >(uint64_t)(((wchar_t *)L"z")[0])) {
newChar -= 26;
}
while (newChar < (uint64_t)(((wchar_t *)L"a")[0])) {
newChar += 26;
}
strRet += (wchar_t)newChar;
} else if (oldChar >= (uint64_t)(((wchar_t *)L"0")[0]) && oldChar <= (uint64_t)(((wchar_t *)L"9")[0])) {
newChar = oldChar + shiftCount;
while (newChar > (uint64_t)(((wchar_t *)L"9")[0])) {
newChar -= 10;
}
while (newChar < (uint64_t)(((wchar_t *)L"0")[0])) {
newChar += 10;
}
strRet += (wchar_t)newChar;
} else {
// grab next character unchanged
strRet += pArgs[0].pVal[x];
}
}
SetString(pReturnValue, strRet.c_str());
return 1;
}