festring& festring::operator=(cchar* CStr) {DBGEXEC(bugTrackInvalidChars(CStr,strlen(CStr))); CheckNull(CStr); sizetype NewSize = strlen(CStr); if(OwnsData) { if(!REFS(Data) && NewSize <= Reserved) { memcpy(Data, CStr, NewSize); Size = NewSize; return *this; } if(!REFS(Data)--) delete [] &REFS(Data); } if(NewSize) CreateOwnData(CStr, NewSize); else Empty(); return *this; }
/// Erase the substring starting from the specified position. void festring::Erase(sizetype Pos, sizetype Length) { if(!Length || Size <= Pos) return; if(Pos == 0 && Pos + Length >= Size) { Empty(); return; } char* OldData = Data; sizetype OldSize = Size; sizetype NewSize = (Pos + Length < Size) ? (Size - Length) : Pos; sizetype EraseEnd = (Pos + Length < Size) ? (Pos + Length) : OldSize; if(OwnsData) { if(!REFS(OldData)) { memmove(OldData + Pos, OldData + EraseEnd, OldSize - EraseEnd); Size = NewSize; return; } else --REFS(OldData); } CreateNewData(NewSize); memcpy(Data, OldData, Pos); memcpy(Data + Pos, OldData + EraseEnd, OldSize - EraseEnd); Size = NewSize; }
/// Set the data to a string of the same char. void festring::Assign(sizetype NewSize, char Char) { if(!NewSize) { Empty(); return; } if(OwnsData) { if(!REFS(Data) && NewSize <= Reserved) { memset(Data, Char, NewSize); Size = NewSize; return; } if(!REFS(Data)--) delete [] &REFS(Data); } CreateNewData(NewSize); memset(Data, Char, NewSize); Size = NewSize; }
void festring::SlowAppend(char Char) { char* OldPtr = Data; if(OldPtr) { sizetype OldSize = Size++; sizetype NewSize = OldSize + 1; ulong* DeletePtr = 0; if(OwnsData && !REFS(OldPtr)--) DeletePtr = &REFS(OldPtr); Reserved = NewSize|FESTRING_PAGE; char* NewPtr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(NewPtr) = 0; Data = NewPtr; memcpy(NewPtr, OldPtr, OldSize); NewPtr[OldSize] = Char; if(DeletePtr) delete [] DeletePtr; } else { Size = 1; Reserved = FESTRING_PAGE; char* Ptr = sizeof(ulong) + new char[FESTRING_PAGE + sizeof(ulong) + 1]; REFS(Ptr) = 0; Ptr[0] = Char; Data = Ptr; } OwnsData = true; }
festring& festring::operator=(cfestring& Str) {DBGEXEC(bugTrackInvalidChars(Str.Data,Str.Size)); char* CStr = Str.Data; sizetype NewSize = Str.Size; if(OwnsData) { if(!REFS(Data) && NewSize <= Reserved) { memcpy(Data, CStr, NewSize); Size = NewSize; return *this; } if(!REFS(Data)--) delete [] &REFS(Data); } Size = NewSize; Data = CStr; OwnsData = Str.OwnsData; if(OwnsData) { if(REFS(Data) < FESTRING_REF_MAX) { ++REFS(Data); Reserved = Str.Reserved; } else CreateOwnData(CStr, NewSize); } return *this; }
void festring::SlowAppend(const char* CStr, sizetype N) { char* OldPtr = Data; if(OldPtr) { sizetype OldSize = Size; sizetype NewSize = OldSize + N; Size = NewSize; ulong* DeletePtr = 0; if(OwnsData && !REFS(OldPtr)--) DeletePtr = &REFS(OldPtr); Reserved = NewSize|FESTRING_PAGE; char* NewPtr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(NewPtr) = 0; Data = NewPtr; memcpy(NewPtr, OldPtr, OldSize); memcpy(NewPtr + OldSize, CStr, N); OwnsData = true; if(DeletePtr) delete [] DeletePtr; } else CreateOwnData(CStr, N); }
festring& festring::operator=(const festring& Str) { sizetype NewSize = Str.Size; Size = NewSize; char* Ptr = Data; char* StrPtr = Str.Data; if(Ptr && OwnsData) { if(!REFS(Ptr) && NewSize <= Reserved) { if(StrPtr) memcpy(Ptr, StrPtr, NewSize); return *this; } if(!REFS(Ptr)--) delete [] &REFS(Ptr); } if((Data = StrPtr) && (OwnsData = Str.OwnsData)) { ++REFS(StrPtr); Reserved = Str.Reserved; } return *this; }
festring& festring::operator=(staticstring SStr) {DBGEXEC(bugTrackInvalidChars(SStr.Data,SStr.Size)); CheckNull(SStr.Data); cchar* CStr = SStr.Data; sizetype NewSize = SStr.Size; if(OwnsData) { if(!REFS(Data) && NewSize <= Reserved) { memcpy(Data, CStr, NewSize); Size = NewSize; return *this; } if(!REFS(Data)--) delete [] &REFS(Data); } Size = NewSize; Data = const_cast<char*>(CStr); OwnsData = false; return *this; }
void festring::EnsureOwnsData(bool Unique) { if(!OwnsData) CreateOwnData(Data, Size); else if(Unique && REFS(Data)) { --REFS(Data); CreateOwnData(Data, Size); } }
void festring::Insert(sizetype Pos, const char* CStr, sizetype N) { if(N) { sizetype OldSize = Size; if(Pos < OldSize) // this implies Data != 0 { char* OldPtr = Data; ulong* DeletePtr = 0; sizetype NewSize = OldSize + N; Size = NewSize; if(OwnsData) { if(!REFS(OldPtr)) { if(NewSize <= Reserved) { char* Ptr = OldPtr + Pos; memmove(Ptr + N, Ptr, OldSize - Pos); memcpy(Ptr, CStr, N); return; } else DeletePtr = &REFS(OldPtr); } else --REFS(OldPtr); } Reserved = NewSize|FESTRING_PAGE; char* NewPtr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(NewPtr) = 0; Data = NewPtr; memcpy(NewPtr, OldPtr, Pos); memcpy(NewPtr + Pos, CStr, N); memcpy(NewPtr + Pos + N, OldPtr + Pos, OldSize - Pos); OwnsData = true; if(DeletePtr) delete [] DeletePtr; } else if(Pos == OldSize) Append(CStr, N); else ABORT("Illegal festring insertion detected!"); } }
void festring::Erase(sizetype Pos, sizetype Length) { char* OldPtr = Data; if(OldPtr && Length) { sizetype OldSize = Size; if(Pos < OldSize) { truth MoveReq = Length < OldSize - Pos; if(OwnsData) { if(!REFS(OldPtr)) { if(MoveReq) { sizetype End = Pos + Length; memmove(OldPtr + Pos, OldPtr + End, OldSize - End); } Size -= Length; return; } else --REFS(OldPtr); } sizetype NewSize = MoveReq ? OldSize - Length : Pos; Size = NewSize; Reserved = NewSize|FESTRING_PAGE; char* Ptr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(Ptr) = 0; Data = Ptr; OwnsData = true; if(Pos) memcpy(Ptr, OldPtr, Pos); if(MoveReq) { sizetype End = Pos + Length; memcpy(Ptr + Pos, OldPtr + End, OldSize - End); } } } }
void festring::CreateNewData(sizetype N) { Reserved = N|FESTRING_PAGE; Data = sizeof(int*) + new char[Reserved + sizeof(int*) + 1]; OwnsData = true; REFS(Data) = 0; Size = 0; }
char * make_shared_string (const char * str) { block_t *b; int h; b = hfindblock(str, h); /* hfindblock macro sets h = StrHash(s) */ if (!b) { b = alloc_new_string(str, h); } else { if (REFS(b)) REFS(b)++; ADD_STRING(SIZE(b)); } NDBG(b); return (STRING(b)); }
/// Insert a string at the specified position. void festring::Insert(sizetype Pos, cchar* CStr, sizetype N) { if(!N) return; if(Size < Pos) ABORT("Illegal festring insertion detected!"); if(Size == Pos) { Append(CStr, N); return; } char* OldData = Data; sizetype OldSize = Size; sizetype NewSize = OldSize + N; decltype(&REFS(Data)) FreePtr = 0; if(OwnsData) { if(!REFS(OldData)) { if(NewSize <= Reserved) { memmove(OldData + Pos + N, OldData + Pos, OldSize - Pos); memcpy(OldData + Pos, CStr, N); Size = NewSize; return; } else FreePtr = &REFS(OldData); } else --REFS(OldData); } CreateNewData(NewSize); memcpy(Data, OldData, Pos); memcpy(Data + Pos, CStr, N); memcpy(Data + Pos + N, OldData + Pos, OldSize - Pos); Size = NewSize; if(FreePtr) delete [] FreePtr; }
const char * ref_string (const char * str) { block_t *b; b = BLOCK(str); #ifdef DEBUG if (b != findblock(str)) { fatal("stralloc.c: called ref_string on non-shared string: %s.\n", str); } #endif /* defined(DEBUG) */ if (REFS(b)) REFS(b)++; NDBG(b); ADD_STRING(SIZE(b)); return str; }
festring& festring::Append(char Char) { if(OwnsData && !REFS(Data) && Size < Reserved) Data[Size++] = Char; else SlowAppend(&Char, 1); return *this; }
festring& festring::Capitalize() { char* OldPtr = Data; if(*OldPtr > 0x60 && *OldPtr < 0x7B) { if(!OwnsData) CreateOwnData(OldPtr, Size); else if(REFS(OldPtr)) { --REFS(OldPtr); CreateOwnData(OldPtr, Size); } *Data ^= 0x20; } return *this; }
void festring::CreateOwnData(const char* CStr, sizetype N) { Size = N; Reserved = N|FESTRING_PAGE; char* Ptr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(Ptr) = 0; Data = Ptr; memcpy(Ptr, CStr, N); OwnsData = true; }
/// Always allocate new memory before append data. void festring::SlowAppend(cchar* CStr, sizetype N) { if(!N) return; char* OldData = Data; sizetype OldSize = Size; sizetype NewSize = OldSize + N; decltype(&REFS(Data)) FreePtr = 0; if(OwnsData && !REFS(OldData)--) FreePtr = &REFS(OldData); CreateNewData(NewSize); memcpy(Data, OldData, OldSize); memcpy(Data + OldSize, CStr, N); Size = NewSize; if(FreePtr) delete [] FreePtr; }
void coup_mu_mesag( const short *message ) { GRECT *rect ; short dummy ; switch( message[0] ) { case WM_REDRAW : form_redraw( message, &coup_form ) ; break ; case WM_MOVED : rect = (GRECT *) &(message[4]) ; wind_set( coup_form.fm_handle, WF_CURRXYWH, PTRS( rect ) ) ; coup_form.fm_box = *rect ; wind_calc( WC_WORK, wind_form_kind, ELTS( coup_form.fm_box ), &coup_form.fm_ptr[ROOT].ob_x, &coup_form.fm_ptr[ROOT].ob_y, &dummy, &dummy ) ; break ; case WM_TOPPED : if( message[3] == coup_form.fm_handle ) wind_set( coup_form.fm_handle, WF_TOP ) ; break ; case WM_BOTTOM : wind_set( coup_form.fm_handle, WF_BOTTOM ) ; break ; case WM_CLOSED : save_couple( edit_coup_ref, edit_coup_block, edit_coup_cptr ) ; close_couple() ; break ; case WM_ALLICONIFY : iconify_all( coup_form.fm_box.g_x ) ; break ; case WM_ICONIFY : wind_set( coup_form.fm_handle, WF_UNICONIFYXYWH, ELTS( coup_form.fm_box ) ) ; coup_form.fm_box = *(GRECT *)&message[4] ; coup_form.iconified = TRUE ; wind_set( coup_form.fm_handle, WF_ICONIFY, ELTS( coup_form.fm_box ) ) ; wind_title( coup_form.fm_handle, coup_form.icon_title ) ; send_redraw_message( &coup_form.fm_box, coup_form.fm_handle ) ; break ; case WM_UNICONIFY : wind_get( coup_form.fm_handle, WF_UNICONIFY, REFS( coup_form.fm_box ) ) ; wind_calc( WC_WORK, wind_form_kind, ELTS( coup_form.fm_box ), &coup_form.fm_ptr[ROOT].ob_x, &coup_form.fm_ptr[ROOT].ob_y, &dummy, &dummy ) ; wind_set( coup_form.fm_handle, WF_UNICONIFY, ELTS( coup_form.fm_box ) ) ; wind_title( coup_form.fm_handle, coup_form.title_str ) ; coup_form.iconified = FALSE ; break ; default : break ; } }
void free_string (const char * str) { block_t **prev, *b; int h; b = BLOCK(str); DEBUG_CHECK1(b != findblock(str),"stralloc.c: free_string called on non-shared string: %s.\n", str); /* * if a string has been ref'd USHRT_MAX times then we assume that its used * often enough to justify never freeing it. */ if (!REFS(b)) return; REFS(b)--; SUB_STRING(SIZE(b)); NDBG(b); if (REFS(b) > 0) return; h = StrHash(str); prev = base_table + h; while ((b = *prev)) { if (STRING(b) == str) { *prev = NEXT(b); break; } prev = &(NEXT(b)); } DEBUG_CHECK1(!b, "free_string: not found in string table! (\"%s\")\n", str); SUB_NEW_STRING(SIZE(b), sizeof(block_t)); FREE(b); CHECK_STRING_STATS; }
festring& festring::operator=(const char* CStr) { sizetype NewSize = strlen(CStr); Size = NewSize; char* Ptr = Data; if(Ptr && OwnsData) { if(!REFS(Ptr) && NewSize <= Reserved) { memcpy(Ptr, CStr, NewSize); return *this; } if(!REFS(Ptr)--) delete [] &REFS(Ptr); } Data = const_cast<char*>(CStr); OwnsData = false; return *this; }
void festring::Resize(sizetype N, char C) { sizetype OldSize = Size; char* OldPtr = Data; char* NewPtr; Size = N; if(OldSize < N) { ulong* DeletePtr = 0; if(OwnsData && OldPtr) { if(!REFS(OldPtr)) { if(N <= Reserved) { memset(OldPtr + OldSize, C, N - OldSize); return; } else DeletePtr = &REFS(OldPtr); } else --REFS(OldPtr); } Reserved = N|FESTRING_PAGE; NewPtr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(NewPtr) = 0; Data = NewPtr; memcpy(NewPtr, OldPtr, OldSize); memset(NewPtr + OldSize, C, N - OldSize); OwnsData = true; if(DeletePtr) delete [] DeletePtr; } else { if(OwnsData && OldPtr) if(!REFS(OldPtr)) return; else --REFS(OldPtr); Reserved = N|FESTRING_PAGE; NewPtr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(NewPtr) = 0; Data = NewPtr; memcpy(NewPtr, OldPtr, N); OwnsData = true; } }
void festring::Assign(sizetype N, char C) { Size = N; char* Ptr = Data; if(OwnsData && Ptr) { if(!REFS(Ptr) && N <= Reserved) { memset(Ptr, C, N); return; } else delete [] &REFS(Ptr); } Reserved = N|FESTRING_PAGE; Ptr = sizeof(ulong) + new char[Reserved + sizeof(ulong) + 1]; REFS(Ptr) = 0; Data = Ptr; memset(Ptr, C, N); OwnsData = true; }
festring& festring::Append(const char* CStr, sizetype N) { sizetype OldSize = Size; sizetype NewSize = OldSize + N; char* OldPtr = Data; if(OwnsData && OldPtr && !REFS(OldPtr) && NewSize <= Reserved) { memcpy(OldPtr + OldSize, CStr, N); Size = NewSize; } else SlowAppend(CStr, N); return *this; }
festring& festring::Append(cchar* CStr, sizetype N) { if(!N) return *this; sizetype OldSize = Size; sizetype NewSize = OldSize + N; if(OwnsData && !REFS(Data) && NewSize <= Reserved) { memcpy(Data + OldSize, CStr, N); Size = NewSize; } else SlowAppend(CStr, N); return *this; }
/// Resize the string and use a char for padding. void festring::Resize(sizetype NewSize, char Char) { if(Size > NewSize) { if(OwnsData) { if(!REFS(Data)) { Size = NewSize; return; } --REFS(Data); } CreateOwnData(Data, NewSize); return; } else if(Size < NewSize) { char* OldData = Data; sizetype OldSize = Size; decltype(&REFS(Data)) FreePtr = 0; if(OwnsData) { if(!REFS(OldData)) { if(NewSize <= Reserved) { memset(OldData + OldSize, Char, NewSize - OldSize); Size = NewSize; return; } else FreePtr = &REFS(OldData); } else --REFS(OldData); } CreateNewData(NewSize); memcpy(Data, OldData, OldSize); memset(Data + OldSize, Char, NewSize - OldSize); Size = NewSize; if(FreePtr) delete [] FreePtr; } else return; }
festring& festring::Append(cfestring& Str) { if(!Str.Size) return *this; sizetype N = Str.Size; sizetype OldSize = Size; sizetype NewSize = OldSize + N; char* OldPtr = Data; char* OtherPtr = Str.Data; if(OwnsData && !REFS(Data) && NewSize <= Reserved) { memcpy(Data + OldSize, Str.Data, N); Size = NewSize; } else SlowAppend(Str.Data, N); return *this; }
INLINE_STATIC block_t * alloc_new_string (const char * string, int h) { block_t *b; int len = strlen(string); int size; if (len > max_string_length) { len = max_string_length; } size = sizeof(block_t) + len + 1; b = (block_t *) DXALLOC(size, TAG_SHARED_STRING, "alloc_new_string"); strncpy(STRING(b), string, len); STRING(b)[len] = '\0'; /* strncpy doesn't put on \0 if 'from' too * long */ SIZE(b) = (len > USHRT_MAX ? USHRT_MAX : len); REFS(b) = 1; NEXT(b) = base_table[h]; base_table[h] = b; ADD_NEW_STRING(SIZE(b), sizeof(block_t)); ADD_STRING(SIZE(b)); return (b); }