示例#1
0
/* Loads a world from a text file */
WORLD* WorldCreateFromFile(PCSZ pszFilename) {
	ASSERT(pszFilename);

	FILE* pf = fopen(pszFilename, "r");
	if (!pf)
		Die("Cannot open '%s'", pszFilename);

	unsigned int cx, cy;
	const int MAX_LINE_LENGTH = 4096;
	char szLine[MAX_LINE_LENGTH + 1];
	char const* pszCur = szLine;
	WORLD* pwld;

	/* read first line (dimensions of map) */
	if (fgets(szLine, MAX_LINE_LENGTH, pf) != szLine)
		Die("Error reading first line from '%s' -- file too short?", pszFilename);
	if (!(ParseUInt(&pszCur, &cx) && ParseUInt(&pszCur, &cy)))
		Die("Malformed first line in '%s'", pszFilename);
	if (cx > MAX_LINE_LENGTH || cy > MAX_LINE_LENGTH)
		Die("World too big (maximum dimensions: %d square)", MAX_LINE_LENGTH);

	pwld = WorldCreate(cx, cy);
	/* Debug("Creating world %d x %d", cx, cy); */

	int x, y;
	bool bGotRobby = false;
	for (y = 0; y < pwld->cy; ++y) {
		if (fgets(szLine, MAX_LINE_LENGTH, pf) != szLine)
			Die("Error reading from '%s' -- file too short?", pszFilename);
		pszCur = szLine;
		for (x = 0; x < pwld->cx; ++x) {
			CELL cell;
			switch (*pszCur) {
			case ' ': cell = CELL_OPEN; break;
			case 'x': cell = CELL_WALL; break;
			case 'R':
				cell = CELL_OPEN;
				pwld->xRobby = x;
				pwld->yRobby = y;
				bGotRobby = true;
				break;
			default:
				cell = CELL_OPEN; /* Silence "uninitialized" warning */
				Die("%s (%d,%d): Unknown character '%c' in world",
					pszFilename, y + 1, x, *pszCur);
			}
			WorldSetCell(pwld, x, y, cell);
			++pszCur;
		}
	}
	fclose(pf);
	if (!bGotRobby)
		Die("World %s contains no Robby start position (R) cell", pszFilename);
	return pwld;
}
示例#2
0
 void test1()
  {
   const uint16 minval=5000u;
   const uint16 maxval=20000u;
  
   uint16 val=random.next16();
   char buf[32];
   PrintBuf out(Range(buf));
   
   Putobj(out,val);
   
   StrLen str=out.close();
   
   //Printf(Con,"str = #;\n",str);
   
   StrParse dev(str);
   
   uint16 ret;
   
   ParseUInt(dev,ret,minval,maxval);
   
   if( val>=minval && val<=maxval )
     {
      if( !dev ) guard(1);
        
      if( ret!=val ) guard(2); 
     }
   else
     {
      if( !!dev ) guard(3);
     }  
  }
示例#3
0
PrintDumpOptType::PrintDumpOptType(const char *ptr,const char *lim)
 {
  StrParse dev(ptr,lim);

  ParseUInt_empty(dev,width,0u);

  if( ParseChar_try(dev,'.') )
    {
     ParseUInt(dev,line_len);
    }
  else
    {
     line_len=Default_line_len;
    }

  if( !dev.finish() ) setDefault();
 }
示例#4
0
static bool ParseInt(char *s, char *e, int64_t *iOut)
{
    if (s >= e)
        return false;

    bool neg = false;
    if ('-' == *s) {
        neg = true;
        s++;
    }
    uint64_t u;
    if (!ParseUInt(s, e, &u))
        return false;
    if (u > MAXLONG64)
        return false;
    int64_t i = (int64_t)u;
    if (neg)
        i = -i;
    *iOut = i;
    return true;
}
示例#5
0
static bool ParseInt(char *s, char *e, int64_t *iOut)
{
    if (s >= e)
        return false;

    bool neg = false;
    if ('-' == *s) {
        neg = true;
        s++;
    }
    uint64_t u;
    if (!ParseUInt(s, e, &u))
        return false;
#if 0 // TODO:: why is this missing?
    if (u > MAXLONG64)
        return false;
#endif
    int64_t i = (int64_t)u;
    if (neg)
        i = -i;
    *iOut = i;
    return true;
}
示例#6
0
static bool DecodeField(DecodeState& ds, TxtNode *firstNode, TxtNode *defaultFirstNode, FieldMetadata *fieldDef, uint8_t *structDataStart)
{
    Type type = fieldDef->type;
    uint8_t *structDataPtr = structDataStart + fieldDef->offset;

    if ((type & TYPE_NO_STORE_MASK) != 0) {
        WriteDefaultValue(structDataPtr, type);
        return true;
    }

    bool isCompact = ((type & TYPE_STORE_COMPACT_MASK) != 0);
    type = (Type)(type & TYPE_NO_FLAGS_MASK);

    const char *fieldName = ds.fieldNamesSeq + fieldDef->nameOffset;
    size_t fieldNameLen = str::Len(fieldName);
    TxtNode *dataNode = FindNode(firstNode, fieldName, fieldNameLen);
    TxtNode *defaultNode = FindNode(defaultFirstNode, fieldName, fieldNameLen);

    // if the node doesn't exist in data, try to get it from default data
    TxtNode *node = dataNode ? dataNode : defaultNode;
    if (!node) {
        WriteDefaultValue(structDataPtr, type);
        return true;
    }
    bool ok;
    if (TYPE_BOOL == type) {
        bool bVal;
        ok = ParseBool(node->valStart, node->valEnd, &bVal);
        if (!ok)
            return false;
        WriteStructBool(structDataPtr, bVal);
    } else if (TYPE_COLOR == type) {
        COLORREF val;
        ok = ParseColor(node->valStart, node->valEnd, &val);
        if (ok)
            WriteStructUInt(structDataPtr, TYPE_COLOR, val);
    } else if (IsUnsignedIntType(type)) {
        uint64_t n;
        ok = ParseUInt(node->valStart, node->valEnd, &n);
        if (ok)
            ok = WriteStructUInt(structDataPtr, type, n);
    } else if (IsSignedIntType(type)) {
        int64_t n;
        ok = ParseInt(node->valStart, node->valEnd, &n);
        if (ok)
            ok = WriteStructInt(structDataPtr, type, n);
    } else if (TYPE_STRUCT_PTR == type) {
        uint8_t *d = NULL;
        if (isCompact && (TextNode == node->type)) {
            d = DeserializeCompact(ds, node, defaultNode, fieldDef->def);
        } else {
            if (StructNode != node->type)
                return false;
            d = DeserializeRec(ds, node, defaultNode, fieldDef->def);
        }
        if (!d)
            goto Error;
        WriteStructPtrVal(structDataPtr, d);
    } else if (TYPE_STR == type) {
        char *s = node->valStart;
        size_t sLen = node->valEnd - s;
        if (s && (sLen > 0)) {
            // note: we don't free s because it's remembered in structDataPtr
            s = str::DupN(s, sLen);
            WriteStructStr(structDataPtr, s);
        }
    } else if (TYPE_WSTR == type) {
        char *s = node->valStart;
        size_t sLen = node->valEnd - s;
        if (s && (sLen > 0)) {
            // note: we don't free ws because it's remembered in structDataPtr
            WCHAR *ws = str::conv::FromUtf8(s);
            WriteStructWStr(structDataPtr, ws);
        }
    }  else if (TYPE_FLOAT == type) {
        float f;
        ok = ParseFloat(node->valStart, node->valEnd, &f);
        if (ok)
            WriteStructFloat(structDataPtr, f);
    } else if (TYPE_ARRAY == type) {
        CrashIf(!fieldDef->def); // array elements must be a struct
        if (StructNode != node->type)
            return false;
        TxtNode *child;
        ListNode<void> *last = NULL;
        for (size_t i = 0; i < node->children->Count(); i++) {
            child = node->children->At(i);
            if (ArrayNode != child->type)
                return false;
            uint8_t *d = DeserializeRec(ds, child, NULL, fieldDef->def);
            if (!d)
                goto Error; // TODO: free root
            ListNode<void> *tmp = AllocArray<ListNode<void>>(1);
            tmp->val = (void*)d;
            if (!last) {
                // this is root
                last = tmp;
                // we remember it so that it gets freed in case of error
                WriteStructPtrVal(structDataPtr, (void*)last);
            } else {
                last->next = tmp;
                last = tmp;
            }
        }
    } else {
        CrashIf(true);
        return false;
    }
    return true;
Error:
    return false;
}
示例#7
0
template<> void TUNDRACORE_API Attribute<uint>::FromString(const std::string& str, AttributeChange::Type change)
{
    Set(ParseUInt(str, DefaultValue()), change);
}
示例#8
0
static bool DecodeField(DecodeState& ds, TxtNode *firstNode, const char *fieldName, const FieldMetadata *fieldDef, uint8_t *structDataStart)
{
    Type type = fieldDef->type;
    uint8_t *structDataPtr = structDataStart + fieldDef->offset;

    if ((type & TYPE_NO_STORE_MASK) != 0) {
        WriteDefaultValue(structDataPtr, type);
        return true;
    }

    bool isCompact = ((type & TYPE_STORE_COMPACT_MASK) != 0);
    type = (Type)(type & TYPE_MASK);

    size_t fieldNameLen = str::Len(fieldName);

    // if the node doesn't exist in data, try to get it from default data
    TxtNode *node = FindNode(firstNode, fieldName, fieldNameLen);
    if (!node) {
        WriteDefaultValue(structDataPtr, type);
        return true;
    }
    bool ok;
    if (TYPE_BOOL == type) {
        bool bVal;
        ok = ParseBool(node->valStart, node->valEnd, &bVal);
        if (!ok)
            return false;
        WriteStructBool(structDataPtr, bVal);
    } else if (TYPE_COLOR == type) {
        COLORREF val;
        ok = ParseColor(node->valStart, node->valEnd, &val);
        if (ok)
            WriteStructUInt(structDataPtr, TYPE_COLOR, val);
    } else if (IsUnsignedIntType(type)) {
        uint64_t n;
        ok = ParseUInt(node->valStart, node->valEnd, &n);
        if (ok)
            ok = WriteStructUInt(structDataPtr, type, n);
    } else if (IsSignedIntType(type)) {
        int64_t n;
        ok = ParseInt(node->valStart, node->valEnd, &n);
        if (ok)
            ok = WriteStructInt(structDataPtr, type, n);
    } else if (TYPE_STRUCT_PTR == type) {
        uint8_t *d = DecodeStruct(ds, fieldDef, node, isCompact);
        if (d)
            WriteStructPtrVal(structDataPtr, d);
    } else if (TYPE_STR == type) {
        char *s = node->valStart;
        size_t sLen = node->valEnd - s;
        if (s && (sLen > 0)) {
            // note: we don't free s because it's remembered in structDataPtr
            s = str::DupN(s, sLen);
            WriteStructStr(structDataPtr, s);
        }
    } else if (TYPE_WSTR == type) {
        char *s = node->valStart;
        size_t sLen = node->valEnd - s;
        if (s && (sLen > 0)) {
            // note: we don't free ws because it's remembered in structDataPtr
            WCHAR *ws = str::conv::FromUtf8(s);
            WriteStructWStr(structDataPtr, ws);
        }
    }  else if (TYPE_FLOAT == type) {
        float f;
        ok = ParseFloat(node->valStart, node->valEnd, &f);
        if (ok)
            WriteStructFloat(structDataPtr, f);
    } else if (TYPE_ARRAY == type) {
        if (StructNode != node->type)
            return false;
        Vec<uint8_t*> *vec = new Vec<uint8_t*>();
        // we remember it right away, so that it gets freed in case of error
        WriteStructPtrVal(structDataPtr, (void*)vec);
        TxtNode *child;
        for (size_t i = 0; i < node->children->Count(); i++) {
            child = node->children->At(i);
            uint8_t *d = DecodeStruct(ds, fieldDef, child, isCompact);
            if (d)
                vec->Append(d);
        }
    } else {
        CrashIf(true);
        return false;
    }
    return true;
}
示例#9
0
unsigned int ParseUIntStd(const str_type::string& str)
{
	return ParseUInt(str.c_str());
}