00001 /* 00002 * Copyright 2008 The qDecoder Project. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE QDECODER PROJECT ``AS IS'' AND ANY 00015 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00016 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00017 * DISCLAIMED. IN NO EVENT SHALL THE QDECODER PROJECT BE LIABLE FOR ANY 00018 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00019 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00020 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00021 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00024 */ 00025 00038 #include <stdio.h> 00039 #include <stdlib.h> 00040 #include <stdbool.h> 00041 #include <fcntl.h> 00042 #include <unistd.h> 00043 #include <sys/types.h> 00044 #include <sys/stat.h> 00045 #include "qDecoder.h" 00046 #include "qInternal.h" 00047 00061 int qCountRead(const char *filepath) { 00062 int fd = open(filepath, O_RDONLY, 0); 00063 if(fd < 0) return 0; 00064 00065 char buf[10+1]; 00066 if(read(fd, buf, sizeof(buf)) <= 0) { 00067 close(fd); 00068 return 0; 00069 } 00070 00071 return atoi(buf); 00072 } 00073 00087 bool qCountSave(const char *filepath, int number) { 00088 int fd = open(filepath, O_CREAT|O_WRONLY|O_TRUNC, DEF_FILE_MODE); 00089 if(fd < 0) return false; 00090 00091 if(_q_writef(fd, "%d", number) <= 0) { 00092 close(fd); 00093 return false; 00094 } 00095 00096 close(fd); 00097 return true; 00098 } 00099 00115 int qCountUpdate(const char *filepath, int number) { 00116 int counter = qCountRead(filepath); 00117 counter += number; 00118 return qCountSave(filepath, counter); 00119 }