]> git.lizzy.rs Git - bspwm.git/blobdiff - helpers.c
Update the documentation regarding query/restore
[bspwm.git] / helpers.c
index d70ab20d5c7b7f9ab6212a6b96637a71e192ee58..c342b6c65b08afa39608a6c1f5cc3befc79df6ea 100644 (file)
--- a/helpers.c
+++ b/helpers.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2012-2014, Bastien Dejean
+/* Copyright (c) 2012, Bastien Dejean
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are those
- * of the authors and should not be interpreted as representing official policies,
- * either expressed or implied, of the FreeBSD Project.
  */
 
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
 #include <math.h>
 #include "bspwm.h"
 
@@ -49,6 +47,58 @@ void err(char *fmt, ...)
        exit(EXIT_FAILURE);
 }
 
+char *read_string(const char *file_path, size_t *tlen)
+{
+       if (file_path == NULL) {
+               return NULL;
+       }
+
+       int fd = open(file_path, O_RDONLY);
+
+       if (fd == -1) {
+               perror("Read file: open");
+               return NULL;
+       }
+
+       char buf[BUFSIZ], *content;
+       size_t len = sizeof(buf);
+
+       if ((content = malloc(len * sizeof(char))) == NULL) {
+               perror("Read file: malloc");
+               return NULL;
+       }
+
+       int nb;
+       *tlen = 0;
+
+       while (true) {
+               nb = read(fd, buf, sizeof(buf));
+               if (nb < 0) {
+                       perror("Restore tree: read");
+                       free(content);
+                       return NULL;
+               } else if (nb == 0) {
+                       break;
+               } else {
+                       *tlen += nb;
+                       if (*tlen > len) {
+                               len *= 2;
+                               char *rcontent = realloc(content, len * sizeof(char));
+                               if (rcontent == NULL) {
+                                       perror("Read file: realloc");
+                                       free(content);
+                                       return NULL;
+                               } else {
+                                       content = rcontent;
+                               }
+                       }
+                       strncpy(content + (*tlen - nb), buf, nb);
+               }
+       }
+
+       return content;
+}
+
 bool get_color(char *col, xcb_window_t win, uint32_t *pxl)
 {
        xcb_colormap_t map = screen->default_colormap;
@@ -87,9 +137,3 @@ double distance(xcb_point_t a, xcb_point_t b)
 {
        return hypot(a.x - b.x, a.y - b.y);
 }
-
-void center_rectangle(xcb_rectangle_t *src, xcb_rectangle_t dst)
-{
-       src->x = dst.x + (dst.width - src->width) / 2;
-       src->y = dst.y + (dst.height - src->height) / 2;
-}