home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / monitors / rsys / source.lha / src / RSysExtern.c < prev    next >
C/C++ Source or Header  |  1995-01-09  |  11KB  |  422 lines

  1. /*
  2. ***************************************************************************
  3. *
  4. * Datei:
  5. *    RSysExtern.c
  6. *
  7. * Inhalt:
  8. *    int GetFile(struct Window *wind, char *dir, char *file, char *pattern, char *title, char *oktext);
  9. *    void SaveList(struct Window *wind, char *filename, char *title, struct List *list, int withheader);
  10. *    int SysStarted(int type);
  11. *    void RemoveSpeech(void);
  12. *    int InitSpeech(void);
  13. *    void Speak(char *text);
  14. *
  15. * Bemerkungen:
  16. *    Verwaltung externer Daten und Schnittstellen zu Dateien.
  17. *
  18. * Erstellungsdatum:
  19. *    07-Jul-93    Rolf Böhme
  20. *
  21. * Änderungen:
  22. *    07-Jul-93    Rolf Böhme    Erstellung
  23. *
  24. ***************************************************************************
  25. */
  26.  
  27. #include "RSysFunc.h"
  28. #include "RSysDebug.h"
  29.  
  30. char _file[PATHPARTWIDTH],_dir[MAXFULLNAME];
  31.  
  32. static char *
  33. strip(char *src, char *dest)
  34. {
  35.    BYTE s = 0, d = 0, l = strlen(src);
  36.  
  37.    while(s < l)
  38.    {
  39.       if(!strchr("'-:/ ", (int)src[s]))
  40.       {
  41.          dest[d++] = src[s++];
  42.       }
  43.       else
  44.          s++;
  45.    }
  46.  
  47.    dest[d] = STRINGEND;
  48.  
  49.    return dest;
  50. }
  51.  
  52.  
  53.    /*
  54.     * GetFile() bietet einen Filerequester zur Auswahl einer Datei
  55.     * an. Dabei wird unterschieden, ob die Datei zum Sichern oder zum
  56.     * Laden verwendet wird.
  57.     */
  58. int
  59. GetFile(struct Window *wind,char *dir,char *file,
  60.         char *pattern,char *title,char *oktext)
  61. {
  62.    struct FileRequester *AslFileRequest = NULL;
  63.    BYTE  Result = FALSE;
  64.    APTR  req;
  65.    UWORD l = 20,t = 20;
  66.    int mask = FILF_PATGAD;
  67.    char _oktext[MAXSHOW],_title[40],_pattern[10] = "#?",*_cancel = "Cancel";
  68.  
  69.    DPOS;
  70.  
  71.    if(SysWnd)
  72.       PrintInfo("Selecting file", NO_SPEAK, 0);
  73.  
  74.    if (Flags.speakmode)
  75.       Speak(title);
  76.  
  77.    if (AslBase = OpenLibrary((UBYTE *) "asl.library", 37L))
  78.    {
  79.       if(wind)
  80.       {
  81.          req = LockWindow(wind);
  82.          CenterWindow(wind->WScreen,&t,&l,(UWORD)260,(UWORD)170);
  83.       }
  84.  
  85.       if(dir && *dir)
  86.          strncpy(_dir,dir,MAXFULLNAME);
  87.       else
  88.          strcpy(_dir,"RAM:");
  89.  
  90.       if(file && *file)
  91.       {
  92.          char header[PATHPARTWIDTH];
  93.  
  94.          strip(file, header);
  95.  
  96.          strncpy(_file,header,PATHPARTWIDTH);
  97.       }
  98.       else
  99.          strcpy(_file,"unknown");
  100.  
  101.       if(pattern && *pattern)
  102.          strncpy(_pattern,pattern,10);
  103.       else
  104.          strcpy(_pattern,"#?");
  105.  
  106.       if(title && *title)
  107.          strncpy(_title,title,40);
  108.       else
  109.          strcpy(_title,"Select file");
  110.  
  111.       if(oktext && *oktext)
  112.          strncpy(_oktext,oktext,MAXSHOW);
  113.       else
  114.          strcpy(_oktext,"Select");
  115.  
  116.       if(strstr(_oktext,"Save"))
  117.          mask |= FILF_SAVE;
  118.  
  119.       if (AslFileRequest = AllocAslRequest(ASL_FileRequest,
  120.                               TAG_DONE))
  121.       {
  122.          if (AslRequestTags(AslFileRequest,
  123.                               ASL_Window,     (ULONG)wind,
  124.                               ASL_Hail,       (ULONG)_title,
  125. /*
  126.                               ASL_Height,     (ULONG)170,
  127.                               ASL_Width,      (ULONG)260,
  128. */
  129.                               ASL_LeftEdge,   (ULONG)l,
  130.                               ASL_TopEdge,    (ULONG)t,
  131.                               ASL_OKText,     (ULONG)_oktext,
  132.                               ASL_CancelText, (ULONG)_cancel,
  133.                               ASL_File,       (ULONG)_file,
  134.                               ASL_Dir,        (ULONG)_dir,
  135.                               ASL_Pattern,    (ULONG)_pattern,
  136.                               ASL_FuncFlags,  (ULONG)mask,
  137.                         TAG_DONE))
  138.          {
  139.             if (AslFileRequest->rf_File)
  140.             {
  141.                if(AslFileRequest->rf_Dir)
  142.                {
  143.                   strncpy(_fullpath,AslFileRequest->rf_Dir,MAXFULLNAME);
  144.                   AddPart((UBYTE *)_fullpath,(UBYTE *)AslFileRequest->rf_File,
  145.                           MAXFULLNAME);
  146.                }
  147.                else
  148.                   strncpy(_fullpath,AslFileRequest->rf_File,MAXFULLNAME);
  149.  
  150.                Result = TRUE;
  151.             }
  152.  
  153.             if(AslFileRequest->rf_Pat)
  154.                strncpy(_pattern,AslFileRequest->rf_Pat,10);
  155.          }
  156.  
  157.          FreeAslRequest(AslFileRequest);
  158.       }
  159.       else
  160.          ErrorHandle(MEMORY_ERR, ALLOC_FAIL, NO_KILL);
  161.  
  162.       CloseLibrary(AslBase);
  163.  
  164.       if(wind)
  165.          UnlockWindow(req);
  166.    }
  167.    else
  168.       ErrorHandle(LIBRARY_ERR, OPEN_FAIL, NO_KILL);
  169.  
  170.    if (SysWnd && strcmp(_oktext, "Check"))
  171.       PrintStatistics();
  172.  
  173.    DPOS;
  174.  
  175.    return (Result);
  176. }
  177.  
  178.    /*
  179.     * SaveList() speichert die Einträge einer Liste (vom Hauptfenster oder
  180.     * intern) in eine Datei ab oder direkt ins Clipboard.
  181.     */
  182. void
  183. SaveList(struct Window *wind, char *filename, char *title,
  184.          struct List *list, int withheader)
  185. {
  186.    BPTR  fout;
  187.    struct Node *node;
  188.    int   mode;
  189.    char  out[MAXWRITESIZE], fmt[MAXWRITESIZE];
  190.    int noheader;
  191.  
  192.    DPOS;
  193.  
  194.    PrintInfo("Save list to file", NO_SPEAK, 0);
  195.  
  196.    if (exist(filename))
  197.    {
  198.       mode = MyEasyRequest(wind, (UBYTE *) NAME " ask you",
  199.                            (UBYTE *) "Append|Overwrite|Cancel",
  200.                            (UBYTE *) "The file <%s> exists!",
  201.                            filename);
  202.       switch (mode)
  203.       {
  204.          case 1:
  205.             fout = Open((UBYTE *) filename, MODE_OLDFILE);
  206.             Seek(fout, 0L, OFFSET_END);
  207.             break;
  208.  
  209.          case 2:
  210.             fout = Open((UBYTE *) filename, MODE_NEWFILE);
  211.             break;
  212.  
  213.          default:
  214.             return;
  215.             break;
  216.       }
  217.    }
  218.    else
  219.       fout = Open((UBYTE *) filename, MODE_NEWFILE);
  220.  
  221.    D(kprintf((UBYTE *)"1. Format: %s\nIO-Error: %ld\n",fmt, IoErr()));
  222.  
  223.    if (fout)
  224.    {
  225.       noheader = RSysFormatOutput(fmt);
  226.  
  227.       if(!noheader)
  228.       {
  229.          sprintf(out, "----------------------------------------------\n"
  230.                  "%s\n"
  231.                  "----------------------------------------------\n\n",
  232.                  title);
  233.  
  234.          Write(fout, out, strlen(out));
  235.  
  236.          if (withheader)
  237.          {
  238.             sprintf(out, "%s\n----------------------------------------------\n",
  239.                     EntryAttr[LastID].ea_header);
  240.             Write(fout, out, strlen(out));
  241.          }
  242.       }
  243.  
  244.       for (node = list->lh_Head; node->ln_Succ /*&& !IoErr()*/;
  245.            node = node->ln_Succ)
  246.       {
  247.          sprintf(out, fmt, node->ln_Name);
  248.          strcat((char *)out,"\n");
  249.  
  250.          Write(fout, out, strlen(out));
  251.       }
  252.  
  253.       D(kprintf((UBYTE *)"Format: %s\nIO-Error: %ld\n",fmt, IoErr()));
  254.  
  255.       if (IoErr())
  256.          ErrorHandle(FILE_ERR, WRITE_FAIL, NO_KILL);
  257.       else
  258.       {
  259.          Write(fout, (UBYTE *)"\n",1);
  260.          PrintInfo("List was written to file", SPEAK, SEC);
  261.       }
  262.  
  263.       Close(fout);
  264.    }
  265.    else
  266.       ErrorHandle(FILE_ERR, OPEN_FAIL, NO_KILL);
  267.  
  268.    PrintStatistics();
  269.  
  270.    return;
  271. }
  272.  
  273.    /*
  274.     * SysStarted() überprüft, ob RSys ein zweites Mal gestartet wurde
  275.     * und sendet im zutreffenden Fall einen Argumenttyp der Art der
  276.     * anzuzeigenden Liste an den Task des zuerst gestarteten Programmes
  277.     */
  278. int
  279. SysStarted(int type)
  280. {
  281.    struct MsgPort *TestPort;
  282.    struct MsgPort *ReplyPort = NULL;
  283.    SysMsg *mess;
  284.    int   ret = FALSE;
  285.  
  286.    DPOS;
  287.  
  288.    if (ReplyPort = CreatePort((UBYTE *) "SysReplyPort", NULL))
  289.    {
  290.       mess = (SysMsg *) MyAllocVec(sizeof(SysMsg),
  291.                                    MEMF_CLEAR | MEMF_PUBLIC, NO_KILL);
  292.  
  293.       if (mess)
  294.       {
  295.          mess->sm_message.mn_Node.ln_Type = NT_MESSAGE;
  296.          mess->sm_message.mn_ReplyPort = ReplyPort;
  297.          mess->sm_message.mn_Length = sizeof(SysMsg);
  298.          mess->sm_newtype = type;
  299.  
  300.          Forbid();
  301.  
  302.          if (TestPort = FindPort((UBYTE *) ID_PORT_NAME))
  303.          {
  304.             PutMsg(TestPort, (struct Message *) mess);
  305.  
  306.             WaitPort(ReplyPort);
  307.  
  308.             ret = TRUE;
  309.          }
  310.  
  311.          Permit();
  312.  
  313.          MyFreeVec(mess);
  314.       }
  315.  
  316.       DeletePort(ReplyPort);
  317.    }
  318.  
  319.    return (ret);
  320. }
  321.  
  322. struct Library *TranslatorBase = NULL;
  323. struct MsgPort *narratorPort;
  324. struct narrator_rb request;
  325.  
  326.    /*
  327.     * RemoveSpeech() entfernt die Ressourcen für die Sprachausgabe
  328.     * und schließt das Device, das für die Sprachausgabe des Amigas
  329.     * zuständig ist, das narrator.device
  330.     */
  331. void
  332. RemoveSpeech(void)
  333. {
  334.    DPOS;
  335.  
  336.    if (narratorPort)
  337.       DeleteMsgPort(narratorPort);      /* Port schliessen */
  338.  
  339.    if (TranslatorBase)
  340.       CloseLibrary(TranslatorBase);     /* Lib schliessen */
  341.  
  342.    if (request.message.io_Device)
  343.       CloseDevice((struct IORequest *) & request);      /* Device schliessen */
  344.  
  345.    return;
  346. }
  347.  
  348.    /*
  349.     * InitSpeech() öffnet die Ressourcen für die Sprachausgabe
  350.     * Anm: Auf einem 4000er stürzt an dieser Stelle das Programm ab!
  351.     */
  352. int
  353. InitSpeech(void)
  354. {
  355.    DPOS;
  356.  
  357.    if (TranslatorBase = OpenLibrary((UBYTE *) "translator.library", 0))
  358.       if (narratorPort = CreateMsgPort())
  359.       {
  360.          if (NOT(OpenDevice((UBYTE *) "narrator.device", 0,
  361.                             (struct IORequest *) & request, 0)))
  362.             return (TRUE);
  363.          else
  364.             ErrorHandle(DEVICE_ERR, OPEN_FAIL, NO_KILL);
  365.       }
  366.       else
  367.          ErrorHandle(PORT_ERR, CREATE_FAIL, NO_KILL);
  368.    else
  369.       ErrorHandle(LIBRARY_ERR, OPEN_FAIL, NO_KILL);
  370.  
  371.    RemoveSpeech();
  372.  
  373.    return (FALSE);
  374. }
  375.  
  376.    /*
  377.     * Speak() gibt einen Text über das narrator.device aus
  378.     */
  379. void
  380. Speak(char *text)
  381. {
  382.    UBYTE help[BUFSIZE + 2];
  383.    UBYTE translate[3 * BUFSIZE];
  384.    UBYTE Channel[4] =
  385.    {
  386.       1, 2, 4, 8
  387.    };
  388.  
  389.    DPOS;
  390.  
  391.    strncpy((char *)help, text, BUFSIZE);
  392.  
  393.    if (!ispunct((int)text[strlen(text) - 1]))
  394.       strcat((char *)help, ".");
  395.  
  396.    if (NOT(Translate(help, (strlen((char *)help) * sizeof(char)),
  397.                      translate, sizeof(translate))))
  398.    {
  399.       request.message.io_Message.mn_ReplyPort = narratorPort;
  400.  
  401.       request.message.io_Command = CMD_WRITE;   /* Kommando Schreiben */
  402.       request.message.io_Data = (APTR) translate;       /* Pointer auf Text */
  403.       request.message.io_Length = sizeof(translate);    /* Groesse des Textes */
  404.  
  405.       request.rate         = DEFRATE;   /* Worte pro Minute: 40 < rate < 400 */
  406.       request.pitch        = DEFPITCH; /* 65 < pitch < 320 */
  407.       request.sex          = DEFSEX;     /* Geschlecht : auch MALE */
  408.       request.volume       = DEFVOL;  /* Lautstaerke max 64 */
  409.       request.mode         = DEFMODE;   /* Aussprache-Modus */
  410.       request.sampfreq     = DEFFREQ;
  411.       request.F0enthusiasm = DEFF0ENTHUS;
  412.       request.F0perturb    = DEFF0PERT;
  413.       request.priority     = DEFPRIORITY;
  414.       request.ch_masks     = (UBYTE *) Channel;     /* Kanaele */
  415.       request.nm_masks     = sizeof(Channel);
  416.  
  417.       DoIO((struct IORequest *) & request);     /* Text aussprechen */
  418.    }
  419.  
  420.    return;
  421. }
  422.