home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Internet / Web / URLScan.lha / urlscan.c < prev    next >
C/C++ Source or Header  |  1998-04-24  |  3KB  |  133 lines

  1. /**
  2.  * Reads a text file or standard input, and extracts any urls in the text.
  3.  * Any urls found will be copied to the clipboard.  Uses some support code
  4.  * for clipboard use from the DevCD, which is included.  Written in mostly
  5.  * ANSI C, with the only exceptions being the AmigaOS clipboard calls.
  6.  *
  7.  * This code is copyright (c) 1998 to Matthew Hunter.  It's my code.  Don't
  8.  * sell it, don't claim you wrote it.  You can distribute it on PD collections
  9.  * such as Aminet CDs and charge a reasonable cost for those.  This license
  10.  * has nothing to do with the source for the support code from the DevCD, see
  11.  * cb.h for that.
  12.  *
  13.  * If you change this code, it would be nice of you to send me your changes.
  14.  *
  15.  * Assume I included the usual legal mummery: I am not responsible if you
  16.  * die while using this code, or even if your computer dies.  It works for me.
  17.  * If it doesn't work for you, blame the phases of the moon or the configurations
  18.  * of the stars, but don't blame me.
  19.  **/
  20.  
  21. #include <exec/types.h>
  22. #include <exec/ports.h>
  23. #include <exec/io.h>
  24. #include <exec/memory.h>
  25. #include <devices/clipboard.h>
  26. #include <libraries/dosextens.h>
  27. #include <libraries/dos.h>
  28.  
  29. #include "cb.h"
  30.  
  31. #include <clib/dos_protos.h>
  32. #include <clib/exec_protos.h>
  33. #include <clib/alib_protos.h>
  34.  
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38.  
  39.  
  40. const static char VersTag[] = "\0$VER: URLScan 1.0 (24.4.1998)";
  41.  
  42. void striplf(char *line)
  43. {
  44.    for (; *line != NULL; *line++)
  45.    {
  46.       if ((*line == 10) || (*line == 13))
  47.       {
  48.          *line = NULL;
  49.          return;
  50.       }
  51.    }
  52. }
  53.  
  54. void cleanurl(char *line)
  55. {
  56.    int count;
  57.    for (count = 0; line[count] != NULL; count++)
  58.    {
  59.       if (line[count] == ' ')
  60.       {
  61.          if (line[count-1] == '.') line[count - 1] = NULL;
  62.          else line[count] = NULL;
  63.          return;
  64.       }
  65.       if ((line[count] == ',') || (line[count] == '>') || (line[count] == '"'))
  66.       {
  67.          line[count] = NULL;
  68.          return;
  69.       }
  70.    }
  71. }
  72.  
  73. void main(int argc, char *argv[])
  74. {
  75.    char buffer[256] = {""};
  76.    char *temp;
  77.    FILE *input = NULL;
  78.    ULONG count = 0;
  79.  
  80.    if (argc == 1)
  81.    {
  82.       // stdio processing
  83.       input = stdin;
  84.    }
  85.    else if (argc == 2)
  86.    {
  87.       // File-based processing
  88.       if ((input = fopen(argv[1], "r")) == NULL)
  89.       {
  90.          printf("Couldn't open input file: %s", argv[1]);
  91.          exit(0);
  92.       }
  93.    }
  94.    else exit(0);
  95.  
  96.    while (fgets(buffer, 256, input) != NULL)
  97.    {
  98.       if ((temp = strstr(buffer, "http://")) != NULL)
  99.       {
  100.          striplf(temp);
  101.          cleanurl(temp);
  102.          strcpy(buffer, temp);
  103.       }
  104.       else buffer[0] = NULL;
  105.  
  106.       if (buffer[0] != NULL)
  107.       {
  108.          struct IOClipReq *Clip;
  109.  
  110.          if ((Clip = CBOpen(0)) != NULL)
  111.          {
  112.             if (CBWriteFTXT(Clip, buffer) == TRUE)
  113.             {
  114.                printf("Unit %d: %s\n", count, buffer);
  115.                count = count + 1;
  116.             } else printf("Couldn't add to clipboard.");
  117.          } else printf("Couldn't open clipboard device");
  118.          CBClose(Clip);
  119.  
  120.          /**
  121.           * This delay needed so clipboard-history programs have a chance to copy
  122.           * information between different clipboard units.
  123.           **/
  124.          Delay(3);
  125.       }
  126.    }
  127.  
  128.    if (argc == 2)
  129.    {
  130.       fclose(input);
  131.    }
  132. }
  133.