home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / timslib / TimsLib / c / Lib < prev    next >
Encoding:
Text File  |  1993-01-21  |  2.8 KB  |  132 lines

  1. /* lib.c */
  2. /*                           
  3.  
  4.   #####                 #         #
  5.     #    #              #      #  #
  6.     #                   #         #
  7.     #   ##  ####   #### #     ##  ####
  8.     #    #  # # # #     #      #  #   #
  9.     #    #  # # #  ###  #      #  #   #
  10.     #    #  # # #     # #      #  #   #
  11.     #   ### # # # ####  ##### ### ####
  12.  
  13. -----------------------------------------------------------------------------
  14.  
  15. This is source for use with the 'DeskLib' Wimp C programming library for
  16. Risc OS. I currently use v1.04 of DeskLib.  This source is FreeWare, which
  17. means you can use it to write commercial applications, but you may not charge
  18. *in any way* for the distribution of this source.  I (Tim Browse) retain
  19. all copyright on this source.
  20.  
  21. This source is provided 'as is' and I offer no guarantees that is useful,
  22. bug-free, commented, that it will compile, or even that it exists.
  23.  
  24. If it breaks in half, you own both pieces.
  25.  
  26. All source © Tim Browse 1993
  27.  
  28. -----------------------------------------------------------------------------
  29.  
  30. */
  31.  
  32. /* ANSI includes */
  33. #include "stdlib.h"
  34. #include "string.h"      
  35.  
  36. /* RISC_OSLib includes */
  37. #include "os.h"
  38. #include "swis.h"
  39.                    
  40. /* DeskLib includes */
  41. #include "DeskLib.Core.h"
  42. #include "DeskLib.Error.h"
  43.  
  44. /* Timslib includes */
  45. #include "Lib.h"
  46.  
  47. char *save_str(const char *src, int length)
  48. {
  49.   int   len = length;
  50.   char *dest;
  51.  
  52.   if (src == NULL)
  53.     return NULL;
  54.  
  55.   if (len == 0)
  56.     len = strlen_cr(src) + 1;
  57.  
  58.   dest = (char *) malloc(len);
  59.  
  60.   if (dest == NULL)
  61.     return NULL;
  62.  
  63.   memcpy(dest, src, len);
  64.  
  65.   /* make sure string is zero terminated */
  66.   dest[len-1] = (char) 0;
  67.  
  68.   return dest;
  69. }
  70.  
  71.  
  72. int set_bits(int src, int mask, int set_bits)
  73. {
  74.   return ((src & ~mask) | set_bits);
  75. }
  76.  
  77. void asciiz_to_cr(char *s, int max_len)
  78. {
  79.   int i = 0;
  80.  
  81.   while ((i < max_len) && (s[i] != '\0'))
  82.     i++;
  83.  
  84.   if (i < max_len)
  85.     s[i] = '\15'; /* CR */
  86. }
  87.  
  88. int strlen_cr(const char *s)
  89. {
  90.   char *p = (char *) s;
  91.  
  92.   /* Look for a Null or CR character... */
  93.   while ((*p != '\0') && (*p != '\15'))
  94.     p++;
  95.  
  96.   return (p-s);
  97. }
  98.  
  99.  
  100. typedef struct
  101. {
  102.   int load_addr, exec_addr, length, fileattr, type;
  103.   char name[11];
  104. } FILE_INFO;
  105.  
  106.  
  107. int get_filetype(char *dirname, char *leafname)
  108. {
  109.   os_gbpbstr gbpb;
  110.   FILE_INFO file_info;
  111.  
  112.   /* Read file info */
  113.   gbpb.action      = 10;
  114.   gbpb.file_handle = (int) dirname;
  115.   gbpb.data_addr   = (void *) &file_info; /* Space for a full entry */
  116.   gbpb.seq_point   = 0;                   /* Start at beginning of directory */
  117.   gbpb.buf_len     = sizeof(file_info);
  118.   gbpb.wild_fld    = leafname;
  119.  
  120.   do
  121.   {
  122.     gbpb.number = 1; /* Only need one entry to be read */
  123.     Error_Check(os_gbpb(&gbpb));
  124.   }
  125.   while ((gbpb.number == 0) && (gbpb.seq_point != -1));
  126.  
  127.   if (gbpb.number == 0)
  128.     return -1;
  129.   else
  130.     return ((file_info.load_addr & 0xFFF00) >> 8);
  131. }
  132.