home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 14 / MA_Cover_14.iso / source / c / pegase_src / amigaos.cpp next >
Encoding:
C/C++ Source or Header  |  1999-06-14  |  7.5 KB  |  251 lines

  1. /*
  2. **
  3. ** AmigaOS.cpp
  4. **
  5. ** (c) 1998 Didier Levet
  6. **
  7. ** Fonctions AmigaOS
  8. **
  9. ** $Revision: 1.1 $
  10. ** $State: Exp $
  11. ** $Date: 1998/11/14 18:23:45 $
  12. **
  13. ** $Log: AmigaOS.cpp $
  14. ** Revision 1.1  1998/11/14 18:23:45  kakace
  15. ** Initial revision
  16. **
  17. **
  18. */
  19.  
  20. #ifndef  _AMIGAOS_HPP
  21. #include "AmigaOS.hpp"
  22. #endif
  23.  
  24.  
  25. //----------------------------------------------------------------------------------------------------
  26. //============================================= ExecLib ==============================================
  27. //----------------------------------------------------------------------------------------------------
  28.  
  29. //==================================================================================================
  30. //
  31. //   SYNOPSIS                                                                      ExecLib::RawDoFmt
  32. //   string = ExecLib::RawDoFmt(patternString, dataStream)
  33. //   STRING ExecLib::RawDoFmt(STRING, void *);
  34. //
  35. //   FUNCTION
  36. //      Output several strings using a format string in a buffer, then returns the resulting string.
  37. //
  38. //   INPUTS
  39. //   patternString - Pattern string.
  40. //   dataStream    - Pointer to the arguments.
  41. //
  42. //   RESULT
  43. //   string - Resulting string.
  44. //
  45. //   NOTES
  46. //      This function check the buffer size before outputing any character. Thus, the resulting
  47. //      string might be shorter than the one expected.
  48. //
  49. //   SEE ALSO
  50. //
  51. //   HISTORY
  52. //
  53. //==================================================================================================
  54. /// ExecLib::RawDoFmt()
  55.  
  56. char  ExecLib::fmt_buffer[350];
  57. char *ExecLib::fmt_ptr;
  58.  
  59. STRING ExecLib::RawDoFmt(STRING patternString, void *dataStream)
  60. {
  61.     fmt_buffer[sizeof(fmt_buffer) - 1] = '\0';      // Initialize the last character.
  62.     fmt_ptr = fmt_buffer;                           // Initialize the output pointer.
  63.  
  64.     ::RawDoFmt((UBYTE *) patternString, dataStream, (void (*)()) PutCharProc, NULL);
  65.  
  66.     return fmt_buffer;
  67. }
  68.  
  69.  
  70. // ExecLib::PutCharProc()  [private]
  71.  
  72. void ExecLib::PutCharProc(register __d0 char ch)
  73. {
  74.     if (fmt_ptr - fmt_buffer < sizeof(fmt_buffer) - 1)
  75.     {
  76.         *fmt_ptr++ = ch;
  77.     }
  78. }
  79. ///
  80.  
  81.  
  82. //----------------------------------------------------------------------------------------------------
  83. //============================================== DOSLib ==============================================
  84. //----------------------------------------------------------------------------------------------------
  85. //
  86. //  Most of the following functions take advantage of the extension feature of the CString objects.
  87. //  Thus, we don't have to care about buffers size.
  88.  
  89. /// DOSLib::AddPart()
  90. //----------------------------------------------------------------------------------------------------
  91. //
  92. //  Add a file name to the path given. This function is able to enlarge the path name buffer when
  93. //  necessary.
  94. //  Returns FALSE if ::AddPart() fails and if the buffer can't be enlarged (memory error).
  95.  
  96. int  DOSLib::AddPart(CString &dirName, STRING fileName)
  97. {
  98.     ULONG buff_size = dirName.extend(256);
  99.     int   result = buff_size;                   // == FALSE if buff_size == 0
  100.  
  101.     if (fileName != NULL && buff_size > 0)
  102.     {
  103.         while ( (result = ::AddPart(dirName, (STRPTR) fileName, buff_size)) == FALSE)
  104.         {
  105.             buff_size += 256;
  106.             if (dirName.extend(buff_size) < buff_size)
  107.             {
  108.                 ::SetIoErr(ERROR_NO_FREE_STORE);
  109.                 break;
  110.             }
  111.         }
  112.     }
  113.  
  114.     dirName.update();                           // Update the string's size.
  115.     return result;
  116. }
  117. ///
  118. /// DOSLib::GetProgramName()
  119. //----------------------------------------------------------------------------------------------------
  120. int  DOSLib::GetProgramName(CString& progName)
  121. {
  122.     return AutoExtend(0, progName, 32, &DOSLib::GetProgramName);
  123. }
  124. ///
  125. /// DOSLib::NameFromLock()
  126. //----------------------------------------------------------------------------------------------------
  127. int  DOSLib::NameFromLock(BPTR lock, CString &buffer)
  128. {
  129.     return AutoExtend(lock, buffer, 256, &::NameFromLock);
  130. }
  131. ///
  132. /// DOSLib::NameFromFH()
  133. //----------------------------------------------------------------------------------------------------
  134. int  DOSLib::NameFromFH(BPTR fh, CString &buffer)
  135. {
  136.     return AutoExtend(fh, buffer, 256, &::NameFromFH);
  137. }
  138. ///
  139. /// DOSLib::MatchPatternNoCase()
  140. //----------------------------------------------------------------------------------------------------
  141.  
  142. int  DOSLib::MatchPatternNoCase(const CString& pattern, STRING str)
  143. {
  144.     CString buffer(NULL, pattern.length() * 2 + 2);
  145.     int  result = FALSE;
  146.  
  147.     if (buffer != CString::UNUSED && ::ParsePatternNoCase(pattern, buffer, buffer.buffsize()) >= 0)
  148.     {
  149.         result = ::MatchPatternNoCase(buffer, (STRPTR) str);
  150.     }
  151.  
  152.     return result;
  153. }
  154. ///
  155.  
  156. /// DOSLib::AutoExtend()            [PRIVATE]
  157. //----------------------------------------------------------------------------------------------------
  158. //
  159. //  This function is used to call a DOS function that put some datas into a buffer. The buffer is
  160. //  automatically enlarged when necessary.
  161. //  Only returns TRUE when the datas have been copied successfully.
  162.  
  163. int  DOSLib::AutoExtend(BPTR handle, CString &buffer, ULONG step, LONG (*func)(BPTR, STRPTR, LONG))
  164. {
  165.     ULONG buff_size = buffer.extend(step);
  166.     int  result = FALSE;
  167.  
  168.     if (buff_size > 0)
  169.     {
  170.         while ( (result = func(handle, buffer, buff_size)) != FALSE
  171.                && DOSLib::IoErr() == ERROR_LINE_TOO_LONG)
  172.         {
  173.             buff_size += step;
  174.             if (buffer.extend(buff_size) < buff_size)
  175.             {
  176.                 ::SetIoErr(ERROR_NO_FREE_STORE);
  177.                 result = FALSE;
  178.                 break;
  179.             }
  180.         }
  181.     }
  182.  
  183.     buffer.update();                            // Update the string's size.
  184.     return result;
  185. }
  186. ///
  187.  
  188. //----------------------------------------------------------------------------------------------------
  189. //============================================== CMatch ==============================================
  190. //----------------------------------------------------------------------------------------------------
  191.  
  192. /// CMatch::First()
  193. LONG CMatch::First(STRING pattern, AnchorPath *anchor)
  194. {
  195.     LONG result = ERROR_NO_FREE_STORE;
  196.  
  197.     if (anchor == NULL)
  198.     {
  199.         anchor = new AnchorPath;
  200.     }
  201.  
  202.     if (anchor != NULL)
  203.     {
  204.         result = ::MatchFirst( (STRPTR) pattern, anchor);
  205.         pAnchor = anchor;
  206.         CloseMe = TRUE;
  207.     }
  208.  
  209.     return result;
  210. }
  211. ///
  212.  
  213.  
  214. //----------------------------------------------------------------------------------------------------
  215. //=========================================== CDiskObject ============================================
  216. //----------------------------------------------------------------------------------------------------
  217.  
  218. /// CDiskObject::Load()
  219. DiskObject *CDiskObject::Load(STRING name)
  220. {
  221.     CString name_buffer;
  222.     BPTR old_dir_lock;
  223.  
  224.     STRING prog_name = name;
  225.  
  226.     // As ToolTypes may be parsed when we start from either CLI or Workbench, we must handle all
  227.     // cases. Thus, we set the current directory to the one where the icon should be, then we
  228.     // get the program name which will be used as the icon's base name.
  229.  
  230.     old_dir_lock = DOSLib::CurrentDir(DOSLib::GetProgramDir());
  231.  
  232.     if (prog_name == NULL)
  233.     {
  234.         if (DOSLib::GetProgramName(name_buffer) != FALSE)
  235.         {
  236.             prog_name = name_buffer;
  237.         }
  238.     }
  239.  
  240.     if (prog_name != NULL)
  241.     {
  242.         release();                      // Releases the current DiskObject, if any.
  243.         dobj = ::GetDiskObject( (UBYTE *) prog_name);
  244.     }
  245.  
  246.     DOSLib::CurrentDir(old_dir_lock);
  247.     return dobj;
  248. }
  249. ///
  250.  
  251.