예제 #1
0
파일: flower.c 프로젝트: tsupo/tamo
void
flower( long g, int dd, int mm, int yy )
{
    FILE    *fp;
    char    buf[BUFSIZ], *p = NULL, *q = NULL;
    long    m, d;
    int     found = 0;

    if ( ( fp = tfopen( flowerTblFile, "r" ) ) == NULL )
        return;

    while ( ( p = fgets( buf, BUFSIZ - 1, fp ) ) != NULL ) {
        if ( (*p >= '0') && (*p <= '1') ) {
            q = strchr( p, ' ' );
            if ( q && (*(q + 1) >= '0' && (*(q + 1) <= '9')) ) {
                m = atoi( p );
                d = atoi( q + 1 );
                if ( (m == mm) && (d == dd) ) {
                    found = 1;
                    break;
                }
            }
        }
    }
    tfclose( fp );

    if ( (found == 1) && q ) {
        p = strchr( q + 1, ' ' );
        if ( !p )
            p = strchr( q + 1, '\t' );
        if ( p && *(p + 1) ) {
            q = strchr( p + 1, ' ' );
            if ( !q )
                q = strchr( p + 1, '\t' );
            if ( q && *(q + 1) ) {
                *q++ = NUL;
                while ( (*p == ' ') || (*p == '\t') )
                    p++;
                while ( (*q == ' ') || (*q == '\t') )
                    q++;
                while ( (q[strlen(q) - 1] == '\n') ||
                        (q[strlen(q) - 1] == ' ' ) ||
                        (q[strlen(q) - 1] == '\t')    )
                    q[strlen(q) - 1] = NUL;
                if ( *q ) {
                    putchar( '\n' );
                    printf( "今月の誕生石: %s\n      誕生花: %s\n",
                            birthStone[mm - 1], birthFlower[mm - 1] );
                    printf( "今日の誕生花: %s\n      花言葉: 『%s』\n",
                            p, q );
                }
            }
        }
    }
}
예제 #2
0
// Initialize Log object
void GTLogTextFile::Initialize(void)
{
	FILE* fp = tfopen(GTLogTextFile::g_szLogFileName, CTEXT("w"));
	if(fp)
	{
		OutTimestamp(fp);
		fprintf(fp, "\n");
		tfclose(fp);
		fp = NULL;
	}
}
예제 #3
0
void GTLogTextFile::LogOutout(const tchar* szMes, const tchar* szType)
{
	if(szMes && szType)
	{
		FILE* fp = tfopen(GTLogTextFile::g_szLogFileName, CTEXT("a+"));
		if(fp)
		{
			tfprintf(fp, CTEXT("[%s]: %s\n"), szType, szMes);
			tfclose(fp);
			fp = NULL;
		}
	}
}
예제 #4
0
int32 GOS::GetFileSize(const tchar* szFileName)
{
	if(NULL == szFileName)
	{
		return -1;
	}

	FILE* fp = tfopen(szFileName, CTEXT("rb"));
	if(NULL == fp)
	{
		// The file doesn't exist.
		return -2;
	}
	// Set fp to the end of the file
	tfseek(fp, 0, SEEK_END);
	// Get the current fp position to get the size
	const int32 nFileSize = tftell(fp);
	// Close the file
	tfclose(fp);
	fp = NULL;

	return nFileSize;
}
예제 #5
0
파일: campaign.c 프로젝트: tsupo/tamo
void
campaign( long g, int dd, int mm, int yy )
{
    FILE    *fp;
    char    buf[BUFSIZ], *p = NULL, *q = NULL, *r = NULL, *s = NULL;
    long    m1, d1, m2, d2;
    long    g1, g2;
    long    cnt = 0;

    if ( ( fp = tfopen( campaignTblFile, "r" ) ) == NULL )
        return;

    while ( ( p = fgets( buf, BUFSIZ - 1, fp ) ) != NULL ) {
        if ( (*p >= '0') && (*p <= '1') ) {
            q = strchr( p, '/' );
            if ( q && (*(q + 1) >= '0' && (*(q + 1) <= '9')) ) {
                q++;
                m1 = atoi( p );
                d1 = atoi( q );
                r = strchr( q, ' ' );
                if ( r && (*(r + 1) >= '0' && (*(r + 1) <= '1')) ) {
                    r++;
                    s = strchr( r, '/' );
                    if ( s && (*(s + 1) >= '0' && (*(s + 1) <= '9')) ) {
                        s++;
                        m2 = atoi( r );
                        d2 = atoi( s );

                        g1 = absoluteFromGregorian( d1, m1, yy );
                        g2 = absoluteFromGregorian( d2, m2, yy );
                        if ( g2 < g1 ) {
                            if ( (g2 < g) && (g < g1) )
                                continue;
                            if ( g1 <= g ) 
                                g2 = absoluteFromGregorian( d2, m2, yy + 1 );
                            else if ( g <= g2 )
                                g1 = absoluteFromGregorian( d1, m1, yy - 1 );
                        }

                        if ( (m1 == 2) && (d1 == 29) ) {
                            if ( !leapyear( yy ) ) {
                                g1--;
                                d1--;
                            }
                        }
                        if ( (m2 == 2) && (d2 == 29) ) {
                            if ( !leapyear( yy ) ) {
                                g2--;
                                d2--;
                            }
                        }

                        if ( (g1 <= g) && (g <= g2) ) {
                            p = strchr( s + 1, ' ' );
                            if ( !p )
                                p = strchr( s + 1, '\t' );
                            if ( p ) {
                                printCampaign( p, d1, m1, d2, m2 );
                                cnt++;
                            }
                        }
                    }
                }
            }
        }
    }
    tfclose( fp );

    printCampaign2( &cnt, g, dd, mm, yy );

    return;
}
예제 #6
0
// Load a file data into memory
void* GOS::LoadFileIntoMemory(const tchar* szFileName, uint32* pSize)
{
	/*
	// Windows version
	HANDLE hFile = INVALID_HANDLE_VALUE;
	uint32 nFileSize = 0;
	void* ptr = NULL;

	hFile = ::CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		return NULL;
	}
	nFileSize = ::GetFileSize(hFile, NULL);
	ptr = ::malloc(nFileSize);
	if(!ptr)
	{
		::CloseHandle(hFile);
		return NULL;
	}
	if(::ReadFile(hFile, ptr, nFileSize, &nFileSize, NULL ) == 0)
	{
		::CloseHandle(hFile);
		::free(ptr);
		return NULL;
	}
	
	::CloseHandle(hFile);
	if(pSize)
	{
		(*pSize) = nFileSize;
	}

	return ptr;
	*/
	
	uint32 nFileSize = 0;
	FILE* hFile = NULL;	
	void* ptr = NULL;

	// Get the file size
	nFileSize = GOS::GetFileSize(szFileName);
	if(nFileSize <= 0)
	{
		return NULL;
	}

	hFile = tfopen(szFileName, CTEXT("rb"));
	if(hFile == NULL)
	{
		return NULL;
	}

	ptr = malloc(nFileSize);
	if(!ptr)
	{
		fclose(hFile);
		return NULL;
	}

	if(0 == tfread(ptr, nFileSize, 1, hFile))
	{
		tfclose(hFile);
		free(ptr);
		return NULL;
	}
	
	tfclose(hFile);
	if(pSize)
	{
		(*pSize) = nFileSize;
	}

	return ptr;
}
예제 #7
0
파일: pgcwhpolygon.c 프로젝트: idkk/Test1
/*
 ** Function : xxx()
 ** Purpose  : 
 ** Arguments: 
 ** Returns  : void
 ** Notes    :
 */
int 
main (int   argc,
      char *argv[])
{  
    int c;
    int cmderr = 0;                /* Initialised: No errors yet           */
    extern char *optarg;
    extern int optind, optopt;
    char *ifile = (char *)NULL;    /* Input file name                      */
    char *ofile = (char *)NULL;    /* Output file name                     */
    char *rewrite = (char *)NULL;  /* -R parameter value                   */
    char *strictstr = (char *)NULL; /* -S parameter value                  */
    FILE *ipf = (FILE *)NULL;      /* Input file pointer                   */
    FILE *opf = (FILE *)NULL;      /* Output file pointer                  */
    struct box   Box;              /* Min and Max X and Y coords           */
	struct diamond Diamond;        /* Interior, contained quadrilateral    */
    struct ring *rings;            /* Ptr to list of Read In rings         */
    int defboxflag = 0;
    int boxflag    = defboxflag;
    int ringCnt    = 0;            /* Count of number of rings found       */
    int ret        = 0;            /* Return code                          */
    int rew        = 0;            /* Overwrite permission - default YES   */
	int i, j, k;                   /* Work variables                       */
    int  thisendian  = LITTLE;     /* Default Endianicty of *this* machine */
    char temp     [NAMEMAX+1];     /* Temporary work area                  */
	char progname [NAMEMAX+1];     /* Name of *this* program               */
	
	char slash = '/';
//	char dot   = '.';
	
    
	
	int *nesting = (int *)NULL; /* Ptr to Matrix for nesting tests */
    //	int outer;  /* IDKK - nesting */
    //	int inner;  /* IDKK - nesting */
	
    fprintf(stderr, "This program is strictly Copyright (c) 2013 "
            "Westheimer Energy Consultants Ltd.\n"
            "All Rights Reserved\n\n");
    
    maxcorners = MAXCORNERS;
    maxmaxcorners = UPPERMAXCORN;
    corners = 0;
    strict = 0;
    tolerance = 1;
    
    /*  ENDIANNESS  */
    
	/*
	 *  Test and remember the endianness of *this* machine:
	 */
	if (ENDIANNESS == BIG)
	{
		thisendian = BIG;
        fprintf(stderr, "INFO: This machine is BIG-endian\n");
	}
	else if (ENDIANNESS == LITTLE)
	{
		thisendian = LITTLE;
        fprintf(stderr, "INFO: This machine is LITTLE-endian\n");
	}
	else 
	{
		fprintf(stderr,
                "ERROR: ENDIAN format of this machine not supported (%d / %x).\n",
                endianness, endianness);
		fprintf(stderr,"[endianness first byte is %x]\n",
                *(const char *)&endianness);
		cmderr++;
	}
    
    /*  PROGRAM NAME  */
	
	/*
	 *  Remember the program name - the name of this program - for
	 *  debug and message printout. We edit out, from the name of
	 *  the program as invoked, any prior directory name specified:
	 */
	strcpy(temp, argv[0]);
	j = strlen(temp); 
	i = 0;
	while((j>i) && (temp[j] != slash)) 
	{
		j--;
	}
    
	/* If there actually was a slash in the program name, we are now pointing at
	 * it - so move forwards to point to the first character after the
	 * rightmost slash: */
	if ( temp[j] == slash ) 
	{
		j++;
	}
	k = strlen(temp); 
	strncpy(progname, &temp[j], k-j);
	/* Add a terminating string end character: */
	progname[k-j] = '\0';
	
    	
    /*
	 ** Read the Command Line Arguments
	 */
    while ((c = getopt(argc, argv, ":i:o:b:c:C:x:y:S:t:R:v")) != -1)
    {
        switch(c)
        {
            /* Input file: */
			case 'i':
				ifile = optarg;
				break;
                
            /* Output file: */
			case 'o':
				ofile = optarg;
				break;
				
            /* Bounding box (Y or N): */
			case 'b':
				boxflag = atoi(optarg);
				break;
               
            /* Max number of corners: */
            case 'c':
                maxcorners = atoi(optarg);
                if (maxcorners > maxmaxcorners)
                {
                    fprintf(stderr,"ERROR: Corners (-c) must be less than %d\n",
                            UPPERMAXCORN);
                    cmderr++;
                }
                break;
				
            /* Tolerance of corner matching: */
            case 'C':
                tolerance = atoi(optarg);
                if ((tolerance < 1) || (tolerance > MAXTOL))
                {
                    fprintf(stderr, 
                            "ERROR: Tolerance (-C) must be between 1 and %d, not %d\n",
                            MAXTOL, tolerance);
                    cmderr++;
                }
                break;
               
            /* Displacement of X column: */
			case 'x':
				xcol = optarg;
				break;
                
            /* Displacement of Y column: */
			case 'y':
				ycol = optarg;
				break;
				
            /* Trace/Debug flags: */
			case 't':
				tprint = atoi(optarg);
				break;
                
            /* Permit (or not) overwriting pre-existing files: */
            case 'R':
                rewrite = optarg;
                /* Check argument against y/Y/n/N */
                if ((strncmp(rewrite,"n",1)==0)||(strncmp(rewrite, "N", 1)==0))
                {
                    fprintf(stderr, "Existing files may NOT be overwritten\n");
                    rew = 0;
                }
                else if ((strncmp(rewrite,"n",1)==0)||
                         (strncmp(rewrite, "N", 1)==0))
                {
                    fprintf(stderr, "Existing files CAN be overwritten\n");
                    rew = 1;
                }
                else 
                {
                    fprintf(stderr, "ERROR: -R may be Y or N not %s\n",
                            rewrite);
                    cmderr++;
                }                
                break;
                
            /* Strictness (Y or N): */
            case 'S':
                strictstr = optarg;
                if ((strncmp(strictstr, "Y", 1) == 0) ||
                    (strncmp(strictstr, "y", 1) == 0))
                {
                    strict = 1;
                }
                else if ((strncmp(strictstr, "N", 1) == 0) ||
                         (strncmp(strictstr, "n", 1) == 0))
                {
                    strict = 0;
                }
                else 
                {
                    fprintf(stderr, 
                            "ERROR: -S (strict) may be Y or N, not %s\n",
                            strictstr);
                    cmderr++;
                }

                break;
				       
            /* Program version display: */
            case 'v':
                fprintf(stderr, "\n");
                fprintf(stderr, "Program %s version: %s / %s\n",
                        progname, PROGRAM_VERSION, PROGRAM_EDIT_DATE);
                fprintf(stderr, "(%s)\n", argv[0]);
                fprintf(stderr, "\n");
                break;
				
            /* Parameter specification error: */
			case ':':       /* -i or -o without operand */
				fprintf(stderr, "ERROR: Option -%c requires an operand\n", 
                        optopt);
				cmderr++;
				break;
                
            /* Unrecognized parameter - error: */
			case '?':
				fprintf(stderr, "ERROR: Unrecognized option: -%c\n", optopt);
				cmderr++;
        }
    }

#ifdef LONGPOINT
    fprintf(stderr,"INFO: Point type is long\n");
#else
    fprintf(stderr,"INFO: Point type is int\n");
#endif
	
    /*
	 ** We must have been passed the input (CSV) Filename
	 */
    if (ifile == (char *)NULL)
    {
        fprintf(stderr, "ERROR: An input file (-i) must be provided\n");
        cmderr++;
    }
    else
    {
        /*
		 ** Does the Input file exist
		 */
        fprintf(stderr,"Opening [%s]\n", ifile);
        if ((ipf = fopen(ifile, "r" )) == (FILE *)NULL)
        {
            fprintf(stderr, "ERROR: Unable to open input file (-i) [%s]\n", 
                    ifile);
            cmderr++;
        }
    }
	
    /*
	 ** We must have been passed the output (SQL) Filename
	 **
	 */
    if (ofile == (char *)NULL)
    {
        fprintf(stderr, "ERROR: An output file (-o) must be provided\n");
        cmderr++;
    }
    else
    {
        /*
		 ** We could see if the Output File already exists - overwrite flag?
		 ** Open the output file
		 */
        if (rew != 0)
        {
            opf = fopen(ofile, "r");
            if (opf != (FILE *)NULL)
            {
                fprintf(stderr, "ERROR: Output file %s already exists -\n"
                                "       Overwriting not permitted\n"
                        "       Run terminates.\n", ofile);
                exit(FAIL_OUTPUT_EXISTS);
            }
        }
        fprintf(stderr,"Opening [%s]\n", ofile);
        if ((opf = fopen(ofile, "w" )) == (FILE *)NULL)
        {
            fprintf(stderr, "ERROR: Unable to open output file (-o) [%s]\n", 
                    ofile);
            cmderr++;
        }
    }
	
    /*
	 ** the Box Flag must be zero or one
	 */
    if (boxflag != 0 && boxflag != 1)
    {
        fprintf(stderr, "ERROR: The Box Flag (-b) must be 0 or 1 (%d)\n", 
                boxflag);
        cmderr++;
    }
	
    /*
	 ** If something went wrong, whinge and die ...
	 */
    if (cmderr)
    {
        fprintf(stderr, "Command Error (%d)\n", cmderr);
        fprintf(stderr, "\n");
        fprintf(stderr, "Usage: %s <options>\n", argv[0]);
        fprintf(stderr, "  Options:-\n");
        fprintf(stderr, "   -i <filename>  Input CSV File     [No Default]\n");
        fprintf(stderr, "   -o <filename>  Output SQL File    [No Default]\n");
        fprintf(stderr, "   -b <0|1>       Build 'box' WHERE  [Def %d]\n",
				defboxflag);
        fprintf(stderr, "   -x <Col Name>  X Coord Col name   [Def '%s']\n",
				XCOL);
        fprintf(stderr, "   -y <Col Name>  Y Coord Col name   [Def '%s']\n",
				YCOL);
        fprintf(stderr, "   -c <corners>   Maximum corners    [Def %d]\n",
                MAXCORNERS);
		fprintf(stderr, "   -t <number>    Trace print level  [Def 0]\n");
        fprintf(stderr, "\n");
        fprintf(stderr, "Program %s version: %s / %s\n",
                progname, PROGRAM_VERSION, PROGRAM_EDIT_DATE);
        fprintf(stderr, "(%s)\n", argv[0]);
        fprintf(stderr, "\n");
		
        /*
		 ** Close any files that have been opened
		 */
        tfclose(ipf);
        tfclose(opf);
		exit(ERROR_PARAMETERS);
    }
	
    /*
	 ** Here We Go >>>
	 */
    fprintf(stderr,"\n");
    /*
     ** Set the outermost Box and Diamond to impossible values:
     */
    
    Box.min.x = XYMAX;
    Box.min.y = XYMAX;
    Box.max.x = XYMIN;
    Box.max.y = XYMIN;
    Diamond.top.x = XYMIN;
    Diamond.top.y = XYMIN;
    Diamond.bottom.x = XYMAX;
    Diamond.bottom.y = XYMAX;
    Diamond.eastmost.x = XYMIN;
    Diamond.eastmost.y = XYMIN;
    Diamond.westmost.x = XYMAX;
    Diamond.westmost.y = XYMAX;
    	
    /*
	 ** Build the Rings (by reading the Coords - etc)
	 */
    if ((rings = GetRings(&ipf, &Box, &Diamond)) == (struct ring *)NULL)
    {
        fprintf(stderr, "ERROR: Failed to read Rings (%d)\n", ret);
        
        exit(FAIL_READ_RINGS);
    }
    
	    
    /*
	 ** Done with the Input File now, so we can close it, but check it is open!
	 */
    tfclose(ipf);
    
    /*
	 ** Sort the Rings so 'rings-within-rings' are suitably concentric ordered
	 */
	/*
	 ** IDKK: I *think* we need to find the convex hulls and *then* order the 
     **       rings
	 */
    ringCnt = rings->prev->pos + 1;
    /*
	 ** Build the Convex Hull Rings
	 */
    if (ConvexHullRings(rings) == 0)
    {
        fprintf(stderr, "ERROR: Failed to build Convex Hull\n");
        
        exit(FAIL_BUILD_CONVEX_HULL);
    }
	/*
	 ** Now determine the nesting of the rings, if there is more than one ring:
	 ** NOTE that this is incompatible with the warning issued just above of our
	 ** not supporting Concentric Rings... remove the previous warnings, though, 
	 ** only when this nesting code is complete (NOT YET as of 20121017).
	 */
	if (ringCnt > 1)
	{
		/*
         **  Ensure that each ring has a good box and diamond - thisis achieved
         **  within routine setBoxDiamond, which runs round every ring.
         **  Then call the nesting routine, which returns a pointer to an array
         **  indicating the nesting of the rings. Note that ringNesting allocates
         **  that array, but returning NULL in the event of error.
		 */
       
//        AllDirections(ringCnt, rings);
        setBoxDiamond(rings);
  		nesting = ringNesting( ringCnt, rings );
		/*
		 ** If the "nesting" pointer is still NULL, then we have had an error:
		 */
		if (nesting == (int *)NULL)
		{
			fprintf(stderr, "ERROR: No memory for nesting calculation.\n");
			exit(FAIL_GET_MEMORY_1);		
		}
	}
	else 
	{
	}

    /*
	 ** Build the Box Where Clause
	 **   and remember we did it in the first ring
	 */
    if (boxflag == 0)
    {
        ShowBox(&Box);
        BuildWhereBox(&opf, &Box);
        rings->opand = OPANDAND;
    }
	
    /*
	 ** Build the WHERE Clause(s) from the Rings
	 */
    
	/* !!!! TODO This can be altered to use the nesting information */
    if ((ret = BuildWhereRing(&opf, rings)) != 0)
    {
        fprintf(stderr, "ERROR: Failed to build WHERE Clause(s) (%d)\n", ret);
        
        exit(FAIL_BUILD_WHERE);
    }
	
    /*
	 ** We have to close the braces if we use the Box
	 */
    if (boxflag == 0)
    {
			fprintf(opf, "      -- End WHERE\n");
    }
	
    /*
	 ** Close the files
	 */
    tfclose(opf);
	
    exit(NORMAL_RETURN);
}     /* endof main */