1 //////////////////////////////////////////////////////////////////////////////
2 //Copyright 2008
3 //  Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
4 //////////////////////////////////////////////////////////////////////////////
5 // This file is part of Teyjus.                                             //
6 //                                                                          //
7 // Teyjus is free software: you can redistribute it and/or modify           //
8 // it under the terms of the GNU General Public License as published by     //
9 // the Free Software Foundation, either version 3 of the License, or        //
10 // (at your option) any later version.                                      //
11 //                                                                          //
12 // Teyjus is distributed in the hope that it will be useful,                //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of           //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
15 // GNU General Public License for more details.                             //
16 //                                                                          //
17 // You should have received a copy of the GNU General Public License        //
18 // along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
19 //////////////////////////////////////////////////////////////////////////////
20 /****************************************************************************/
21 /*                                                                          */
22 /* File mcstring.c.                                                         */
23 /****************************************************************************/
24 #include <string.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include "mcstring.h"
28 #include "mctypes.h"
29 
30 //length of a given string; the string pointer is assumed to not be NULL
MCSTR_strLength(MCSTR_Str str)31 int MCSTR_strLength(MCSTR_Str str)
32 {
33     return *((int *)str);
34 }
35 
36 //number of words needed for a string with n characters
MCSTR_numWords(int n)37 int MCSTR_numWords(int n)
38 {
39     return ((int)ceil(((double)(n+1))/WORD_SIZE)) + 1; //with '\0' terminator
40 }
41 
42 //from machine string to c string
MCSTR_toCString(MCSTR_Str str)43 char* MCSTR_toCString(MCSTR_Str str)
44 {
45     return (char*)(str + 1);
46 }
47 
48 //to string
MCSTR_toString(MCSTR_Str loc,char * buf,int length)49 void MCSTR_toString(MCSTR_Str loc, char* buf, int length)
50 {
51     char* chloc = (char*)(loc + 1);
52     *((int *)loc) = length;
53     strcpy(chloc, buf);
54 }
55 
56 //compare whether two string literals are the same
MCSTR_sameStrs(MCSTR_Str str1,MCSTR_Str str2)57 Boolean MCSTR_sameStrs(MCSTR_Str str1, MCSTR_Str str2)
58 {
59     if (strcmp((char*)(str1+1), (char*)(str2+1)) == 0) return TRUE;
60     else return FALSE;
61 }
62 
63 /* compare strings: return <  0 if str1 <  str2
64                     return == 0 if str1 == str2
65                     return >  0 if str1 >  str2
66 */
MCSTR_compareStrs(MCSTR_Str str1,MCSTR_Str str2)67 int MCSTR_compareStrs(MCSTR_Str str1, MCSTR_Str str2)
68 {
69     return strcmp((char*)(str1+1), (char*)(str2+1));
70 }
71 
72 //string concatenate (the new string is created at address started from loc)
MCSTR_concat(MCSTR_Str loc,MCSTR_Str str1,MCSTR_Str str2)73 void MCSTR_concat(MCSTR_Str loc, MCSTR_Str str1, MCSTR_Str str2)
74 {
75     char* chloc = (char*)(loc + 1);
76     *((int *)loc) = MCSTR_strLength(str1) + MCSTR_strLength(str2);
77     strcpy(chloc, (char*)(str1+1));
78     strcat(chloc, (char*)(str2+1));
79 }
80 
81 //substring (the new string is created at address started from loc)
MCSTR_subString(MCSTR_Str loc,MCSTR_Str str,int startPos,int length)82 void MCSTR_subString(MCSTR_Str loc, MCSTR_Str str, int startPos, int length)
83 {
84     char* fromPtr = ((char*)(str + 1))+startPos;
85     char* toPtr   = (char*)(loc + 1);
86 
87     *((int *)loc) = (length);
88     while (length > 0) {
89       *toPtr++ = *fromPtr++;
90       length--;
91     }
92     *toPtr = '\0';
93 }
94 
95 //chr
MCSTR_chr(MCSTR_Str loc,int integer)96 void MCSTR_chr(MCSTR_Str loc, int integer)
97 {
98     char* chloc = (char*)(loc + 1);
99     *((int *)loc) = 1;
100     *chloc++ = (char)integer;
101     *chloc   = '\0';
102 }
103 
104 //ord
MCSTR_ord(MCSTR_Str str)105 int MCSTR_ord(MCSTR_Str str)
106 {
107     return (int)(*((char*)(str + 1)));
108 }
109 
110 //display on standard IO
MCSTR_printStr(MCSTR_Str str)111 void    MCSTR_printStr(MCSTR_Str str)
112 {
113     printf("%s", (char*)(str+1));
114 }
115 
116