int tm_eq(tm_obj a, tm_obj b){ if( a.type != b.type ) return 0; switch( a.type ){ case TM_NUM: return get_num(a) == get_num(b); case TM_STR: { char* sa = get_str(a); char* sb = get_str(b); return sa == sb || ( get_str_len(a) == get_str_len(b) && strncmp(sa, sb, get_str_len(a)) == 0 ); } case TM_LST: { if( get_list(a) == get_list(b)) return 1; int i; int len = get_list(a)->len; tm_obj* nodes1 = get_list(a)->nodes; tm_obj* nodes2 = get_list(b)->nodes; for(i = 0; i < len; i++){ if( !tm_eq(nodes1[i], nodes2[i]) ){ return 0; } } return 1; } case TM_NON:return 1; default: tm_raise("tm_eq(): not supported type %d", a.type); } return 0; }
tm_obj tm_add( tm_obj a, tm_obj b){ if( a.type == b.type ){ switch( a.type ){ case TM_NUM: get_num(a) += get_num(b); return a; case TM_STR: { char* sa = get_str(a); char* sb = get_str(b); int la = get_str_len(a); int lb = get_str_len(b); if( la == 0){ return b; } if( lb == 0){ return a; } int len = la + lb; tm_obj des = str_new(NULL, len); char*s = get_str(des); memcpy(s, sa, la); memcpy(s + la, sb, lb); return des; } case TM_LST: { return list_join(get_list(a), get_list(b) ); } } } tm_raise("tm_add: can not add %t and %t", (a),(b)); return obj_none; }
tm_obj tm_get(tm_obj self, tm_obj k){ tm_obj v; switch( self.type){ case TM_STR:{ if( k.type == TM_NUM ){ int n = get_num(k); if (n < 0) n += get_str_len(self); if( n >= get_str_len(self) || n < 0) tm_raise("tm_get: index overflow"); //tm_printf("str = @, len = @, index = @\n", self,number_new(get_str_len(self)), k); unsigned char c = get_str(self)[n]; //printf("c = %d\n", c); return __chars__[c]; }else if( dict_iget( get_dict(str_class), k , &v) ){ return method_new(v, self); } } case TM_LST: { if( k.type == TM_NUM ){ return list_get( self.value.list, get_num(k)); }else if( dict_iget(get_dict(list_class), k, &v) ){ return method_new( v, self); } break; } case TM_DCT: if( dict_iget( get_dict(self), k, &v)){ return v; }else if(dict_iget(get_dict(dict_class), k, &v)){ return method_new( v, self); } break; case TM_FNC: /* if( k.type == TM_STR && strequals(get_str(k), "code")){ return get_func( self )->code; }*/ break; } // tm_printf_only_type("@", self); // cprintln(self); tm_raise("tm_get: keyError @, self = @ ", _tm_type(k), _tm_type(self) ); return obj_none; }
int _tm_bool( tm_obj v){ switch( v.type ){ case TM_NUM:return get_num(v) != 0; case TM_NON:return 0; case TM_STR:return get_str_len(v) > 0; case TM_LST:return get_list_len(v) > 0; case TM_DCT:return get_dict_len(v) > 0; } return 0; }
tm_obj _tm_save(char*fname, tm_obj content){ FILE* fp = fopen(fname, "wb"); if( fp == NULL ){ tm_raise("tm_save : can not save to file @" , fname ); } char* txt = get_str( content ); int len = get_str_len( content ); fwrite(txt, len, 1, fp); fclose(fp); }
void print_word( char* c) { int length = get_str_len(c); int i = 0; for(; i < length; i++) { char ch = c[i]; printf("%c", ch); } }
void h_string_append (HString *str, const char *append, int len) { int l; l = get_str_len (append, len); str->str = (char *) realloc (str->str, str->len + l + 1); memcpy (str->str + str->len, append, l); str->len += l; str->str[str->len] = '\0'; }
tm_obj tm_mul( tm_obj a, tm_obj b){ if( a.type == b.type && a.type == TM_NUM){ return number_new( get_num(a) * get_num(b) ); } if( a.type == TM_NUM && b.type == TM_STR){ tm_obj temp = a; a = b; b = temp; } if( a.type == TM_STR && b.type == TM_NUM){ if( get_str_len(a) == 0) return a; tm_obj str = str_new(NULL, get_int(b) * get_str_len(a) ); char* s = get_str(str); int i;for(i = 0; i < get_str_len(str) ; i+= get_str_len(a)){ memcpy( s + i , get_str(a), get_str_len(a)); } return str; } tm_raise("tm_mul: can not mul @ and @", a,b ); return obj_none; }
tm_obj _tm_not( tm_obj o){ switch( o.type ){ case TM_NUM: if( get_num(o) ) return obj_false; return obj_true; case TM_STR: if( get_str_len(o) > 0) return obj_false; return obj_true; case TM_NON: return obj_true; } return obj_false; }
HString * h_string_new (const char *initValue, int len) { HString *str; unsigned int l; str = (HString *) malloc (sizeof (HString)); l = get_str_len (initValue, len); str->str = (char *) malloc (l + 1); memcpy (str->str, initValue, l); str->str[l] = '\0'; str->len = l; return str; }
void print_dependencies(struct file_redir** file_depend_list) { int index; for(index = 0; index < g_num_depend; index++) { printf("%d ", index); int num = file_depend_list[index][0].num; int i; if(num == 0) { printf("no files\n"); continue; } /*else { printf("num: %d\n", num); }*/ for(i = 1; i < num+1; i++) { /*char* c = file_depend_list[index][i].fileName; if(c == 0) { printf("NULL: %d\n", index); }*/ int length = get_str_len(file_depend_list[index][i].fileName); // printf("length: %d\n", length); int j; for(j = 0; j < length; j++) { char c = file_depend_list[index][i].fileName[j]; printf("%c", c); } if(file_depend_list[index][i].redir == INPUT) { printf(", INPUT\n"); } else { printf(", OUTPUT\n"); } } } }
//Driver int main( int argc, char *argv[] ) { //Local Variables char test_str[ MAX_STR_SZ + 1 ]; int str_sz; //CLI Parsing if( argc < 2 ) { //Prompt the user for input printf( "Please Enter String for consideration\nMax Character Length: %d\n>", MAX_STR_SZ ); fgets( test_str, MAX_STR_SZ, stdin ); } //Size Checking of argument str_sz = get_str_len( argv[ 1 ] ); memcpy( &test_str, argv[ 1 ], ( ( str_sz < MAX_STR_SZ ? str_sz : MAX_STR_SZ ) ) ); printf( "Result: %s\n", ( determine_palindrome( ( char* )( &test_str ), str_sz ) ? "Yes." : "No. " ) ); return 0; }
void print_words(char** words, int numWords) { //printf("numWords: %d\n", numWords); int i; for(i = 0; i < numWords; i++) { char* word = words[i]; int length = get_str_len(word); //printf("printWord1"); //printWord(word); //printf("printWord2"); //int length = strlen(word); int j; //printf("length: %d\n", length); for(j = 0; j < length; j++) { char c = word[j]; printf("%c", c); } printf(" "); } printf("\n"); }