home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / Nuntius / Tools.cp < prev    next >
Encoding:
Text File  |  1994-02-20  |  6.5 KB  |  296 lines  |  [TEXT/MPS ]

  1. // Copyright © 1992 Peter Speck, speck@dat.ruc.dk. All rights reserved.
  2. // Tools.cp
  3.  
  4. #include "Tools.h"
  5. #include "FileTools.h"
  6.  
  7. #include <Resources.h>
  8. #include <Packages.h>
  9. #include <Errors.h>
  10. #include <ToolUtils.h>
  11.  
  12. #pragma segment MyTools
  13.  
  14. void InitUTools()
  15. {
  16. }
  17.  
  18. void MyGetIndString(CStr255 &s, short id)
  19. {
  20. #if qDebug
  21.     if (id < 200)
  22.     {
  23.         fprintf(stderr, "MyGetIndString, id = %ld\n", long(id));
  24.         ProgramBreak(gEmptyString);
  25.     }
  26. #endif
  27.     const short kListSize = 20;
  28.     short rsrcID = (id / kListSize) * kListSize;
  29.     short index = id % kListSize;
  30.     GetIndString(s, rsrcID, index);
  31. #if qDebug
  32.     Handle h = GetResource('STR#', rsrcID);
  33.     if (!h)
  34.     {
  35.         fprintf(stderr, "MyGetIndString: String with id = %hd  (rsrcID = %hd) does not exist\n", id, rsrcID);
  36.         ProgramBreak(gEmptyString);
  37.     }
  38.     else if (index < 1 || index > *( (short*) *h))
  39.     {
  40.         fprintf(stderr, "MyGetIndString: String with id = %hd (rsrcID = %hd, index = %hd) is out of index\n", id, rsrcID, index);
  41.         ProgramBreak(gEmptyString);
  42.     }
  43. #endif
  44. }
  45.  
  46. void VersionToString(long version, CStr255 &text)
  47. {
  48.     NumToString((version >> 8) & 0xFFFFFF, text);
  49.     CStr255 s;
  50.     NumToString((version >> 4) & 0xF, s);
  51.     text += "." + s;
  52.     NumToString(version & 0xF, s);
  53.     text += "." + s;
  54. }
  55.  
  56. short CStr255CharPos(const CStr255 &s, char ch)
  57. {
  58.     unsigned char uch = (unsigned char)ch;
  59.     for (short i = 1; i <= s.Length(); ++i)
  60.         if (s[i] == uch)
  61.             return i;
  62.     return 0;
  63. }
  64.  
  65. Boolean CStr255HasChar(const CStr255 &s, char ch)
  66. {
  67.     if (!s.Length())
  68.         return false;
  69.     const unsigned char *p = (const unsigned char*)&s;
  70.     unsigned char uch = (unsigned char)ch;
  71.     for (short i = s.Length(); i; --i)
  72.         if (*p++ == uch)
  73.             return true;
  74.     return false;
  75. }
  76.  
  77. short CStr255CharPos(const CStr255 &s, char ch, short maxPos)
  78. {
  79.     unsigned char uch = (unsigned char)ch;
  80.     short mmax = short(Min(maxPos, s.Length()));
  81.     for (short i = 1; i <= mmax; ++i)
  82.         if (s[i] == uch)
  83.             return i;
  84.     return 0;
  85. }
  86.  
  87. short CStr255CharPosBackwards(const CStr255 &s, char ch, short startPos)
  88. {
  89.     unsigned char uch = (unsigned char)ch;
  90.     short mmax = short(Min(startPos, s.Length()));
  91.     for (short i = mmax; i; --i)
  92.         if (s[i] == uch)
  93.             return i;
  94.     return 0;
  95. }
  96.  
  97. short StrCmp(const CStr255 &item1, const CStr255 &item2) // see UList.h for return value
  98. {
  99.     short i = 1;
  100.     short maxI = short(Min(item1.Length(), item2.Length()));
  101.     while (i <= maxI)
  102.     {
  103.         unsigned char ch1 = item1[i];
  104.         unsigned char ch2 = item2[i];
  105.         if (ch1 < ch2)
  106.             return kItem1LessThanItem2;
  107.         else if (ch1 > ch2)
  108.             return kItem1GreaterThanItem2;
  109.         ++i;
  110.     }
  111.     // got past one of the strings
  112.     if (item1.Length() == item2.Length())
  113.         return kItem1EqualItem2;
  114.     --i; // point at last common char
  115.     if (i < item1.Length()) // not past end of string
  116.         return kItem1GreaterThanItem2;
  117. #if qDebug
  118.     if (i == item2.Length())
  119.     {
  120.         fprintf(stderr, "Bad stop in StrCmp: item1= '%s', item2 = '%s'\n", (char*)item1, (char*)item2);
  121.         ProgramBreak(gEmptyString);
  122.     }
  123. #endif
  124.     return kItem1LessThanItem2;
  125. }
  126.  
  127. Boolean StrEqual(const CStr255 &item1, const CStr255 &item2)
  128. {
  129.     const unsigned char *p1 = (const unsigned char *)&item1;
  130.     const unsigned char *p2 = (const unsigned char *)&item2;
  131.     if (*p1++ != *p2++) /// compare length
  132.         return false;
  133.     for (short i = item1.Length(); i; --i)
  134.         if (*p1++ != *p2++)
  135.             return false;
  136.     return true;
  137. }
  138.  
  139. void CopyDynamicArray(TDynamicArray *from, TDynamicArray *to)
  140. {
  141. #if qDebug
  142.     if (!IsObject(from))
  143.         ProgramBreak("from is not object");
  144.     if (!IsObject(to))
  145.         ProgramBreak("to is not object");
  146.     if (from->fElementSize != to->fElementSize)
  147.         ProgramBreak("from->fElementSize != to->fElementSize");
  148. #endif
  149.     long noElem = from->GetSize();
  150.     to->SetArraySize(noElem);
  151.     to->fSize = noElem;
  152.     if (noElem)
  153.     {
  154.         Ptr p1 = from->ComputeAddress(1);
  155.         Ptr p2 = to->ComputeAddress(1);
  156.         BytesMove(p1, p2, noElem * from->fElementSize);
  157.     }
  158. }
  159.  
  160. short SubstituteOneStringItem(CStr255 &string, const char *marker, const CStr255 &substString)
  161. {
  162.     short pos = string.Pos(marker);
  163.     if (pos)
  164.     {
  165.         string.Delete(pos, strlen(marker));
  166.         string.Insert(substString, pos);
  167.     }
  168.     return pos;
  169. }
  170.  
  171. short SubstituteStringItems(CStr255 &string, const CStr255 &marker, const CStr255 &substString)
  172. {
  173.     short pos = string.Pos(marker);
  174.     short pos1 = pos;
  175.     while (pos)
  176.     {
  177.         string.Delete(pos, marker.Length());
  178.         string.Insert(substString, pos);
  179.         pos = string.Pos(marker);
  180.     }
  181.     return pos1;
  182. }
  183.  
  184. short SubstituteStringItems(CStr255 &string, const char *marker, const CStr255 &substString)
  185. {
  186.     short pos = string.Pos(marker);
  187.     short pos1 = pos;
  188.     while (pos)
  189.     {
  190.         string.Delete(pos, strlen(marker));
  191.         string.Insert(substString, pos);
  192.         pos = string.Pos(marker);
  193.     }
  194.     return pos1;
  195. }
  196.  
  197. short SubstituteStringItems(CStr255 &string, const char *marker, const char *substString)
  198. {
  199.     short pos = string.Pos(marker);
  200.     short pos1 = pos;
  201.     while (pos)
  202.     {
  203.         string.Delete(pos, strlen(marker));
  204.         string.Insert(substString, pos);
  205.         pos = string.Pos(marker);
  206.     }
  207.     return pos1;
  208. }
  209.  
  210. short SubstituteStringItems(CStr255 &string, const char *marker, long number)
  211. {
  212.     CStr255 s;
  213.     NumToString(number, s);
  214.     return SubstituteStringItems(string, marker, s);
  215. }
  216.  
  217. short SubstituteStringItems(CStr255 &string, const char *marker, short number)
  218. {
  219.     return SubstituteStringItems(string, marker, long(number));
  220. }
  221.  
  222. #if qDebug
  223. void BytesMove(const void *src, void *dest, long size)
  224. {
  225.     if (size < 0)
  226.     {
  227.         ProgramBreak("BytesMove: size < 0");
  228.         return;
  229.     }
  230.     memcpy(dest, src, (unsigned int)size);
  231. }
  232. #endif
  233.  
  234. #if qDebug
  235. void MyBlockMove(const void *src, void *dest, long size)
  236. {
  237.     if (size < 0)
  238.     {
  239.         ProgramBreak("MyBlockMove: size < 0");
  240.         return;
  241.     }
  242.     memmove(dest, src, (unsigned int)size);
  243. }
  244. #endif
  245.  
  246. void CopyCString2String(const CStr255 &src, StringPtr dest)
  247. {
  248.     const unsigned char *ucp = (const unsigned char*) &src;
  249.     BytesMove(ucp, dest, *ucp + 1);
  250. }
  251.  
  252. void AppendStringToHandle(const CStr255 &text, Handle h)
  253. {
  254.     if (text.Length())
  255.     {
  256.         long oldSize = GetHandleSize(h);
  257.         SetPermHandleSize(h, oldSize + text.Length());
  258.         const unsigned char *ucp = (const unsigned char*) &text;
  259.         BytesMove(ucp + 1, *h + oldSize, text.Length());
  260.     }
  261. }
  262.  
  263. void AppendStringToHandle(const char *p, Handle h)
  264. {
  265.     long textLen = strlen(p);
  266.     if (textLen)
  267.     {
  268.         long oldSize = GetHandleSize(h);
  269.         SetPermHandleSize(h, oldSize + textLen);
  270.         BytesMove(p, *h + oldSize, textLen);
  271.     }
  272. }
  273.  
  274. void Long2Hex(long l, CStr255 &s)
  275. {
  276.     s.Length() = 8;
  277.     for (short i = 8; i > 0; i--)
  278.     {
  279.         s[i] = "0123456789ABCDEF"[l & 15];
  280.         l >>= 4;
  281.     }
  282. }
  283.  
  284. const char *OSType2String(OSType ot)
  285. {
  286.     const short kTempCStrings = 4;
  287.     static short currentCString = 0;
  288.     static char cStrings[kTempCStrings][8];
  289.     currentCString = (currentCString + 1) % kTempCStrings;
  290.     char *p = cStrings[currentCString];
  291.     *(long*)p = ot;
  292.     p[5] = 0;
  293.     return p;
  294. }
  295.  
  296.