C-CS PortWizard
C:/Users/Brad/Documents/School Assignments/Year 2/Semester 2 v2/Software Quality 2/Assignment 3/ccsportwizard/CCSPortWizard/CCSPortWizard/Transposer.cs
1 
8 using System.Text.RegularExpressions;
9 
10 namespace CCSPortWizard
11 {
12  internal class Transposer
13  {
14  private const int HEADER_MAX = 100;
15  private const int SYNTAX_MAX = 100;
16  private string[,] SyntaxTable;
17  private string CSNameSpace = "";
18 
19  public Transposer()
20  {
21  }
22 
29  public Transposer(string CSNameSpace)
30  {
31  this.CSNameSpace = CSNameSpace;
32 
33  SyntaxTable = new string[Transposer.HEADER_MAX, Transposer.SYNTAX_MAX];
34  SyntaxTable[0, 0] = "generic";
35  SyntaxTable[0, 1] = "main";
36  SyntaxTable[0, 2] = "#include";
37  SyntaxTable[0, 3] = "int";
38  SyntaxTable[0, 4] = "float";
39  SyntaxTable[0, 5] = "double";
40  SyntaxTable[0, 6] = "char";
41  SyntaxTable[0, 7] = "void";
42  SyntaxTable[0, 8] = "NULL";
43  }
44 
51  private int SyntaxTableLastIndex(int IndexA, int IndexB)
52  {
53  int i = 0;
54 
55  switch (IndexA)
56  {
60  case 1:
61  while (SyntaxTable[i, 0] != null)
62  {
63  ++i;
64  if (i >= Transposer.HEADER_MAX) return -1;
65  }
66  break;
67 
71  case 2:
72  while (SyntaxTable[IndexB, i] != null)
73  {
74  ++i;
75  if (i >= Transposer.SYNTAX_MAX) return -1;
76  }
77  break;
78  }
79  return i - 1;
80  }
81 
88  private void Transpose_Generic_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
89  {
90  switch (Mode)
91  {
95  case 'r':
96 
97  int fileIndex = SyntaxTableLastIndex(1, -1) + 1;
98 
99  SyntaxTable[fileIndex, 0] = "generic";
100  SyntaxTable[fileIndex, 1] = "main";
101  SyntaxTable[fileIndex, 2] = "#include";
102  SyntaxTable[fileIndex, 3] = "int";
103  SyntaxTable[fileIndex, 4] = "float";
104  SyntaxTable[fileIndex, 5] = "double";
105  SyntaxTable[fileIndex, 6] = "char";
106  SyntaxTable[fileIndex, 7] = "void";
107  SyntaxTable[fileIndex, 8] = "NULL";
108  break;
109 
113  case 'c':
114 
115  // check which keyword is found in a line
116  switch (SyntaxMatchNumber)
117  {
121  case 1:
122 
123  string SyntaxPattern = @"(^|[^\w])([a-zA-Z]+)(\s+)(main)(.*)";
124  string Syntax_Main = "";
125  if (Regex.IsMatch(codeLine, SyntaxPattern))
126  {
127  Syntax_Main = Regex.Replace(codeLine, SyntaxPattern, main => "\t\tstatic " + main.Groups[1].Value +
128  main.Groups[2].Value + " Main(string[] args)");
129  }
130 
131  codeLine = "namespace " + CSNameSpace + System.Environment.NewLine;
132  codeLine += "{" + System.Environment.NewLine;
133  codeLine += "\tclass Program" + System.Environment.NewLine;
134  codeLine += "\t{" + System.Environment.NewLine;
135  codeLine += Syntax_Main;
136 
137  break;
138 
142  case 2:
143 
144  Match match = Regex.Match(codeLine, @"(^|[^\w])<stdio.h>([^\w]|$)");
145  if (match.Success)
146  {
147  Transpose_Studio_Syntax('r', ref codeLine, 0);
148 
149  codeLine = @"using System;" + System.Environment.NewLine;
150  codeLine += @"using System.Collections.Generic;" + System.Environment.NewLine;
151  codeLine += @"using System.Text;";
152  }
153 
157 
158  match = Regex.Match(codeLine, @"(^|[^\w])<stdlib.h>([^\w]|$)");
159  if (match.Success)
160  {
161  Transpose_Stdlib_Syntax('r', ref codeLine, 0);
162  codeLine = "";
163  }
164 
168 
169  match = Regex.Match(codeLine, @"(^|[^\w])<string.h>([^\w]|$)");
170  if (match.Success)
171  {
172  Transpose_String_Syntax('r', ref codeLine, 0);
173  codeLine = "";
174  }
175 
176  break;
177 
181  case 6:
182 
183  string patternCharArray = @"(^|[^\w])(char)(\s+)(\w+)(\[\d+\])";
184  if (Regex.IsMatch(codeLine, patternCharArray))
185  {
186  codeLine = Regex.Replace(codeLine, patternCharArray, main => main.Groups[1].Value + "string" + main.Groups[3].Value + main.Groups[4].Value);
187  }
188  break;
189 
193  case 8:
194 
195  string patternNull = @"(^|[^\w])(NULL)([^\w]|$)";
196  if (Regex.IsMatch(codeLine, patternNull))
197  {
198  codeLine = Regex.Replace(codeLine, patternNull, main => main.Groups[1].Value + "null" + main.Groups[3].Value);
199  }
200  break;
201  }
202 
203  break;
204  }
205  }
206 
213  private void Transpose_Studio_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
214  {
215  switch (Mode)
216  {
220  case 'r':
221 
222  int fileIndex = SyntaxTableLastIndex(1, -1) + 1;
223 
224  this.SyntaxTable[fileIndex, 0] = "stdio.h";
225  this.SyntaxTable[fileIndex, 1] = "printf";
226  this.SyntaxTable[fileIndex, 2] = "gets";
227  this.SyntaxTable[fileIndex, 3] = "FILE";
228  this.SyntaxTable[fileIndex, 4] = "fopen";
229  this.SyntaxTable[fileIndex, 5] = "fgets";
230  this.SyntaxTable[fileIndex, 6] = "fclose";
231  this.SyntaxTable[fileIndex, 7] = "fprintf";
232  break;
233 
237  case 'c':
238 
239  switch (SyntaxMatchNumber)
240  {
244  case 1:
245 
246  string patternPrintf = @"(^|[^\w])(printf)([^\w])";
247  Regex rNewLine = new Regex(@"(\\n)(\s*"")");
248 
252  if (rNewLine.IsMatch(codeLine))
253  {
254  codeLine = Regex.Replace(codeLine, patternPrintf, m => m.Groups[1].Value + @"System.Console.WriteLine" + m.Groups[3].Value);
255  codeLine = rNewLine.Replace(codeLine, m => "" + m.Groups[2].Value);
256  }
260  else
261  {
262  codeLine = Regex.Replace(codeLine, patternPrintf, m => m.Groups[1].Value + @"System.Console.Write" + m.Groups[3].Value);
263  }
264 
265  string patternFormat = @"(""[^""^%]*)(%(\d)*([dDfFsSuUcC]))";
266  Match matchFormat = Regex.Match(codeLine, patternFormat);
267 
268  int i = 0;
269  while (matchFormat.Success)
270  {
271  if (matchFormat.Groups[3].Success)
272  codeLine = Regex.Replace(codeLine, patternFormat, m => m.Groups[1].Value + "{" + i++ +
273  "," + matchFormat.Groups[3].Value + ":" + matchFormat.Groups[4].Value + "}");
274  else
275  codeLine = Regex.Replace(codeLine, patternFormat, m => m.Groups[1].Value + "{" + i++ +
276  ":" + matchFormat.Groups[4].Value + "}");
277  matchFormat = Regex.Match(codeLine, patternFormat);
278  }
279 
280  break;
281 
285  case 2:
286 
287  string patternGets = @"(^|[^\w])(gets)(\s*)(\()(\w+)(\))";
288  if (Regex.IsMatch(codeLine, patternGets))
289  {
290  codeLine = Regex.Replace(codeLine, patternGets, m => m.Groups[1].Value + m.Groups[5].Value + " = " +
291  @"System.Console.ReadLine()");
292  }
293 
294  break;
295 
299  case 3:
300 
301  string patternFopen = @"(^|[^\w])(fopen)([^\w])";
302  if (Regex.IsMatch(codeLine, patternFopen))
303  {
304  string patternFile = @"(^|[^\w])(FILE)([^\w])";
305  codeLine = Regex.Replace(codeLine, patternFile, m => "");
306  }
307  else
308  {
309  codeLine = "";
310  }
311 
312  break;
313 
317  case 4:
318 
319  string patternFopen1 = @"(^|[^\w])(\w+)(\s*=\s*)(fopen)(\s*\([^,]*)(,\s*"")([a-zA-Z]+)(""\s*\))";
320  Match match = Regex.Match(codeLine, patternFopen1);
321  if (match.Success)
322  {
326  if (match.Groups[7].Value == "w")
327  {
328  codeLine = Regex.Replace(codeLine, patternFopen1, m => m.Groups[1].Value + "System.IO.StreamWriter " + m.Groups[2].Value +
329  m.Groups[3].Value + "new System.IO.StreamWriter" + m.Groups[5].Value + ")");
330  }
334  else if (match.Groups[7].Value == "r")
335  {
336  codeLine = Regex.Replace(codeLine, patternFopen1, m => m.Groups[1].Value + "System.IO.StreamReader " + m.Groups[2].Value +
337  m.Groups[3].Value + "new System.IO.StreamReader" + m.Groups[5].Value + ")");
338  }
339  }
340 
341  break;
342 
346  case 5:
347 
348  string patternFgets = @"(^|[^\w])(fgets)(\s*\()(\w+)(\s*,\s*[^,]+,\s*)(\w+)(\s*\)\s*)((!=)?(==)?)";
349  Match matchFgets = Regex.Match(codeLine, patternFgets);
350  if (matchFgets.Success)
351  {
352  if (matchFgets.Groups[8].Success)
353  {
354  codeLine = Regex.Replace(codeLine, patternFgets, m => "(" + m.Groups[1].Value + m.Groups[4].Value +
355  " = " + m.Groups[6].Value + ".ReadLine()" + ") " + m.Groups[8].Value);
356  }
357  else
358  {
359  codeLine = Regex.Replace(codeLine, patternFgets, m => m.Groups[1].Value + m.Groups[4].Value +
360  " = " + m.Groups[6].Value + ".ReadLine()");
361  }
362  }
363 
364  break;
365 
369  case 6: // keyword: fclose
370 
371  string patternFclose = @"(^|[^\w])(fclose)(\s*\()(\w+)(\s*\))";
372  if (Regex.IsMatch(codeLine, patternFclose))
373  {
374  codeLine = Regex.Replace(codeLine, patternFclose, m => m.Groups[1].Value + m.Groups[4].Value +
375  ".Close()");
376  }
377 
378  break;
379 
383  case 7:
384 
385  string patternFprintf = @"(^|[^\w])(fprintf)(\s*\()(\w+)(,\s*)([^\)]*)";
386  Regex rNewLine1 = new Regex(@"(\\n)(\s*"")");
387 
388  if (rNewLine1.IsMatch(codeLine))
389  {
390  codeLine = Regex.Replace(codeLine, patternFprintf, m => m.Groups[1].Value + m.Groups[4].Value +
391  ".WriteLine(" + m.Groups[6].Value);
392  codeLine = rNewLine1.Replace(codeLine, m => "" + m.Groups[2].Value);
393  }
394  else
395  {
396  codeLine = Regex.Replace(codeLine, patternFprintf, m => m.Groups[1].Value + m.Groups[4].Value +
397  ".Write(" + m.Groups[6].Value + ")");
398  }
399 
400  break;
401  }
402 
403  break;
404  }
405  }
406 
413  private void Transpose_Stdlib_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
414  {
415  switch (Mode)
416  {
420  case 'r':
421 
422  int fileIndex = SyntaxTableLastIndex(1, -1) + 1;
423 
424  this.SyntaxTable[fileIndex, 0] = "stdlib.h";
425  this.SyntaxTable[fileIndex, 1] = "atoi";
426  break;
427 
431  case 'c':
432 
433  switch (SyntaxMatchNumber)
434  {
438  case 1:
439 
440  string patternAtoi = @"(^|[^\w])(atoi)([^\w])";
441 
442  if (Regex.IsMatch(codeLine, patternAtoi))
443  {
444  codeLine = Regex.Replace(codeLine, patternAtoi, m => m.Groups[1].Value + "Int32.Parse" + m.Groups[3].Value);
445  }
446 
447  break;
448  }
449 
450  break;
451  }
452  }
453 
460  private void Transpose_String_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
461  {
462  switch (Mode)
463  {
467  case 'r':
468 
469  int fileIndex = this.SyntaxTableLastIndex(1, -1) + 1;
470 
471  SyntaxTable[fileIndex, 0] = "string.h";
472  SyntaxTable[fileIndex, 1] = "strlen";
473  break;
474 
478  case 'c':
479 
480  switch (SyntaxMatchNumber)
481  {
485  case 1:
486 
487  string SyntaxPattern = @"(^|[^\w])(strlen\s*\(\s*)([^)|^\s]+)(\s*\))";
488 
489  if (Regex.IsMatch(codeLine, SyntaxPattern))
490  {
491  codeLine = Regex.Replace(codeLine, SyntaxPattern, m => m.Groups[1].Value + m.Groups[3].Value + ".Length");
492  }
493 
494  break;
495  }
496 
497  break;
498  }
499  }
500 
507  public bool SyntaxScan(ref string codeLine)
508  {
513  bool SyntaxMainScan = false;
514  int IndexMaxA = SyntaxTableLastIndex(1, -1) + 1;
515 
519  for (int IndexHeader = 0; IndexHeader < IndexMaxA; ++IndexHeader)
520  {
524  int IndexMaxB = SyntaxTableLastIndex(2, IndexHeader) + 1;
525  for (int IndexSyntax = 1; IndexSyntax < IndexMaxB; ++IndexSyntax)
526  {
530  Match SYNTAX = Regex.Match(codeLine, @"(^|[^\w])(" + SyntaxTable[IndexHeader, IndexSyntax] + @")([^\w]|$)");
531  if (SYNTAX.Success)
532  {
533  if (SyntaxTable[IndexHeader, 0] == "generic") { Transpose_Generic_Syntax('c', ref codeLine, IndexSyntax); }
534  if (SyntaxTable[IndexHeader, 0] == "stdio.h") { Transpose_Studio_Syntax('c', ref codeLine, IndexSyntax); }
535  if (SyntaxTable[IndexHeader, 0] == "stdlib.h") { Transpose_Stdlib_Syntax('c', ref codeLine, IndexSyntax); }
536  if (SyntaxTable[IndexHeader, 0] == "string.h") { Transpose_String_Syntax('c', ref codeLine, IndexSyntax); }
537  if (SyntaxTable[IndexHeader, IndexSyntax] == "main") { SyntaxMainScan = true; }
538  }
539  }
540  }
541 
542  return SyntaxMainScan;
543  }
544  }
545 }
Transposer(string CSNameSpace)
This method manages the name space for the cs file
Definition: Transposer.cs:29
void Transpose_Stdlib_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method manages the stdlib.h library transpose
Definition: Transposer.cs:413
void Transpose_Studio_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method manages the stdio.h library transpose
Definition: Transposer.cs:213
bool SyntaxScan(ref string codeLine)
This method scan each line of object c code and transpose it to c-sharp
Definition: Transposer.cs:507
void Transpose_Generic_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method Manages the generic syntax transpose
Definition: Transposer.cs:88
int SyntaxTableLastIndex(int IndexA, int IndexB)
This method manages the syntax table indexs
Definition: Transposer.cs:51
void Transpose_String_Syntax(char Mode, ref string codeLine, int SyntaxMatchNumber)
This method manages the String.h libraty transpose
Definition: Transposer.cs:460