home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / POLYEDIT.LZH / ML / FILEIO.C < prev    next >
C/C++ Source or Header  |  1996-06-21  |  7KB  |  351 lines

  1. /*
  2.  *        ファイル処理
  3.  *
  4.  *        M.Takatsu    1996.6.20
  5.  */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <io.h>
  10. #include <assert.h>
  11. #include <ctype.h>
  12.  
  13. #ifdef X68K
  14. #include <doslib.h>
  15. #include <direct.h>
  16. #else
  17. #include <dir.h>
  18. #endif
  19.  
  20. #include "parse.h"
  21. #include "exec.h"
  22. #include "strclass.h"
  23.  
  24. #include "inlib.h"
  25. #include "err.h"
  26.  
  27. static    int        FileClassID;
  28. extern    int        StringClassID;
  29. static    int        FuncFullPath( int, int, DataStruct* );
  30. static    int        FuncDelete( int, int, DataStruct* );
  31. static    int        FuncChdir( int, int, DataStruct* );
  32. static    int        FuncFprint( int, int, DataStruct* );
  33. static    int        fileOpen( int, int, DataStruct* );
  34. static    int        filePuts( int, int, DataStruct* );
  35. static    int        fileGets( int, int, DataStruct* );
  36. static    int        filePutc( int, int, DataStruct* );
  37. static    int        fileGetc( int, int, DataStruct* );
  38. static    int        fileClose( int, int, DataStruct* );
  39.  
  40. typedef enum {FileWrite, FileRead} OpenType;
  41. typedef struct    _FileClass {
  42.     Object    object;
  43.     FILE *fp;
  44.     OpenType type;
  45. }    FileClass;
  46.  
  47.  
  48. /*    初期化    */
  49. void    FileioInit()
  50. {
  51.     FileClassID = ObjectSetClass( "File", MaxFunctions, 0 );
  52.  
  53.     FunctionSet( StringClassID, "fullpath", FuncFullPath );
  54.     FunctionSet( StringClassID, "delete", FuncDelete );
  55.     FunctionSet( 0,                "dir",    FuncChdir );
  56.     FunctionSet( StringClassID, "fprint",    FuncFprint );
  57.  
  58.     FunctionSet( StringClassID, "fileopen",    fileOpen );
  59.     FunctionSet( FileClassID, "fileputs",    filePuts);
  60.     FunctionSet( FileClassID, "filegets",    fileGets );
  61.     FunctionSet( FileClassID, "fileputc",    filePutc );
  62.     FunctionSet( FileClassID, "filegetc",    fileGetc );
  63.     FunctionSet( FileClassID, "fileclose",    fileClose );
  64. }
  65.  
  66. #define MAX_PATH     1024
  67. /*    文字列をフルパスに    */
  68. static    int        FuncFullPath( ident, args, buf )
  69. int        ident ;
  70. int        args ;
  71. DataStruct    *buf ;
  72. {
  73.     StringClass    *str, *ret ;
  74.     char *p, *q;
  75.     char tmp[MAX_PATH];
  76.  
  77.     ArgCheck( "toupper", args, buf, TYPE_OBJECT, TYPE_NOASN );
  78.  
  79.     str = (StringClass*)buf[0].od.ptr ;
  80.     p = str->str;
  81.     q = tmp;
  82.     if (p[0] && p[1] == ':') {
  83.         *q++ = toupper(p[0]);
  84.         *q++ = ':';
  85.         p += 2;
  86.     } else {
  87. #ifdef X68K
  88.         *q++ = CURDRV() + 'A';
  89. #else
  90.         *q++ = getdisk() + 'A';
  91. #endif
  92.         *q++ = ':';
  93.     }
  94.     *q++ = '\\';
  95.     if (p[0] == '\\') {
  96.         p++;
  97.     } else {
  98. #ifdef X68K
  99.         CURDIR(toupper(tmp[0]) - 'A' + 1, (unsigned char *)q);
  100. #else
  101.         getcurdir(0, q);
  102. #endif
  103.         q += strlen(q);
  104.         if (q[-1] != '\\') {
  105.             *q++ = '\\';
  106.         }
  107.     }
  108.     while (*p) {
  109.         if (p[0] == '.' && p[1] == '\\') {
  110.             p += 2;
  111.         } else if (p[0] == '.' && p[1] == '.' && p[2] == '\\') {
  112.             p += 3;
  113.             q -= 2;;
  114.             while (tmp < q && q[0] != ':' && q[0] != '\\') {
  115.                 q--;
  116.             }
  117.             q++;
  118.         } else {
  119.             while (*p && *p != '\\') {
  120.                 *q++ = *p++;
  121.             }
  122.             if (*p) {
  123.                 *q++ = *p++;
  124.             }
  125.         }
  126.     }
  127.  
  128.     *q = '\0';
  129.  
  130.     ret = StringAlloc(strlen(tmp));
  131.     strcpy(ret->str, tmp);
  132.     buf = StackAlloc( 1 );
  133.     buf->type = TYPE_OBJECT ;
  134.     buf->od.ptr = (Object*)ret ;
  135.  
  136.     return RETURN_RETURN ;
  137. }
  138.  
  139. /*    ファイル削除    */
  140. static    int        FuncDelete( ident, args, buf )
  141. int        ident ;
  142. int        args ;
  143. DataStruct    *buf ;
  144. {
  145.     StringClass    *str;
  146.     str = (StringClass*)buf[0].od.ptr;
  147.     unlink(str->str);
  148.     return RETURN_VOID;
  149. }
  150.  
  151. /*    カレントディレクトリ設定/取得    */
  152. static    int        FuncChdir( ident, args, buf )
  153. int        ident ;
  154. int        args ;
  155. DataStruct    *buf ;
  156. {
  157.     StringClass *ret;
  158.     char tmp[MAX_PATH];
  159.     if (args == 0) {
  160.         getcwd(tmp, MAX_PATH);
  161.         tmp[MAX_PATH-1] = '\0';
  162.  
  163.         ret = StringSet( tmp );
  164.  
  165.         buf = StackAlloc( 1 );
  166.         buf->type = TYPE_OBJECT ;
  167.         buf->od.ptr = (Object*)ret ;
  168.         return RETURN_RETURN;
  169.     } else {
  170.         ArgCheck( "dir", args, buf, TYPE_OBJECT, TYPE_NOASN );
  171.         if ( ObjectCheck( &buf[0], StringClassID ) == FALSE )
  172.         {
  173.             ExecError( "引数の型が違います。(dir)" );
  174.             return RETURN_VOID ;
  175.         }
  176.         chdir(((StringClass*)buf[0].od.ptr)->str);
  177.         return RETURN_VOID;
  178.     }
  179. }
  180.  
  181. /*    表示    */
  182. static    int        FuncFprint( ident, args, buf )
  183. int        ident ;
  184. int        args ;
  185. DataStruct    *buf ;
  186. {
  187.     int        i ;
  188.     char *file;
  189.     StringClass    *str ;
  190.     DataStruct    *top ;
  191.     FILE *fp;
  192.  
  193.     file = ((StringClass*)buf[0].od.ptr)->str;
  194.     if ((fp = fopen(file, "a")) == NULL) {
  195.         return RETURN_VOID;
  196.     }
  197.  
  198.     ident = IdentSearch( IdentFunction, "tostring" );
  199.  
  200.     for( i = 1 ; i < args ; i++ )
  201.     {
  202.         StackPush( buf+i );
  203.         top = StackTop();
  204.         CallFunctionLocal( ident, 1, top );
  205.         StackRelease( top );
  206.  
  207.         assert( top->type == TYPE_OBJECT );
  208.         str = (StringClass*)top->od.ptr ;
  209.         fprintf(fp,  str->str );
  210.         if ( i < args - 1 )
  211.             fprintf(fp, " " );
  212.         StackPop();
  213.     }
  214.     fclose(fp);
  215.     return RETURN_VOID ;
  216. }
  217.  
  218. static    int        fileOpen( ident, args, buf )
  219. int        ident ;
  220. int        args ;
  221. DataStruct    *buf ;
  222. {
  223.     char *file, *type;
  224.     FILE *fp;
  225.     FileClass *fc;
  226.     file = ((StringClass*)buf[0].od.ptr)->str;
  227.     if (args == 1) {
  228.         type = "r";
  229.     } else {
  230.         ArgCheck( "fileopen", args, buf, TYPE_OBJECT, TYPE_OBJECT, TYPE_NOASN );
  231.         type = ((StringClass*)buf[1].od.ptr)->str;
  232.     }
  233.     fp = fopen(file, type);
  234.     if (fp == NULL) {
  235.         StackPushBoolean(FALSE);
  236.         return RETURN_RETURN;
  237.     }
  238.  
  239.     fc = (FileClass*)ObjectAlloc(sizeof(FileClass)-sizeof(Object), FileClassID);
  240.     if (type[0] == 'r' || type[0] == 'R') {
  241.         fc->type = FileRead;
  242.     } else {
  243.         fc->type = FileWrite;
  244.     }
  245.     fc->fp = fp;
  246.     buf = StackAlloc( 1 );
  247.     buf->type = TYPE_OBJECT ;
  248.     buf->od.ptr = (Object*)fc ;
  249.     return RETURN_RETURN;
  250. }
  251.  
  252. static    int        filePuts( ident, args, buf )
  253. int        ident ;
  254. int        args ;
  255. DataStruct    *buf ;
  256. {
  257.     FileClass *fc;
  258.  
  259.     int        i ;
  260.     StringClass    *str ;
  261.     DataStruct    *top ;
  262.     FILE *fp;
  263.  
  264.     fc = (FileClass*)buf[0].od.ptr;
  265.  
  266.     ident = IdentSearch( IdentFunction, "tostring" );
  267.  
  268.     for( i = 1 ; i < args ; i++ )
  269.     {
  270.         StackPush( buf+i );
  271.         top = StackTop();
  272.         CallFunctionLocal( ident, 1, top );
  273.         StackRelease( top );
  274.  
  275.         assert( top->type == TYPE_OBJECT );
  276.         str = (StringClass*)top->od.ptr ;
  277.         fprintf(fc->fp,  str->str );
  278.         if ( i < args - 1 )
  279.             fprintf(fc->fp, " " );
  280.         StackPop();
  281.     }
  282.     StackPushBoolean(ferror(fc->fp) == 0);
  283.     return RETURN_RETURN;
  284. }
  285.  
  286. static    int        fileGets( ident, args, buf )
  287. int        ident ;
  288. int        args ;
  289. DataStruct    *buf ;
  290. {
  291.     FileClass *fc;
  292.     StringClass *ret;
  293.     char str[2048];
  294.     fc = (FileClass*)buf[0].od.ptr;
  295.     if (fgets(str, 2048, fc->fp) == NULL) {
  296.         StackPushBoolean(FALSE);
  297.         return RETURN_RETURN;
  298.     }
  299.  
  300.     ret = StringSet( str );
  301.     buf = StackAlloc( 1 );
  302.     buf->type = TYPE_OBJECT ;
  303.     buf->od.ptr = (Object*)ret ;
  304.     return RETURN_RETURN;
  305.  
  306. }
  307.  
  308. static    int        filePutc( ident, args, buf )
  309. int        ident ;
  310. int        args ;
  311. DataStruct    *buf ;
  312. {
  313.     FileClass *fc;
  314.  
  315.     ArgCheck( "filegetc", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_NOASN );
  316.     fc = (FileClass*)buf[0].od.ptr;
  317.     if (fc->type == FileWrite) {
  318.         fputc(buf[1].id.i, fc->fp);
  319.     }
  320.  
  321.     StackPushBoolean(ferror(fc->fp) == 0);
  322.     return RETURN_RETURN;
  323. }
  324.  
  325. static    int        fileGetc( ident, args, buf )
  326. int        ident ;
  327. int        args ;
  328. DataStruct    *buf ;
  329. {
  330.     int c;
  331.     FileClass *fc;
  332.     fc = (FileClass*)buf[0].od.ptr;
  333.     c = fgetc(fc->fp);
  334.     StackPushInt(c);
  335.     return RETURN_RETURN;
  336. }
  337.  
  338. static    int        fileClose( ident, args, buf )
  339. int        ident ;
  340. int        args ;
  341. DataStruct    *buf ;
  342. {
  343.     int flag;
  344.     FileClass *fc;
  345.     fc = (FileClass*)buf[0].od.ptr;
  346.     flag = ferror(fc->fp);
  347.     fclose(fc->fp);
  348.     StackPushBoolean(flag == 0);
  349.     return RETURN_RETURN;
  350. }
  351.