The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
roman.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * roman.cpp -
4  *
5  * $Id: roman.cpp 3754 2020-07-10 17:45:48Z scribe $
6  *
7  * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #include <roman.h>
26 
27 
29 
30 
31 char isRoman(const char *str, int maxchars) {
32  char *ch = (char*)str;
33  for (; *ch && (!maxchars || (ch-str) <= maxchars); ch++)
34  if (!strchr("IVXLCDMivxlcdm ", *ch))
35  return 0;
36  return 1;
37 }
38 
39 int fromRoman(const char* str) {
40  int i, n = (int)strlen(str);
41  short * num= (short *) calloc(n, sizeof(short));
42  for (i = 0; str[i]; i++) {
43  switch(str[i]) {
44  case 'i':
45  case 'I':
46  num[i] = 1;
47  break;
48  case 'v':
49  case 'V':
50  num[i] = 5;
51  break;
52  case 'x':
53  case 'X':
54  num[i] = 10;
55  break;
56  case 'l':
57  case 'L':
58  num[i] = 50;
59  break;
60  case 'c':
61  case 'C':
62  num[i] = 100;
63  break;
64  case 'd':
65  case 'D':
66  num[i] = 500;
67  break;
68  case 'm':
69  case 'M':
70  num[i] = 1000;
71  break;
72  default:
73  num[i] = 0;
74  }
75  }
76  for (i = 1; str[i]; i++) {
77  if (num[i] > num[i-1]) {
78  num[i] -= num[i-1];
79  num[i-1] = 0;
80  }
81  }
82  n = 0;
83  for (i = 0; str[i]; i++) {
84  n += num[i];
85  }
86 
87  free(num);
88 
89  return n;
90 }
91 
92 
94 
#define SWORD_NAMESPACE_START
Definition: defs.h:39
free(preg->fastmap)
SWORD_NAMESPACE_START char isRoman(const char *str, int maxchars)
Definition: roman.cpp:31
int fromRoman(const char *str)
Definition: roman.cpp:39
#define SWORD_NAMESPACE_END
Definition: defs.h:40