Esempio n. 1
0
size_t JsonWriteJoinReply(char* dest, size_t maxLen, const char* userId, int reply, int nonce)
{
    JsonWriter writer(dest, maxLen);

    {
        WriteObject obj(writer);

        WriteKey(writer, "cmd");
        if (reply == DISCORD_REPLY_YES) {
            writer.String("SEND_ACTIVITY_JOIN_INVITE");
        }
        else {
            writer.String("CLOSE_ACTIVITY_JOIN_REQUEST");
        }

        WriteKey(writer, "args");
        {
            WriteObject args(writer);

            WriteKey(writer, "user_id");
            writer.String(userId);
        }

        JsonWriteNonce(writer, nonce);
    }

    return writer.Size();
}
Esempio n. 2
0
bool StoreBeaconKeys(
        const std::string &cpid,
        const std::string &pubKey,
        const std::string &privKey)
{
    if (   !WriteKey("publickey" + cpid + GetNetSuffix(), pubKey)
        || !WriteKey("privatekey" + cpid + GetNetSuffix(), privKey)
       )
        return false;

    else
        return true;
}
Esempio n. 3
0
size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId)
{
    JsonWriter writer(dest, maxLen);

    {
        WriteObject obj(writer);
        WriteKey(writer, "v");
        writer.Int(version);
        WriteKey(writer, "client_id");
        writer.String(applicationId);
    }

    return writer.Size();
}
Esempio n. 4
0
static void JsonWriteNonce(JsonWriter& writer, int nonce)
{
    WriteKey(writer, "nonce");
    char nonceBuffer[32];
    NumberToString(nonceBuffer, nonce);
    writer.String(nonceBuffer);
}
Esempio n. 5
0
size_t JsonWriteUnsubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName)
{
    JsonWriter writer(dest, maxLen);

    {
        WriteObject obj(writer);

        JsonWriteNonce(writer, nonce);

        WriteKey(writer, "cmd");
        writer.String("UNSUBSCRIBE");

        WriteKey(writer, "evt");
        writer.String(evtName);
    }

    return writer.Size();
}
Esempio n. 6
0
void _cdecl CScript::WriteKeyFormat( LPCTSTR pszKey, LPCTSTR pszVal, ... )
{
	ADDTOCALLSTACK_INTENSIVE("CScript::WriteKeyFormat");
	TemporaryString pszTemp;
	va_list vargs;
	va_start( vargs, pszVal );
	_vsnprintf(pszTemp, pszTemp.realLength(), pszVal, vargs);
	WriteKey(pszKey, pszTemp);
	va_end( vargs );
}
Esempio n. 7
0
static void
InsertSectionKey (const char *section, const char *var,
		  const char *var_name, FILE * file, int saut)
{
  if (saut)
    fwrite ("\n", 1, 1, file);
  fwrite ("*", 1, 1, file);
  fwrite (section, strlen (section), 1, file);
  fwrite ("?\n", 2, 1, file);
  WriteKey (var, var_name, file);
}
Esempio n. 8
0
void IniFile::WriteEntryAsString ( wchar_t *section, wchar_t *entry, wchar_t *value)
{
	// sets an entry in a dictionary. If the given entry can be found in the dictionary, it is
	// modified to contain the provided value. If it cannot be found, it is created.

	int length;
	int i;
	wchar_t key[256];

	if (section == NULL)
		section = INIFile_default_section; // if no section was provided, use empty section name

	// if we were given an entry, build a key as section#entry
	if (entry != NULL)
	{
		WriteKey ( section, NULL); // create the section if it doesn't exist

		swprintf (key, 256, L"%s%c%s", section, INI_SECTION_SEPARATOR, entry); // compose the key

		i = (int) wcslen (key); // get the key string length
		if (i < sizeof (key))
			length = i;
		else
			length = sizeof (key) - 1; // clamp it to a max value

		// for each character in the string after the section separator...
		for (i = (int) wcslen (section) + 1; i < length; i++)
			key[i] = towlower (key[i]); // convert it to lowercase
		key[i] = 0; // terminate the string
	}

	// else it must be a section name
	else
		wcscpy_s (key, 256, section); // copy the name into the key

	WriteKey ( key, value); // write the new key in the dictionary
	return; // finished
}
WrittenArraySampleIDPtr
WriteStringArrayT( WrittenArraySampleMap &iMap,
                   hid_t iGroup,
                   const std::string &iName,
                   const AbcA::ArraySample &iSamp,
                   const AbcA::ArraySample::Key &iKey,
                   int iCompressionLevel )
{
    // because strings are packed together, always write out the dimensions
    Dimensions dims = iSamp.getDimensions();
    ABCA_ASSERT( dims.rank() > 0,
        "String type can not have a rank-0 array sample" );
    std::string dimsName = iName + ".dims";
    WriteDimensions( iGroup, dimsName, dims );

    // See whether or not we've already stored this.
    WrittenArraySampleIDPtr writeID = iMap.find( iKey );
    if ( writeID )
    {
        CopyWrittenArray( iGroup, iName, writeID );
        return writeID;
    }

    // Okay, need to actually store it.
    // It will be a dataset with an internal attribute for storing
    // the hash id.

    bool hasData = dims.numPoints() > 0;
    hid_t dspaceId = -1;
    Dimensions wdims; // Used to store the "fake" dimensions.
    std::vector<CharT> charBuffer;

    // Get the dimensions, validate sample size.
    if ( hasData )
    {
        size_t extent = iSamp.getDataType().getExtent();
        size_t numStrings = dims.numPoints() * extent;
        ABCA_ASSERT( dims.rank() > 0 && numStrings > 0,
                     "Degenerate array sample in WriteStringArrayT" );

        // Get the data out of the array sample.
        const StringT *strings =
            reinterpret_cast<const StringT *>( iSamp.getData() );
        ABCA_ASSERT( strings,
                     "Degenerate strings in WriteStringArrayT" );

        // Compact the strings in the string array.
        CompactStrings( strings, numStrings, charBuffer );

        // Create the dataspace.
        size_t len = charBuffer.size();
        assert( len >= numStrings );
        wdims = Dimensions( len );
        HDimensions hdims( wdims );

        dspaceId = H5Screate_simple( hdims.rank(),
                                     hdims.rootPtr(), NULL );
    }
    else
    {
        dspaceId = H5Screate( H5S_NULL );
    }

    ABCA_ASSERT( dspaceId >= 0,
                 "WriteStringsT() Failed in dataspace constructor" );
    DspaceCloser dspaceCloser( dspaceId );

    hid_t dsetId = -1;
    if ( iCompressionLevel >= 0 && hasData )
    {
        // Make a compression plist
        hid_t zipPlist = DsetGzipCreatePlist( wdims,
            iCompressionLevel > 9 ? 9 : iCompressionLevel );
        PlistCloser plistCloser( zipPlist );

        //std::cout << "Creating compressed data set named: "
        //          << iName << " in group named: " << iGroup.name()
        //          << std::endl;

        // Make the dataset.
        dsetId = H5Dcreate2( iGroup, iName.c_str(), GetFileDtype<CharT>(),
                             dspaceId, H5P_DEFAULT, zipPlist, H5P_DEFAULT );
    }
    else
    {
        //std::cout << "Creating uncompressed data set named: "
        //          << iName << " in group named: " << iGroup.name()
        //          << std::endl;
        dsetId = H5Dcreate2( iGroup, iName.c_str(), GetFileDtype<CharT>(),
                             dspaceId, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
    }
    DsetCloser dsetCloser(dsetId);

    ABCA_ASSERT( dsetId >= 0,
                 "WriteArray() Failed in dataset constructor" );

    // Write the data.
    if ( hasData )
    {
        H5Dwrite( dsetId, GetNativeDtype<CharT>(), H5S_ALL,
                  H5S_ALL, H5P_DEFAULT, &charBuffer.front() );
    }

    // Write the key
    WriteKey( dsetId, "key", iKey );

    writeID.reset( new WrittenArraySampleID( iKey, dsetId ) );

    iMap.store( writeID );

    // Return the reference.
    return writeID;
}
Esempio n. 10
0
int GTIFWriteKeys(GTIF *gt)
{
    int i;
    GeoKey *keyptr;
    KeyEntry *entptr;
    KeyHeader *header;
    TempKeyData tempData;
    int sortkeys[MAX_KEYS];
	
    if (!(gt->gt_flags & FLAG_FILE_MODIFIED)) return 1;

    if( gt->gt_tif == NULL )
        return 0;
	
    tempData.tk_asciiParams = 0;
    tempData.tk_asciiParamsLength = 0;
    tempData.tk_asciiParamsOffset = 0;

    /*  Sort the Keys into numerical order */
    if (!SortKeys(gt,sortkeys))
    {
        /* XXX error: a key was not recognized */
    }
	
    /* Set up header of ProjectionInfo tag */
    header = (KeyHeader *)gt->gt_short;
    header->hdr_num_keys = (pinfo_t) gt->gt_num_keys;
    header->hdr_version  = GvCurrentVersion;
    header->hdr_rev_major  = GvCurrentRevision;
    header->hdr_rev_minor  = GvCurrentMinorRev;
	
    /* Sum up the ASCII tag lengths */
    for (i = 0; i < gt->gt_num_keys; i++)
    {
        keyptr = gt->gt_keys + sortkeys[i];
        if (keyptr->gk_type == TYPE_ASCII)
        {
            tempData.tk_asciiParamsLength += keyptr->gk_count;
        }
    }
    if (tempData.tk_asciiParamsLength > 0)
    {
        tempData.tk_asciiParams =
            (char *)_GTIFcalloc(tempData.tk_asciiParamsLength + 1);
        tempData.tk_asciiParams[tempData.tk_asciiParamsLength] = '\0';
    }

    /* Set up the rest of SHORT array properly */
    keyptr = gt->gt_keys;
    entptr = (KeyEntry*)(gt->gt_short + 4);
    for (i=0; i< gt->gt_num_keys; i++,entptr++)
    {
        if (!WriteKey(gt,&tempData,entptr,keyptr+sortkeys[i])) return 0;
    }	
	
    /* Write out the Key Directory */
    (gt->gt_methods.set)(gt->gt_tif, GTIFF_GEOKEYDIRECTORY, gt->gt_nshorts, gt->gt_short );	
	
    /* Write out the params directories */
    if (gt->gt_ndoubles)
        (gt->gt_methods.set)(gt->gt_tif, GTIFF_DOUBLEPARAMS, gt->gt_ndoubles, gt->gt_double );
    if (tempData.tk_asciiParamsLength > 0)
    {
        /* just to be safe */
        tempData.tk_asciiParams[tempData.tk_asciiParamsLength] = '\0';
        (gt->gt_methods.set)(gt->gt_tif,
                             GTIFF_ASCIIPARAMS, 0, tempData.tk_asciiParams);
    }
	
    gt->gt_flags &= ~FLAG_FILE_MODIFIED;

    if (tempData.tk_asciiParamsLength > 0)
    {
        _GTIFFree (tempData.tk_asciiParams);
    }
    return 1;
}
Esempio n. 11
0
//-*****************************************************************************
WrittenArraySampleIDPtr
WriteArray( WrittenArraySampleMap &iMap,
            hid_t iGroup,
            const std::string &iName,
            const AbcA::ArraySample &iSamp,
            const AbcA::ArraySample::Key &iKey,
            hid_t iFileType,
            hid_t iNativeType,
            int iCompressionLevel )
{

    // Dispatch to string writing utils.
    const AbcA::DataType &dataType = iSamp.getDataType();
    if ( dataType.getPod() == kStringPOD )
    {
        return WriteStringArray( iMap, iGroup, iName, iSamp, iKey,
                                 iCompressionLevel );
    }
    else if ( dataType.getPod() == kWstringPOD )
    {
        return WriteWstringArray( iMap, iGroup, iName, iSamp, iKey,
                                  iCompressionLevel );
    }

    // write the dimensions as necessary
    Dimensions dims = iSamp.getDimensions();
    size_t rank = dims.rank();

    ABCA_ASSERT( rank > 0, "Cannot have a rank-0 array sample" );

    // rank 1 is the most common case, and we can easily infer it's size
    // from the dataspace for non-strings, so don't bother writing it out
    if (rank > 1)
    {
        std::string dimsName = iName + ".dims";
        WriteDimensions( iGroup, dimsName, dims );
    }

    // See whether or not we've already stored this.
    WrittenArraySampleIDPtr writeID = iMap.find( iKey );
    if ( writeID )
    {
        CopyWrittenArray( iGroup, iName, writeID );
        return writeID;
    }

    // Okay, need to actually store it.
    // It will be a dataset with an internal attribute for storing
    // the hash id.

    bool hasData = dims.numPoints() > 0;

    hid_t dspaceId = -1;
    if ( hasData )
    {
        hsize_t hdim = dims.numPoints() * dataType.getExtent();
        dspaceId = H5Screate_simple( 1, &hdim, NULL );
    }
    else
    {
        dspaceId = H5Screate( H5S_NULL );
    }

    ABCA_ASSERT( dspaceId >= 0,
                 "WriteArray() Failed in dataspace construction" );
    DspaceCloser dspaceCloser( dspaceId );

    hid_t dsetId = -1;
    if ( iCompressionLevel >= 0 && hasData )
    {
        // Make a compression plist
        hid_t zipPlist = DsetGzipCreatePlist( dims,
            iCompressionLevel > 9 ? 9 : iCompressionLevel );
        PlistCloser plistCloser( zipPlist );

        // Make the dataset.
        dsetId = H5Dcreate2( iGroup, iName.c_str(), iFileType, dspaceId,
                             H5P_DEFAULT, zipPlist, H5P_DEFAULT );
    }
    else
    {
        dsetId = H5Dcreate2( iGroup, iName.c_str(),
                             iFileType, dspaceId,
                             H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
    }
    DsetCloser dsetCloser(dsetId);

    ABCA_ASSERT( dsetId >= 0,
                 "WriteArray() Failed in dataset constructor" );

    // Write the data.
    if ( hasData )
    {
        H5Dwrite( dsetId, iNativeType, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                  iSamp.getData() );
    }

    // Write the array sample key.
    WriteKey( dsetId, "key", iKey );

    writeID.reset( new WrittenArraySampleID( iKey, dsetId ) );
    iMap.store( writeID );

    // Return the reference.
    return writeID;
}
Esempio n. 12
0
/*!
 This is a recursive function that gets the type of the \c value object and
 calls several protected functions depending on the type:

 \li \c WriteNullvalue for type NULL
 \li \c WriteStringValue() for STRING and CSTRING types
 \li \c WriteIntValue for INT types
 \li \c WriteUIntValue for UINT types
 \li \c WriteBoolValue for BOOL types
 \li \c WriteDoubleValue for DOUBLE types
 \li \c WriteMemoryBuff for MEMORYBUFF types

 If the value is an array or key/value map (types ARRAY and OBJECT), the function
 iterates through all JSON value object in the array/map and calls itself for every
 item in the container.
*/
int
wxJSONWriter::DoWrite( wxOutputStream& os, const wxJSONValue& value, const wxString* key, bool comma )
{
    // note that this function is recursive

    // some variables that cannot be allocated in the switch statement
    const wxJSONInternalMap* map = 0;
    int size;
    m_colNo = 1; m_lineNo = 1;
    // determine the comment position; it is one of:
    //
    //  wxJSONVALUE_COMMENT_BEFORE
    //  wxJSONVALUE_COMMENT_AFTER
    //  wxJSONVALUE_COMMENT_INLINE
    //
    // or -1 if comments have not to be written
    int commentPos = -1;
    if ( value.GetCommentCount() > 0 && (m_style & wxJSONWRITER_WRITE_COMMENTS))  {
        commentPos = value.GetCommentPos();
        if ( ( m_style & wxJSONWRITER_COMMENTS_BEFORE) != 0 ) {
            commentPos = wxJSONVALUE_COMMENT_BEFORE;
        }
        else if ( (m_style & wxJSONWRITER_COMMENTS_AFTER) != 0 ) {
            commentPos = wxJSONVALUE_COMMENT_AFTER;
        }
    }

    int lastChar = 0;  // check if WriteComment() writes the last LF char

    // first write the comment if it is BEFORE
    if ( commentPos == wxJSONVALUE_COMMENT_BEFORE )   {
        lastChar = WriteComment( os, value, true );
        if ( lastChar < 0 )   {
            return lastChar;
        }
        else if ( lastChar != '\n' )  {
            WriteSeparator( os );
        }
    }

    lastChar = WriteIndent( os );
    if ( lastChar < 0 )   {
        return lastChar;
    }

    // now write the key if it is not NULL
    if ( key )   {
        lastChar = WriteKey( os, *key );
    }
    if ( lastChar < 0 )   {
        return lastChar;
    }

    // now write the value
    wxJSONInternalMap::const_iterator it;    // declare the map object
    long int count = 0;

    wxJSONType t = value.GetType();
    switch ( t )  {
    case wxJSONTYPE_INVALID :
        WriteInvalid( os );
        wxFAIL_MSG( _T("wxJSONWriter::WriteEmpty() cannot be called (not a valid JSON text"));
        break;

    case wxJSONTYPE_INT :
    case wxJSONTYPE_SHORT :
    case wxJSONTYPE_LONG :
    case wxJSONTYPE_INT64 :
        lastChar = WriteIntValue( os, value );
        break;

    case wxJSONTYPE_UINT :
    case wxJSONTYPE_USHORT :
    case wxJSONTYPE_ULONG :
    case wxJSONTYPE_UINT64 :
        lastChar = WriteUIntValue( os, value );
        break;

    case wxJSONTYPE_NULL :
        lastChar = WriteNullValue( os );
        break;
    case wxJSONTYPE_BOOL :
        lastChar = WriteBoolValue( os, value );
        break;

    case wxJSONTYPE_DOUBLE :
        lastChar = WriteDoubleValue( os, value );
        break;

    case wxJSONTYPE_STRING :
    case wxJSONTYPE_CSTRING :
        lastChar = WriteStringValue( os, value.AsString());
        break;

    case wxJSONTYPE_MEMORYBUFF :
        lastChar = WriteMemoryBuff( os, value.AsMemoryBuff());
        break;

    case wxJSONTYPE_ARRAY :
        ++m_level;
        os.PutC( '[' );
        // the inline comment for objects and arrays are printed in the open char
        if ( commentPos == wxJSONVALUE_COMMENT_INLINE )   {
            commentPos = -1;  // we have already written the comment
            lastChar = WriteComment( os, value, false );
            if ( lastChar < 0 )   {
                return lastChar;
            }
            if ( lastChar != '\n' )   {
                lastChar = WriteSeparator( os );
            }
        }
        else   {    // comment is not to be printed inline, so write a LF
            lastChar = WriteSeparator( os );
            if ( lastChar < 0 )   {
                return lastChar;
            }
        }

        // now iterate through all sub-items and call DoWrite() recursively
        size = value.Size();
        for ( int i = 0; i < size; i++ )  {
            bool comma = false;
            if ( i < size - 1 )  {
                comma = true;
            }
            wxJSONValue v = value.ItemAt( i );
            lastChar = DoWrite( os, v, 0, comma );
            if ( lastChar < 0 )  {
                return lastChar;
            }

        }
        --m_level;
        lastChar = WriteIndent( os );
        if ( lastChar < 0 )   {
            return lastChar;
        }
        os.PutC( ']' );
        break;

    case wxJSONTYPE_OBJECT :
        ++m_level;

        os.PutC( '{' );
        // the inline comment for objects and arrays are printed in the open char
        if ( commentPos == wxJSONVALUE_COMMENT_INLINE )   {
            commentPos = -1;  // we have already written the comment
            lastChar = WriteComment( os, value, false );
            if ( lastChar < 0 )   {
                return lastChar;
            }
            if ( lastChar != '\n' )   {
                WriteSeparator( os );
            }
        }
        else   {
            lastChar = WriteSeparator( os );
        }

        map = value.AsMap();
        size = value.Size();
        count = 0;
        for ( it = map->begin(); it != map->end(); ++it )  {
            // get the key and the value
            wxString key = it->first;
            const wxJSONValue& v = it->second;
            bool comma = false;
            if ( count < size - 1 )  {
                comma = true;
            }
            lastChar = DoWrite( os, v, &key, comma );
            if ( lastChar < 0 )  {
                return lastChar;
            }
            count++;
        }
        --m_level;
        lastChar = WriteIndent( os );
        if ( lastChar < 0 )   {
            return lastChar;
        }
        os.PutC( '}' );
        break;

    default :
        // a not yet defined wxJSONType: we FAIL
        wxFAIL_MSG( _T("wxJSONWriter::DoWrite() undefined wxJSONType type"));
        break;
    }

    // writes the comma character before the inline comment
    if ( comma )  {
        os.PutC( ',' );
    }

    if ( commentPos == wxJSONVALUE_COMMENT_INLINE )   {
        lastChar = WriteComment( os, value, false );
        if ( lastChar < 0 )   {
            return lastChar;
        }
    }
    else if ( commentPos == wxJSONVALUE_COMMENT_AFTER )   {
        WriteSeparator( os );
        lastChar = WriteComment( os, value, true );
        if ( lastChar < 0 )   {
            return lastChar;
        }
    }
    if ( lastChar != '\n' )  {
        lastChar = WriteSeparator( os );
    }
    return lastChar;
}
Esempio n. 13
0
size_t JsonWriteRichPresenceObj(char* dest,
                                size_t maxLen,
                                int nonce,
                                int pid,
                                const DiscordRichPresence* presence)
{
    JsonWriter writer(dest, maxLen);

    {
        WriteObject top(writer);

        JsonWriteNonce(writer, nonce);

        WriteKey(writer, "cmd");
        writer.String("SET_ACTIVITY");

        {
            WriteObject args(writer, "args");

            WriteKey(writer, "pid");
            writer.Int(pid);

            if (presence != nullptr) {
                WriteObject activity(writer, "activity");

                WriteOptionalString(writer, "state", presence->state);
                WriteOptionalString(writer, "details", presence->details);

                if (presence->startTimestamp || presence->endTimestamp) {
                    WriteObject timestamps(writer, "timestamps");

                    if (presence->startTimestamp) {
                        WriteKey(writer, "start");
                        writer.Int64(presence->startTimestamp);
                    }

                    if (presence->endTimestamp) {
                        WriteKey(writer, "end");
                        writer.Int64(presence->endTimestamp);
                    }
                }

                if ((presence->largeImageKey && presence->largeImageKey[0]) ||
                    (presence->largeImageText && presence->largeImageText[0]) ||
                    (presence->smallImageKey && presence->smallImageKey[0]) ||
                    (presence->smallImageText && presence->smallImageText[0])) {
                    WriteObject assets(writer, "assets");
                    WriteOptionalString(writer, "large_image", presence->largeImageKey);
                    WriteOptionalString(writer, "large_text", presence->largeImageText);
                    WriteOptionalString(writer, "small_image", presence->smallImageKey);
                    WriteOptionalString(writer, "small_text", presence->smallImageText);
                }

                if ((presence->partyId && presence->partyId[0]) || presence->partySize ||
                    presence->partyMax) {
                    WriteObject party(writer, "party");
                    WriteOptionalString(writer, "id", presence->partyId);
                    if (presence->partySize && presence->partyMax) {
                        WriteArray size(writer, "size");
                        writer.Int(presence->partySize);
                        writer.Int(presence->partyMax);
                    }
                }

                if ((presence->matchSecret && presence->matchSecret[0]) ||
                    (presence->joinSecret && presence->joinSecret[0]) ||
                    (presence->spectateSecret && presence->spectateSecret[0])) {
                    WriteObject secrets(writer, "secrets");
                    WriteOptionalString(writer, "match", presence->matchSecret);
                    WriteOptionalString(writer, "join", presence->joinSecret);
                    WriteOptionalString(writer, "spectate", presence->spectateSecret);
                }

                writer.Key("instance");
                writer.Bool(presence->instance != 0);
            }
        }
    }

    return writer.Size();
}
Esempio n. 14
0
 WriteArray(JsonWriter& w, T& name)
   : writer(w)
 {
     WriteKey(writer, name);
     writer.StartArray();
 }
Esempio n. 15
0
 WriteObject(JsonWriter& w, T& name)
   : writer(w)
 {
     WriteKey(writer, name);
     writer.StartObject();
 }
Esempio n. 16
0
void
WritePrivateProfileString (const char *section, const char *var,
			   const char *var_name, const char *filename)
{
  FILE *file;
  struct stat sb;
  int res;

  res = stat (filename, &sb);
  if (-1 == res)		// fichier n'existe pas
    {
      file = fopen (filename, "w");
      InsertSectionKey (section, var, var_name, file, 0);
      fclose (file);
    }
  else
    {
      int filesize;
      filesize = sb.st_size - 1;	// on ne lit pas EOT
      if (filesize >= 0)
	{
	  char *buf;
	  char *section_begin;
	  int size1, size2;

	  buf = file2buf (filename);

	  section_begin = SearchSection (buf, section);
	  if (NULL != section_begin)	//section existe déjà
	    {
	      char *key_begin;
	      key_begin = SearchKey (section_begin, var);
	      if (NULL != key_begin)	// la clé existe : on copie la partie d'avant l'ancienne clé, la nouvelle clé, puis la partie d'après l'ancienne clé
		{
		  int old_key_size;
		  char *key_end;

		  size1 = key_begin - buf;
		  key_end = strstr (key_begin, "\n");
		  if (NULL != key_end)
		    {
		      old_key_size = key_end - key_begin + 1;
		      size2 = filesize - (size1 + old_key_size);
		      file = fopen (filename, "w");
		      fwrite (buf, size1, 1, file);
		      WriteKey (var, var_name, file);
		      fwrite (buf + size1 + old_key_size, size2, 1, file);
		      fclose (file);
		    }
		  else
		    {		// la clé est la dernière du fichier
		      file = fopen (filename, "w");
		      fwrite (buf, size1, 1, file);
		      WriteKey (var, var_name, file);
		      fclose (file);
		    }

		}
	      else
		{		// clé n'existe pas : on la rajoute avant la prochaine section
		  char *next_section_begin;
		  next_section_begin = strstr (section_begin, "*");
		  if (NULL == next_section_begin)	// la section recherchée est unique : il suffit de copier la clé à la fin du fichier
		    {
		      file = fopen (filename, "a");
		      WriteKey (var, var_name, file);
		      fclose (file);
		    }
		  else
		    {		// on insère la clé avant la prochaine section
		      size1 = next_section_begin - buf;
		      size2 = filesize - size1;
		      file = fopen (filename, "w");
		      fwrite (buf, size1, 1, file);
		      WriteKey (var, var_name, file);
		      fwrite (buf + size1, size2, 1, file);
		      fclose (file);
		    }
		}
	    }
	  else
	    {			//section n'existe pas : on la rajoute ainsi que la clé
	      file = fopen (filename, "a");
	      InsertSectionKey (section, var, var_name, file, 1);
	      fclose (file);
	    }
	  free (buf);
	}
      else
	{			//taille fichier < 0
	  file = fopen (filename, "w");
	  InsertSectionKey (section, var, var_name, file, 0);
	  fclose (file);
	}
    }
}