home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Applications / Nuntius 1.2 / src / MyToolSource / MakeFinderAlias.cp < prev    next >
Encoding:
Text File  |  1993-12-30  |  3.0 KB  |  126 lines  |  [TEXT/MPS ]

  1.  
  2. #include <Memory.h>
  3. #include <StdIO.h>
  4. #include <stream.h>
  5. #include <Types.h>
  6. #include <Strings.h>
  7. #include <StdLib.h>
  8. #include <PLStringFuncs.h>
  9. #include <Aliases.h>
  10. #include <Resources.h>
  11. #include <Errors.h>
  12. #include <ToolUtils.h>
  13.  
  14. Str31 gTheZone, gTheServer;
  15. Str255 gFullpath;
  16. FSSpec gAliasFileSpec;
  17. short gAliasFileRefnum = -1;
  18. AliasHandle gTargetAliasH = nil;
  19.  
  20. void PrintUsage()
  21. {
  22.     cerr << "### MakeFinderAlias - bad or missing parameter\n";
  23.     cerr << "# Usage - PLookup [-z zone] [-t type] gFullpath alias-file\n";
  24.     exit(1);
  25. }
  26.  
  27. void FailOSErr(OSErr err, const char *msg)
  28. {
  29.     if (err == noErr)
  30.         return;
  31.     cerr << "### MakeFinderAlias, error at " << msg << "\n";
  32.     cerr << "GetErrorText " << err;
  33.     exit(1);
  34. }
  35.  
  36. void FailNIL(void *p)
  37. {
  38.     if (!p)
  39.         FailOSErr(-108, "??");
  40. }
  41.  
  42. void DoMakeTargetAlias(const char *gFullpath)
  43. {
  44.     FailOSErr(NewAliasMinimalFromFullpath(short(strlen(gFullpath)),
  45.                                             (const unsigned char*)gFullpath, gTheZone, gTheServer, &gTargetAliasH),
  46.                                             "NewAlias of gFullpath");
  47.     FailNIL(gTargetAliasH);
  48. }
  49.  
  50. void DoMakeAliasFile(const char *aliasfilename)
  51. {
  52.     AliasHandle aliasH = nil;
  53.     FailOSErr(NewAliasMinimalFromFullpath(short(strlen(aliasfilename)),
  54.                                             (const unsigned char*)aliasfilename, "\p", "\p", &aliasH),
  55.                                             "NewAlias of alias-file");
  56.     FailNIL(aliasH);
  57.     Boolean wasChanged;
  58.     OSErr err = ResolveAlias(nil, aliasH, &gAliasFileSpec, &wasChanged);
  59.     if (err != fnfErr)
  60.         FailOSErr(err, "Resolve alias-file alias");
  61.     FSpCreateResFile(&gAliasFileSpec, 'MPS ', 'TEXT', 0);
  62.     FailOSErr(ResError(), "FSpCreate");
  63.     gAliasFileRefnum = FSpOpenResFile(&gAliasFileSpec, fsRdWrPerm);
  64.     if (gAliasFileRefnum == -1)
  65.     {
  66.         FailOSErr(ResError(), "FSpOpenResFile of alias-file");
  67.         FailOSErr(-1111, "FSpOpenResFile of alias-file, returned -1");
  68.     }
  69. }
  70.  
  71. void DoAddAlias()
  72. {
  73.     UseResFile(gAliasFileRefnum);
  74.     FailOSErr(ResError(), "UseResFile(gAliasFileRefnum)");
  75.     AddResource(Handle(gTargetAliasH), 'alis', 0, "\pAlias");
  76.     FailOSErr(ResError(), "AddResource");
  77.     WriteResource(Handle(gTargetAliasH));
  78.     FailOSErr(ResError(), "WriteResource");
  79. }
  80.  
  81. void DoCloseFile()
  82. {
  83.     CloseResFile(gAliasFileRefnum);
  84.     FailOSErr(ResError(), "CloseResFile(gAliasFileRefnum)");
  85.     FInfo fndrInfo;
  86.     FailOSErr(FSpGetFInfo(&gAliasFileSpec, &fndrInfo), "FSpGetFInfo");
  87.     fndrInfo.fdFlags |= 1 << 15; // no constant in IM
  88.     FailOSErr(FSpSetFInfo(&gAliasFileSpec, &fndrInfo), "FSpSetFInfo");
  89. }
  90.  
  91. int main(int argc,char *argv[])
  92. {
  93.     PLstrcpy(gTheZone,                "\p*");
  94.     PLstrcpy(gTheServer,            "\p=");
  95.     PLstrcpy(gFullpath,            "\p=");
  96.  
  97.     StringHandle sH = GetString(-16413); // can't find it in IM: name of Macintosh
  98.     if (sH && *sH)
  99.         PLstrcpy(gTheServer, *sH);
  100.     
  101.     while ((--argc != 0) && (*++argv)[0] == '-') {
  102.         switch ( *(argv[0]+1) ) {
  103.         case 'z':
  104.         case 'Z':
  105.             if (!argc--) PrintUsage();
  106.             PLstrcpy(gTheZone, c2pstr(*++argv));
  107.             break;
  108.         case 's':
  109.         case 'S':
  110.             if (!argc--) PrintUsage();
  111.             PLstrcpy(gTheServer, c2pstr(*++argv));
  112.             break;
  113.         default:
  114.             PrintUsage();
  115.         }
  116.     }
  117.     if (argc != 2)
  118.         PrintUsage();
  119.     DoMakeTargetAlias(*argv);
  120.     --argc;
  121.     DoMakeAliasFile(*++argv);
  122.     DoAddAlias();
  123.     DoCloseFile();
  124.     return 0;
  125. }
  126.